我正在尝试使用 Seaborn barplot 绘制上面的数据框。
plt.figure()
sns.set_theme(context = "notebook", style = "darkgrid", palette = "deep", font = "sans-serif",
rc = {"figure.figsize": (20, 12)})
ax = sns.barplot(data = graph_df, x = "Month", y = "Contract", color = "green")
ax = sns.barplot(data = graph_df, x = "Month", y = "Definite", color = "lightgreen")
ax = sns.barplot(data = graph_df, x = "Month", y = "T1", color = "blue")
ax = sns.barplot(data = graph_df, x = "Month", y = "T2", color = "lightblue")
ax = sns.barplot(data = graph_df, x = "Month", y = "CXLD", color = "red")
ax.yaxis.set_major_formatter(tick.FuncFormatter(reformat_large_tick_values))
plt.ylabel = "Total Rent"
plt.show()
生成的图不会显示每个月的所有值。例如,看看 2 月份,它应该有 Contract = 404,600(绿色)、Definite = 94,450(浅绿色)、T1 = 726,000(蓝色)和 T2 = 121,850(浅蓝色),总计 1,346,900 美元。但是,它仅显示蓝色 (T1) 和浅蓝色 (T2) 值,并且 y 轴应该能够显示该值。
我使用 matplotlib 创建了相同的图表,这显示了图表的外观。我不使用它的唯一原因是我似乎无法将 y 轴转换为正常美元值,也无法让 y 轴标签显示“Total $'s”的值。
感谢您的帮助。
最佳答案
在 pandas/matplotlib 中创建堆叠图通常更容易。我在下面给出了代码...
colors = ['green', 'lightgreen', 'blue', 'lightblue', 'red'] ## Colors for the bars
graph_df.set_index('Month').plot(kind='bar', stacked=True, color=colors) ## Plot
plt.ticklabel_format(style='plain', useOffset=False, axis='y') ## No offset
plt.gca().set_ylabel("Total $'s") ## Set Y-axis
关于Python Seaborn堆叠条形图多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75857867/