如何更改直方图的轴标签的字体大小

时间:2019-07-08 18:04:10

标签: python pandas dataframe matplotlib histogram

我根据熊猫数据框的一列创建了字符串出现的直方图。字符串很长,因此当我绘制直方图时,标签会被切除。如何更改刻度标签的大小?

我知道set_xticklabels,但是我不太确定如何在创建的直方图中使用它。

这是我创建直方图的方式:

assignments_df[['Major:']].apply(pd.value_counts).plot(kind = 'bar', subplots = True)
plt.savefig('Major_Distribution.png')

2 个答案:

答案 0 :(得分:1)

plt.xtics()

查看fontsize的{​​{1}}参数:

plt.xticks()

enter image description here

旋转时

您还可以考虑旋转刻度线标签:

df = pd.DataFrame({'x':np.random.rand(20) // .01,
                  'y':np.random.rand(20) // .01})

df.plot(kind='bar')
plt.xticks(fontsize=8)
plt.show()

enter image description here

水平

另一个选择是使条形图水平。它仍然占据空间(水平而不是垂直),但使较长的标签更易于阅读。

# Different example data:
df = pd.DataFrame({'x':['abcdedf','abcdef','abcde','abcd'],
                  'y':np.random.rand(4) // .01})

df.plot(kind='bar', x='x', y='y')
plt.xticks(fontsize=8, rotation=45)
plt.show()

enter image description here

答案 1 :(得分:0)

要更改字体大小,请假设您已将matplotlib.pyplot导入为plt

plt.xlabel('xlabel', fontsize=10)
plt.ylabel('ylabel', fontsize=10)
相关问题