我正在使用matplotlib生成(垂直)条形图 . 问题是我的标签很长 . 有没有办法在栏中或上方或下方垂直显示它们?
你的意思是这样的:
>>> from matplotlib import * >>> plot(xrange(10)) >>> yticks(xrange(10), rotation='vertical')
?
通常,要在matplotlib中显示垂直方向的任何文本,可以添加关键字 rotation='vertical' .
rotation='vertical'
有关更多选项,您可以查看帮助(matplotlib.pyplot.text)
yticks函数绘制y轴上的刻度;我不确定你最初是指这个还是ylabel函数,但程序是相同的,你必须添加rotation ='vertical'
也许您还可以找到有用的选项'verticalalignment'和'horizontalalignment',它允许您定义如何将文本与刻度线或其他元素对齐 .
在Jupyter Notebook中你可能会使用这样的东西
%matplotlib inline import matplotlib.pyplot as plt import numpy as np plt.xticks(rotation='vertical') plt.plot(np.random.randn(100).cumsum())
或者您可以使用:
plt.xticks(rotation=90)
我建议看the matplotlib gallery . 至少有两个例子似乎是相关的:
text_rotation.py了解文本布局的工作原理
barchart_demo2.py,一个条形图的示例,其布局比最基本的示例稍微复杂一些 .
请看看这个链接:https://python-graph-gallery.com/7-custom-barplot-layout/
import matplotlib.pyplot as plt heights = [10, 20, 15] bars = ['A_long', 'B_long', 'C_long'] y_pos = range(len(bars)) plt.bar(y_pos, heights) # Rotation of the bars names plt.xticks(y_pos, bars, rotation=90)
结果就像这样
希望它有所帮助 .
4 回答
你的意思是这样的:
?
通常,要在matplotlib中显示垂直方向的任何文本,可以添加关键字
rotation='vertical'
.有关更多选项,您可以查看帮助(matplotlib.pyplot.text)
yticks函数绘制y轴上的刻度;我不确定你最初是指这个还是ylabel函数,但程序是相同的,你必须添加rotation ='vertical'
也许您还可以找到有用的选项'verticalalignment'和'horizontalalignment',它允许您定义如何将文本与刻度线或其他元素对齐 .
在Jupyter Notebook中你可能会使用这样的东西
或者您可以使用:
我建议看the matplotlib gallery . 至少有两个例子似乎是相关的:
text_rotation.py了解文本布局的工作原理
barchart_demo2.py,一个条形图的示例,其布局比最基本的示例稍微复杂一些 .
请看看这个链接:https://python-graph-gallery.com/7-custom-barplot-layout/
结果就像这样
希望它有所帮助 .