Python 无法更改matplotlib中的x记号
我正在努力更改以更改此绘图中的x_刻度标签。我想用['Decision Tree','Random Forest','SVM with SMOTE']替换0,1,2Python 无法更改matplotlib中的x记号,python,matplotlib,machine-learning,plot,Python,Matplotlib,Machine Learning,Plot,我正在努力更改以更改此绘图中的x_刻度标签。我想用['Decision Tree','Random Forest','SVM with SMOTE']替换0,1,2 # Plotting fig, ax = plt.subplots(1, 1, figsize=(14,6)) ax.set_xlabel("Machine learning model",fontsize=20) ax.set_ylabel("Score",fontsize=20) sns.set_style("whitegri
# Plotting
fig, ax = plt.subplots(1, 1, figsize=(14,6))
ax.set_xlabel("Machine learning model",fontsize=20)
ax.set_ylabel("Score",fontsize=20)
sns.set_style("whitegrid")
# plot_df.rename(index={0: 'Decision Tree', 1: 'Random Forest', 2: 'SVM with SMOTE'})
locs, labels = plt.xticks()
print("locs",locs)
print("labels",labels)
labels = ['Decision Tree','Random Forest','SVM with SMOTE']
ax.set_xticklabels(labels)
plot_df.plot.bar(ax=ax,width=0.5,stacked = False,fontsize = 11)
打印输出:
locs [0. 0.2 0.4 0.6 0.8 1. ]
labels <a list of 6 Text xticklabel objects>
另外,设置记号的样式:
chart = plot_df.plot.bar(ax=ax,width=0.5,stacked = False,fontsize = 11)
chart.set_xticklabels(chart.get_xticklabels(), rotation=45, horizontalalignment='right',fontsize = 16)
只需删除
设置标签
并在代码中更改:
plot_df.plot.bar(x=labels, ax=ax,width=0.5,stacked = False,fontsize = 11)
我很不幸地得到了这个结果:keyrerror:“[Index(['Decision Tree','Random Forest','SVM with SMOTE'],dtype='object')]中没有一个在[columns]中。”
plot_df.plot.bar(x=labels, ax=ax,width=0.5,stacked = False,fontsize = 11)