Python 如何在 matplotlib / pylab 图表中水平打印 Y 轴标签?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27671748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print Y axis label horizontally in a matplotlib / pylab chart?
提问by Karl D
I'm creating very simple charts with matplotlib / pylab Python module. The letter "y" that labels the Y axis is on its side. You would expect this if the label was longer, such as a word, so as not to extend the outside of the graph to the left too much. But for a one letter label, this doesn't make sense, the label should be upright. My searches have come up blank. How can I print the "y" horizontally?
我正在使用 matplotlib / pylab Python 模块创建非常简单的图表。标记 Y 轴的字母“y”在其一侧。如果标签更长,例如一个单词,您会期望这种情况,以免将图形的外部向左扩展太多。但是对于单字母标签,这没有意义,标签应该是直立的。我的搜索结果空白。如何水平打印“y”?
采纳答案by Jens Munk
It is very simple. After plotting the label, you can simply change the rotation:
这很简单。绘制标签后,您可以简单地更改旋转:
from matplotlib import pyplot as plt
plt.ion()
plt.plot([1,2,3])
h = plt.ylabel('y')
h.set_rotation(0)
plt.draw()
Alternatively, you can pass the rotation as an argument, i.e
或者,您可以将旋转作为参数传递,即
plt.ylabel('y',rotation=0)
回答by Evgeni Sergeev
Expanding on the accepted answer, when we work with a particular axes object ax
:
当我们使用特定的轴对象时,扩展已接受的答案ax
:
ax.set_ylabel('abc', rotation=0, fontsize=20, labelpad=20)
Note that often the labelpad
will need to be adjusted manually too — otherwise the "abc" will intrude onto the plot.
请注意,通常labelpad
也需要手动调整 - 否则“abc”将侵入情节。
From brief experiments I'm guessing that labelpad
is the offset between the bounding box of the tick labels and the y-label's centre. (So, not quite the padding the name implies — it would have been more intuitive if this was the gap to the label's bounding box instead.)
从简短的实验中,我猜测这labelpad
是刻度标签的边界框与 y 标签中心之间的偏移量。(因此,并不是名称所暗示的填充——如果这是与标签边界框的间隙,它会更直观。)