[英]How do I set the figure title and axes labels font size in Matplotlib?
[英]How to change the font size of axes labels of a histogram
我基于熊猫数据框的列创建了字符串出现的直方图。 字符串很长,因此当我绘制直方图时,标签会被切除。 如何更改刻度标签的大小?
我知道set_xticklabels,但是我不太确定如何将其与我创建的直方图一起使用。
这是我创建直方图的方式:
assignments_df[['Major:']].apply(pd.value_counts).plot(kind = 'bar', subplots = True)
plt.savefig('Major_Distribution.png')
plt.xtics()
查看plt.xticks()
的fontsize
参数:
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()
您还可以考虑旋转刻度线标签:
# 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()
另一个选择是使条形图水平。 它仍然占用空间(水平而不是垂直),但使较长的标签更易于阅读。
df = pd.DataFrame({'x':['abcdedf','abcdef','abcde','abcd'],
'y':np.random.rand(4) // .01})
plt.barh('x', 'y', data=df)
plt.show()
要更改字体大小,可以使用,假设您已将matplotlib.pyplot导入为plt
plt.xlabel('xlabel', fontsize=10)
plt.ylabel('ylabel', fontsize=10)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.