【问题标题】:"panel barchart" in matplotlibmatplotlib 中的“面板条形图”
【发布时间】:2015-01-16 13:04:35
【问题描述】:

我想用 matplotlib 制作一个像这样的图:


(来源:peltiertech.com

我的数据在 pandas DataFrame 中,并且我已经得到了常规堆叠条形图,但我不知道如何执行每个类别都有自己的 y 轴基线的部分。

理想情况下,我希望所有子图的垂直比例完全相同,并将面板标签移到一边,这样行之间就不会有间隙。

【问题讨论】:

  • 能否提供样本数据集

标签: matplotlib pandas bar-chart facet


【解决方案1】:

我没有完全复制你想要的,但这应该让你非常接近。

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

#create dummy data
cols = ['col'+str(i) for i in range(10)]
ind = ['ind'+str(i) for i in range(10)]
df = pd.DataFrame(np.random.normal(loc=10, scale=5, size=(10, 10)), index=ind, columns=cols)

#create plot
sns.set_style("whitegrid")
axs = df.plot(kind='bar', subplots=True, sharey=True, 
              figsize=(6, 5), legend=False, yticks=[], 
              grid=False, ylim=(0, 14), edgecolor='none', 
              fontsize=14, color=[sns.xkcd_rgb["brownish red"]])
plt.text(-1, 100, "The y-axis label", fontsize=14, rotation=90)  # add a y-label with custom positioning
sns.despine(left=True)  # get rid of the axes
for ax in axs:  # set the names beside the axes
    ax.lines[0].set_visible(False)  # remove ugly dashed line
    ax.set_title('')
    sername = ax.get_legend_handles_labels()[1][0]
    ax.text(9.8, 5, sername, fontsize=14)
plt.suptitle("My panel chart", fontsize=18)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-28
    • 1970-01-01
    • 2021-12-21
    • 2018-08-22
    • 1970-01-01
    • 2016-07-13
    • 1970-01-01
    • 2016-01-02
    相关资源
    最近更新 更多