marplot lib 运行如下代码遇到这个问题
import matplotlib.pyplot as plt
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
plt.figure(1, figsize=(9, 3))
plt.bar(names, values)
plt.show()
/Users/Reno_Lei/anaconda/lib/python3.6/site-packages/matplotlib/axes/_axes.py in bar(self, left, height, width, bottom, **kwargs)
2103 if align == 'center':
2104 if orientation == 'vertical':
-> 2105 left = [left[i] - width[i] / 2. for i in xrange(len(left))]
2106 elif orientation == 'horizontal':
2107 bottom = [bottom[i] - height[i] / 2.
/Users/Reno_Lei/anaconda/lib/python3.6/site-packages/matplotlib/axes/_axes.py in <listcomp>(.0)
2103 if align == 'center':
2104 if orientation == 'vertical':
-> 2105 left = [left[i] - width[i] / 2. for i in xrange(len(left))]
2106 elif orientation == 'horizontal':
2107 bottom = [bottom[i] - height[i] / 2.
TypeError: unsupported operand type(s) for -: 'str' and 'float'
原因是轴不能直接用 字符串列表
使用plt.xticks()可以解决这个问题
先将字符串列表转化为数字列表,再把数字列表和字符串列表组合起来
import matplotlib.pyplot as plt
names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
plt.figure(1, figsize=(9, 3))
x = range(len(names))
plt.xticks(x, names)
plt.bar(x, values)
plt.show()