Seaborn catplot(kind ='count')将条形图更改为饼形图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Seaborn catplot(kind ='count')将条形图更改为饼形图相关的知识,希望对你有一定的参考价值。

我在电晕跟踪应用程序上的论文(使用pd.melt)的外观如下:]

    CTQ-tool    opinion
0   Information and awareness purposes  unacceptable
1   Information and awareness purposes  unacceptable
2   Information and awareness purposes  acceptable
3   Information and awareness purposes  acceptable
4   Information and awareness purposes  unacceptable
... ... ...
2827    Central/Local data storage  NaN
2828    Central/Local data storage  NaN
2829    Central/Local data storage  NaN
2830    Central/Local data storage  NaN
2831    Central/Local data storage  NaN
2832 rows × 2 columns

我正在使用Seaborn库进行以下绘制:

代码:

g = sns.catplot("opinion", col="CTQ-tool", col_wrap=4, data=df_original_small, kind="count", height=6.5, aspect=.8)

enter image description here

但是,与其将它们显示在条形图中,不如将它们显示为饼图。 Seaborn.catplot不允许使用kind ='count-pie'。有人知道可以解决吗?

在TiTo问题之后编辑:

这基本上是我希望看到的所有8个条形图都发生的情况:

enter image description here

答案
我最终使用matplotlib库从bottem构建它:

plt.style.use('seaborn') IAP = df_original_small['Information and awareness purposes'].value_counts().to_frame().T QE = df_original_small['Quarantine Enforcement'].value_counts().to_frame().T CTCR = df_original_small['Contact Tracing and Cross-Referencing'].value_counts().to_frame().T VPID = df_original_small['Voluntary provision of infection data'].value_counts().to_frame().T QMA = df_original_small['Quarantine Monitoring App'].value_counts().to_frame().T QRCode = df_original_small['QR code provided registration tracking'].value_counts().to_frame().T total = pd.concat([IAP, QE, CTCR, VPID, QMA, QRCode]) fig, ax = plt.subplots(nrows=3, ncols=2) labels = 'acceptable', 'unacceptable' colors = ['#008fd5', '#fc4f30'] explode = (0, 0.1) explode2 = (0.2, 0) plt.title('Pie chart per CTQ-tool') plt.tight_layout() ax[0,0].pie(total.iloc[[0]], startangle=90, colors=colors, wedgeprops={'edgecolor': 'black'}, autopct='%1.f%%', explode=explode, shadow=True) ax[0,0].set_title('Information and awareness purposes', fontweight='bold') ax[0,1].pie(total.iloc[[1]], startangle=90, colors=colors, wedgeprops={'edgecolor': 'black'}, autopct='%1.f%%', explode=explode, shadow=True) ax[0,1].set_title('Quarantine Enforcement', fontweight='bold') ax[1,0].pie(total.iloc[[2]], startangle=90, colors=colors, wedgeprops={'edgecolor': 'black'}, autopct='%1.f%%', explode=explode2, shadow=True) ax[1,0].set_title('Contact Tracing and Cross-Referencing', fontweight='bold') ax[1,1].pie(total.iloc[[3]], startangle=90, colors=colors, wedgeprops={'edgecolor': 'black'}, autopct='%1.f%%', explode=explode, shadow=True) ax[1,1].set_title('Voluntary provision of infection data', fontweight='bold') ax[2,0].pie(total.iloc[[4]], startangle=90, colors=colors, wedgeprops={'edgecolor': 'black'}, autopct='%1.f%%', explode=explode2, shadow=True) ax[2,0].set_title('Quarantine Monitoring App', fontweight='bold') ax[2,1].pie(total.iloc[[5]], startangle=90, colors=colors, wedgeprops={'edgecolor': 'black'}, autopct='%1.f%%', explode=explode, shadow=True) ax[2,1].set_title('QR code provided registration tracking', fontweight='bold') fig.suptitle('Public Opinion on CTQ-measures', fontsize=20, y=1.07, fontweight='bold', x=0.37) fig.set_figheight(10) fig.set_figwidth(7) fig.legend(loc='best', labels=labels, fontsize='medium') fig.tight_layout() fig.savefig('Opinions_ctq') plt.show()

enter image description here

以上是关于Seaborn catplot(kind ='count')将条形图更改为饼形图的主要内容,如果未能解决你的问题,请参考以下文章

Seaborn 如何在 sns.catplot 中添加每个类别的样本数

seaborn使用Catplot函数可视化水平小提琴图(Make Horizontal Violin Plot with Catplot in Seaborn)

seaborn使用Catplot函数可视化分组小提琴图( Grouped Violinplot with Seaborn Catplot)并保存可视化结果

pandas数据预处理(字段筛选query函数进行数据筛选缺失值删除)seaborn使用Catplot函数可视化基本箱图(Boxplot with Seaborn Catplot)

如何将 seaborn 的 catplot 绘制到 gridspec

如何在顶部添加条形值(catplot seaborn)?