如何从Pandas DataFrame直方图中删除y轴刻度标签

3
我想使用 pandas.DataFrame.hist 绘制我的 DataFrame 的直方图,但我不想显示 y 轴刻度标签。
我尝试了这个 solution 和这个 solution,但对于 pandas.DataFrame.hist 仍然不起作用。
到目前为止,代码看起来像这样:
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'length': [1.5, 0.5, 1.2, 0.9, 3],
    'width': [0.7, 0.2, 0.15, 0.2, 1.1]
    }, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])

fig, ax = plt.subplots()
ax.axes.get_yaxis().set_visible(False)
hist = df.hist(bins=3, ax=ax)

直方图如下:

enter image description here

但我希望它看起来像这样(在MSPaint上编辑):

enter image description here


上传图片失败;服务器发生错误。我的回答也遇到了同样的问题,所以那里也没有图片... - tturbo
2个回答

3

很奇怪,我不知道为什么你的代码不起作用。不过我找到了一个解决办法:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'length': [1.5, 0.5, 1.2, 0.9, 3],
    'width': [0.7, 0.2, 0.15, 0.2, 1.1]
    }, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])

fig, ax = plt.subplots()
df.hist(bins=3, ax=ax, ylabelsize=0) # <- set ylabelsize to zero

1
我想正确访问 AxesSubplot 对象的方式是通过 hist 对象进行,像这样。 这将产生所需的输出(目前无法上传 figs)。
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'length': [1.5, 0.5, 1.2, 0.9, 3],
    'width': [0.7, 0.2, 0.15, 0.2, 1.1]
    }, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])

fig, ax = plt.subplots()
hist = df.hist(bins=3, ax=ax)
hist[0][0].get_yaxis().set_visible(False)
hist[0][1].get_yaxis().set_visible(False)
plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接