我的列表看起来像这样
top = [('a',1.875),('c',1.125),('d',0.5)]
有人可以帮我绘制条形图,其中x轴为a,c,d和y轴值为1.875,1.125,0.5?
我尝试使用以下代码进行绘图。
import numpy as np
import matplotlib.pyplot as plt
top = [('a',1.875),('c',1.125),('d',0.5)]
labels, values = zip(*top)
indexes = np.arange(len(labels))
width = 1
plt.bar(indexes, values, width)
plt.xticks(indexes + width * 0.5, labels)
plt.savefig('netscore.png')
我可以绘制条形图,但图表中的y轴值是错误的。
答案 0 :(得分:7)
更改此行:
Optional<T> where T:AnyObject
为:
import numpy
更改此行:
import numpy as np
为:
labels, values = zip(*top[])
将这些错误排除在外:
使用labels, values = zip(*top)
方法:
axes
使用import numpy as np
import matplotlib.pyplot as plt
top=[('a',1.875),('c',1.125),('d',0.5)]
labels, ys = zip(*top)
xs = np.arange(len(labels))
width = 1
fig = plt.figure()
ax = fig.gca() #get current axes
ax.bar(xs, ys, width, align='center')
#Remove the default x-axis tick numbers and
#use tick numbers of your own choosing:
ax.set_xticks(xs)
#Replace the tick numbers with strings:
ax.set_xticklabels(labels)
#Remove the default y-axis tick numbers and
#use tick numbers of your own choosing:
ax.set_yticks(ys)
plt.savefig('netscore.png')
方法:
plt
答案 1 :(得分:0)
你正在调用numpy,但你没有使用它
就行了
indexes = np.arange(len(labels))
我猜你试图使用它,或者:
import numpy as np
或:
indexes = numpy.arange(len(labels))