zwo*_*wol 1 matplotlib facet bar-chart pandas
我想使用 matplotlib 生成这样的图:
(来源:peltiertech.com)
我的数据位于 pandas DataFrame 中,并且我已经得到了常规的堆叠条形图,但我不知道如何执行每个类别都有自己的 y 轴基线的部分。
理想情况下,我希望所有子图的垂直比例完全相同,并将面板标签移到一侧,以便行之间没有间隙。
我还没有完全复制你想要的东西,但这应该会让你非常接近。
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)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1524 次 |
最近记录: |