条形图与python/matplotlib中的垂直标签

phi*_*hag 54 python charts matplotlib bar-chart

我正在使用matplotlib生成(垂直)条形图.问题是我的标签很长.有没有办法在栏中或上方或下方垂直显示它们?

dal*_*ogm 77

你的意思是这样的:

>>> from matplotlib import *
>>> plot(xrange(10))
>>> yticks(xrange(10), rotation='vertical')
Run Code Online (Sandbox Code Playgroud)

通常,要在matplotlib中显示垂直方向的任何文本,您可以添加关键字rotation='vertical'.

有关更多选项,您可以查看帮助(matplotlib.pyplot.text)

yticks函数绘制y轴上的刻度; 我不确定你最初是指这个还是ylabel函数,但程序是相同的,你必须添加rotation ='vertical'

也许您还可以找到有用的选项'verticalalignment'和'horizo​​ntalalignment',它允许您定义如何将文本与刻度线或其他元素对齐.

  • 谢谢!!不过,对于这样的答案来说,也许500分太多了,其实并没有那么难。所以,如果你,你可以把积分留到下次:) (2认同)

Vla*_*den 19

在Jupyter Notebook中你可能会使用这样的东西

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

plt.xticks(rotation='vertical')
plt.plot(np.random.randn(100).cumsum())
Run Code Online (Sandbox Code Playgroud)

或者您可以使用:

plt.xticks(rotation=90)
Run Code Online (Sandbox Code Playgroud)


Jou*_*nen 5

我建议看看matplotlib画廊.至少有两个例子似乎是相关的:


小智 5

请查看此链接: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)
Run Code Online (Sandbox Code Playgroud)

结果将是这样 在此处输入图片说明

希望它会有所帮助。