字符串x轴的直方图

时间:2018-04-16 05:22:50

标签: python matplotlib jupyter

直方图/条形图是否可以显示字符串x轴标题?下面我试图显示带有预先配置的x和y轴值的图表,其中每个x轴值都是一个字符串范围。

代码如下:

import matplotlib.pyplot as plt

x = ['1 - 100','200 - 400','400 - 600','600 - 800','800 - 100']
y = [10,2,3,4,5]

plt.bar(x,y,align='center') # A bar chart
plt.xlabel('Number iterations')
plt.ylabel('Time taken')
plt.show()

导致错误:

TypeError: unsupported operand type(s) for -: 'str' and 'float

2 个答案:

答案 0 :(得分:0)

根据@miradulo& @tom评论问题确实是使用matplotlib pre 2.1.0。此代码实现了类似的效果:

import matplotlib.pyplot as plt

x = [100, 200, 300, 400, 500, 600, 700]
y = [10,2 , 5 , 6 , 6 , 7 , 8]

plt.bar(x,y,align='center', width=30) # A bar chart
plt.xlabel('Number iterations')
plt.ylabel('Time taken')
plt.show()

但遗憾的是不包括字符串范围。

答案 1 :(得分:-1)

删除 - 标志。这会将你的整数变成字符串。

如果使用直方图,请使用bins = 200。

你确定1-100没有错误吗?