Python 如何在matplotlib中指定重复标签的使用?
下面是一个简单的水平条形图示例,它是用Python 3和Matplotlib制作的:Python 如何在matplotlib中指定重复标签的使用?,python,matplotlib,Python,Matplotlib,下面是一个简单的水平条形图示例,它是用Python 3和Matplotlib制作的: from matplotlib import pyplot as plt labels = ['Dog', 'Cat', 'Dog', 'Mouse'] amounts = [5, 10, 12, 30] plt.barh(labels, amounts) plt.show() 这将生成以下图表: 如您所见,默认行为是忽略第一个“Dog”标签和值。然而,我想画两个独立的“狗”点,一个在5点,一个在12点
from matplotlib import pyplot as plt
labels = ['Dog', 'Cat', 'Dog', 'Mouse']
amounts = [5, 10, 12, 30]
plt.barh(labels, amounts)
plt.show()
这将生成以下图表:
如您所见,默认行为是忽略第一个“Dog”标签和值。然而,我想画两个独立的“狗”点,一个在5点,一个在12点。我如何才能做到这一点呢?首先按唯一的记号进行绘图,然后重命名
from matplotlib import pyplot as plt
labels = ['Dog', 'Cat', 'Dog', 'Mouse']
ticks = range(len(labels))
amounts = [5, 10, 12, 30]
fig = plt.barh(ticks, amounts)
plt.yticks(ticks, labels)
plt.show()
首先按唯一记号打印,然后重命名
from matplotlib import pyplot as plt
labels = ['Dog', 'Cat', 'Dog', 'Mouse']
ticks = range(len(labels))
amounts = [5, 10, 12, 30]
fig = plt.barh(ticks, amounts)
plt.yticks(ticks, labels)
plt.show()
如果您想要直方图,只需在此处提及文本的方向即可。您可以添加空间使字符串不同(如“Dogs”)。在文本前添加约20个空格进行测试,它们不会破坏格式。如果需要,这种方法很容易在程序中实现:只需在每个重复元素之前添加足够的空间
from matplotlib import pyplot as plt
labels = ['Dog', 'Cat', ' Dog', 'Mouse']
amounts = [5, 10, 12, 30]
plt.barh(labels, amounts)
plt.show()
如果您想要直方图,只需在此处提及文本的方向即可。您可以添加空间使字符串不同(如“Dogs”)。在文本前添加约20个空格进行测试,它们不会破坏格式。如果需要,这种方法很容易在程序中实现:只需在每个重复元素之前添加足够的空间
from matplotlib import pyplot as plt
labels = ['Dog', 'Cat', ' Dog', 'Mouse']
amounts = [5, 10, 12, 30]
plt.barh(labels, amounts)
plt.show()