无法更改matplotlib中的x记号

2024-05-29 08:31:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力更改以更改此绘图中的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("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>

编辑 解决方案:

最好的方法就是更改我在绘图中使用的数据帧:

plot_df = plot_df.set_index([pd.Index(['Decision Tree','Random Forest','SVM with SMOTE'])])
plot_df

另外,设置记号的样式:

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)

enter image description here


Tags: treedflabelsplotwithrandomaxset

热门问题