简体   繁体   English

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

[英]How to change the font size of axes labels of a histogram

I created a histogram of string occurrences based on a column of a pandas dataframe. 我基于熊猫数据框的列创建了字符串出现的直方图。 The strings are quite long so when I plot the histogram, the labels get cut off. 字符串很长,因此当我绘制直方图时,标签会被切除。 How do I change the size of the tick labels? 如何更改刻度标签的大小?

I am aware of set_xticklabels, but I'm not quite sure how to use this with the histogram I created. 我知道set_xticklabels,但是我不太确定如何将其与我创建的直方图一起使用。

Here's how I created my histogram: 这是我创建直方图的方式:

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

plt.xtics()

Look into the fontsize argument of plt.xticks() : 查看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()

在此处输入图片说明

With rotation 旋转

You could also look into rotating your tick labels: 您还可以考虑旋转刻度线标签:

# 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()

在此处输入图片说明

Horizontal

Yet another option is to make your bar chart horizontal. 另一个选择是使条形图水平。 It still takes up space (horizontal rather than vertical) but makes longer labels easier to read. 它仍然占用空间(水平而不是垂直),但使较长的标签更易于阅读。

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

plt.barh('x', 'y', data=df)
plt.show()

在此处输入图片说明

To change the font size you can use, assuming you have imported matplotlib.pyplot as plt 要更改字体大小,可以使用,假设您已将matplotlib.pyplot导入为plt

plt.xlabel('xlabel', fontsize=10)
plt.ylabel('ylabel', fontsize=10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM