我有 2 个独立的数据框,每个数据框都包含列标题为“W”和“L”的数据。我正在尝试使用列标题作为 x 轴上的标签将它们绘制为条形图。
我希望将“W”映射到代表数据框中列名“W”的栏,将“L”映射到代表数据框中列名“L”的栏。
我的输出和代码如下。
inducted_df_mean = inducted_df[['W', 'L']].mean()
not_inducted_df_mean = not_inducted_df[['W', 'L']].mean()
bar_width = 0.35
bar1 = plt.bar(index, not_inducted_df_mean, bar_width, color='r', label='Not Inducted')
bar2 = plt.bar(index + bar_width, inducted_df_mean, bar_width, color='g',label='Inducted')
plt.title('Average Wins and Losses for MLB Hall of Fame Pitchers')
plt.ylabel('Number of Wins/Losses')
plt.xlabel('Wins/Loses')
plt.legend()
请您参考如下方法:
尝试像这样设置每个条形图的 tick_label 属性:
bar1 = plt.bar(index, not_inducted_df_mean, bar_width, color='r', label='Not Inducted', tick_label=['W','L'])
或者尝试像这样设置 plt.xticks():
plt.xticks( [0.0, 0.3, 1.0, 1.3], ('W', 'L', 'W', 'L') )
