Python Matplotlib直方图标签文本

Python Matplotlib直方图标签文本,python,matplotlib,histogram,Python,Matplotlib,Histogram,我在matplotlib中制作直方图,每个箱子的文本标签相互重叠,如下所示: 我试图通过以下方式在x轴上旋转标签 但我收到错误消息“tuple”对象没有属性“set\xticklabels”。为什么?我如何解决这个问题?或者,如何“转置”绘图,使标签位于垂直轴上?给你。我把两个答案都集中在一个例子中: # create figure and ax objects, it is a good practice to always start with this fig, ax = plt.sub

我在matplotlib中制作直方图,每个箱子的文本标签相互重叠,如下所示:

我试图通过以下方式在x轴上旋转标签


但我收到错误消息
“tuple”对象没有属性“set\xticklabels”
。为什么?我如何解决这个问题?或者,如何“转置”绘图,使标签位于垂直轴上?

给你。我把两个答案都集中在一个例子中:

# create figure and ax objects, it is a good practice to always start with this
fig, ax = plt.subplots()

# then plot histogram using axis
# note that you can change orientation using keyword
ax.hist(np.random.rand(100), bins=10, orientation="horizontal")

# get_xticklabels() actually gets you an iterable, so you need to rotate each label
for tick in ax.get_xticklabels():
    tick.set_rotation(45)
它生成带有旋转x记号和水平直方图的图形。

可能会有帮助,因为它是关于旋转标签的

import matplotlib.pyplot as plt


x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']

plt.plot(x, y, 'ro')
# You can specify a rotation for the tick labels in degrees or with keywords.
plt.xticks(x, labels, rotation='vertical')
# Pad margins so that markers don't get clipped by the axes
plt.margins(0.2)
# Tweak spacing to prevent clipping of tick-labels
plt.subplots_adjust(bottom=0.15)
plt.show()
所以我想

plt.xticks(x, labels, rotation='vertical')

这里是重要的一行。

plt.hist的返回值不是运行函数
setxticklabels

运行该函数的是一个
matplotlib.axes.\u subplot.AxesSubplot
,您可以从这里获得:

fig, ax = plt.subplots(1, 1)
cuisine_hist = ax.hist(train.cuisine, bins=100)
ax.set_xticklabels(rotation=45)
plt.show()
从plt.hist的“帮助”中:

Returns
-------
n : array or list of arrays
    The values of the histogram bins. See *normed* or *density*

bins : array
    The edges of the bins. ...

patches : list or list of lists
   ...
Returns
-------
n : array or list of arrays
    The values of the histogram bins. See *normed* or *density*

bins : array
    The edges of the bins. ...

patches : list or list of lists
   ...