Python Matplotlib X轴不使用一个名称
Im使用两个列表填充matplotlib中的简单线图: X轴=结果[]和 Y轴=名称[] 以下是“名称”包含的内容: ['RF','GBR','KNN','CART','SVM','NN','ADA'] 以下是“结果”包含的内容: [0.8941514126644701, 0.8821308167746561, 0.7864150401953114, 0.812054320544109, 0.14266745822021032, 0.7439163675782782, 0.7350927779887548] Im使用简单的布局进行绘图,如下所示:Python Matplotlib X轴不使用一个名称,python,matplotlib,Python,Matplotlib,Im使用两个列表填充matplotlib中的简单线图: X轴=结果[]和 Y轴=名称[] 以下是“名称”包含的内容: ['RF','GBR','KNN','CART','SVM','NN','ADA'] 以下是“结果”包含的内容: [0.8941514126644701, 0.8821308167746561, 0.7864150401953114, 0.812054320544109, 0.14266745822021032, 0.7439163675782782, 0.73509277798
fig = plt.figure(figsize=(9,6))
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
plt.plot(results)
ax.set_xticklabels(names)
plt.show()
但我的情节在列表中只少了一个元素;下面是它的外观:
我在这里遗漏了什么?调用
ax.get_xticks()
告诉我们matplotlib在以下位置显示刻度:
[-1. 0. 1. 2. 3. 4. 5. 6. 7.]
这意味着第一个标签出现在图表左侧水平轴上的-1处。在调用plot
后添加以下行,以设置记号的位置并使标签对齐:
ax.set_xticks(range(len(names)))
如果不设置刻度,则无法设置标签。要么同时指定,要么不指定
fig = plt.figure(figsize=(9,6))
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
plt.plot(results)
ax.set_xticks(list(range(len(results))))
ax.set_xticklabels(names)
plt.show()
或
什么是我的_xticks?对不起-我不小心把它放在里面了(这不是问题的根源)。这篇文章已被编辑。
fig = plt.figure(figsize=(9,6))
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)
plt.plot(names, results)
plt.show()