Python Seaborn堆叠条形图多列

标签 python matplotlib seaborn bar-chart

data frame trying to graph using seaborn barplot

我正在尝试使用 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 轴应该能够显示该值。

enter image description here

我使用 matplotlib 创建了相同的图表,这显示了图表的外观。我不使用它的唯一原因是我似乎无法将 y 轴转换为正常美元值,也无法让 y 轴标签显示“Total $'s”的值。

enter image description here

感谢您的帮助。

最佳答案

在 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

enter image description here

关于Python Seaborn堆叠条形图多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75857867/

相关文章:

python - 在pytorch中计算角度

python - 根据其他列值选择值

python - 使用十六进制代码着色饼图 Matplotlib.pyplot

Python int 太大而无法转换为 C long - 绘制 Pandas 日期

python - 关于 sns.barplot 及其工作原理的问题?

python - 如何根据条形图的值在 matplotlib 中创建自定义图例?

python - 使用Python的Requests库,无法调用API来正确格式化

python - 如何将二级索引更改为 pandas DataFrame 中的二级列?

python - Seaborn 导入错误 : DLL load failed: The specified module could not be found

python - Seaborn - 将 x 轴移动到顶部