我试图让非常长的x轴标签变成多行,就像右图中圈出来的那样:
我目前正在使用的代码:
ax = cluster3.plot(x='Neighborhood',y=['Population'],kind='bar',alpha=0.75,title='Population of Each Neighborhood',figsize=(15, 10))
ax.set_ylabel('Population')
cluster3是一个带有Neighborhood列的数据框
我试图让非常长的x轴标签变成多行,就像右图中圈出来的那样:
我目前正在使用的代码:
ax = cluster3.plot(x='Neighborhood',y=['Population'],kind='bar',alpha=0.75,title='Population of Each Neighborhood',figsize=(15, 10))
ax.set_ylabel('Population')
cluster3是一个带有Neighborhood列的数据框
在你的标签和你想要换行的位置添加"\n"
来插入换行。
如果你想每隔x个字符就自动换行,可以使用以下方法:
def insert_linebreak(string, lengLabel=48):
return '\n'.join(string[i:i+lengLabel] for i in xrange(0, len(string), every))
\n
字符。例如,"All creatures from out of space" 可以变成 "All creatures\nfrom out of space"。
textwrap.fill()
,它(引用文档)"是"\n".join(wrap(text, ...))
的简写。" - Skippy le Grand Gourou