从数据框中绘制每个单独条形图的值

Plotting values for each individual bar plot from a dataframe

上下文:我正在绘制多个模型的准确度值,我想显示每个模型(条形图)的准确度值(来自列表结果)。考虑到我正在从最高精度到最低精度进行排序,我该怎么做?谢谢!

results = [lr_cv[1],svm_cv[1], decision_tree[1],score_log_reg_pca,nb_cv[1]] #colocar lista dos modelos feitos

names = ["Logistic Regression","SVM", "Decision Tree","Logistic Regression with PCA","Naive Bayes"] #colocar nomes para o grafico
df_new = pd.DataFrame(list(zip(names, results)), columns= ["Model","Accuracy"])
df_sorted = df_new.sort_values("Accuracy")
df_sorted.index=df_sorted.Model
plt.figure(figsize=(12,7))
ax = df_sorted.plot(kind="barh", facecolor="#AA0000",figsize=(15,10), fontsize=12)
ax.spines["bottom"].set_color("#CCCCCC")
ax.set_xlabel("Accuracy", fontsize=12)
ax.set_ylabel("Model",fontsize=12)
plt.title("Comparação de modelos para Classificação")

如果我没理解错的话,您想在每个栏的顶部添加一些文本,以显示每个模型所达到的准确度的实际值。您可以使用 ax()text() 方法(已记录 here)。我已将您的代码修改如下(使用一些虚拟值来提高准确性):

import pandas as pd
import matplotlib.pyplot as plt

results = [lr_cv[1],svm_cv[1], decision_tree[1],score_log_reg_pca,nb_cv[1]] #colocar lista dos modelos feitos


names = ["Logistic Regression","SVM", "Decision Tree","Logistic Regression with PCA","Naive Bayes"] #colocar nomes para o grafico
df_new = pd.DataFrame(list(zip(names, results)), columns= ["Model","Accuracy"])
df_sorted = df_new.sort_values("Accuracy")
df_sorted.index=df_sorted.Model
plt.figure(figsize=(12,7))
ax = df_sorted.plot(kind="barh", facecolor="#AA0000",figsize=(15,10), fontsize=12)

gap = 0.015 # Space between the text and the end of the bar
# You have to call ax.text() for each bar
# They are already sorted and you need the index of the bar
for i, v in enumerate(df_sorted.Accuracy):
    ax.text(v + gap, i, str(v), color='blue') # Place the text at x=v+gap and y= idx

ax.spines["bottom"].set_color("#CCCCCC")
ax.set_xlabel("Accuracy", fontsize=12)
ax.set_ylabel("Model",fontsize=12)
plt.title("Comparação de modelos para Classificação")

另一种选择是修改代码并注意使用pandas的plot()方法,Axes.bar_label()如下:

import pandas as pd
import matplotlib.pyplot as plt

results = [lr_cv[1],svm_cv[1], decision_tree[1],score_log_reg_pca,nb_cv[1]] #colocar lista dos modelos feitos
names = ["Logistic Regression","SVM", "Decision Tree","Logistic Regression with PCA","Naive Bayes"] #colocar nomes para o grafico
df_new = pd.DataFrame(list(zip(names, results)), columns= ["Model","Accuracy"])
df_sorted = df_new.sort_values("Accuracy")
df_sorted.index=df_sorted.Model
plt.figure(figsize=(12,7))
fig, ax = plt.subplots() # get ax handle
bars = plt.barh(df_sorted.Model, df_sorted.Accuracy) # Plot barh
ax.bar_label(bars) # Set the labels

ax.spines["bottom"].set_color("#CCCCCC")
ax.set_xlabel("Accuracy", fontsize=12)
ax.set_ylabel("Model",fontsize=12)
plt.title("Comparação de modelos para Classificação")