boxplot箱型图一些参数及代码

箱形图(Box-plot)又称为盒须图、盒式图或箱线图,是一种用作显示一组数据分散情况资料的统计图。它能显示出一组数据的最大值最小值中位数上下四分位数。因形状如箱子而得名。在各种领域也经常被使用,常见于品质管理。

中位数变粗: medianprops={'linewidth': 4}

箱型宽度: widths=0.15

上下边缘隐藏: showcaps=False

白点变黑点: flierprops={'marker': 'o', 'markerfacecolor': 'black'}

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

df = pd.read_csv(r'/content/sample_data/Example_Distributions.csv')

plt.figure(figsize=(10, 5))

s = "China-owned\r\nOwnership"

new = s.replace("\r\n","\n")

df['group'] = df['group'].replace('China-owned\r\nOwnership', 'China-owned\nOwnership')

new_order=[new,'HK-owned', 'Foreign-owned']

colors = {

    'Written English': 'mediumblue',

    'Spoken English': 'c',

    'Cantonese': 'steelblue',

    'Written Chinese': 'palegreen',

    'Putonghua': 'palegoldenrod'}

palette = {key: value for key, value in zip(df['class'].unique(), colors.values())}

sns.boxplot(x='group', y='value',order=new_order, hue='class',hue_order=['Written English', 'Spoken English', 'Cantonese','Written Chinese', 'Putonghua'],

      zorder=1, data=df, palette=palette, medianprops={'linewidth': 4}, flierprops={'marker': 'o', 'markerfacecolor': 'black'}, widths=0.15, showcaps=False)

plt.xlabel('Company Ownership')

plt.ylabel('Perceived importance (Range:1-7)')

plt.ylim(1, 7)

plt.axhline(y=1, color='lightgrey', zorder=0)

plt.axhline(y=2, color='lightgrey', zorder=0)

plt.axhline(y=3, color='lightgrey', zorder=0)

plt.axhline(y=4, color='lightgrey', zorder=0)

plt.axhline(y=5, color='lightgrey', zorder=0)

plt.axhline(y=6, color='lightgrey', zorder=0)

plt.axhline(y=7, color='lightgrey', zorder=0)

plt.legend(title='Class')

plt.legend(loc='center', bbox_to_anchor=(0.5, 1.03), ncol=5, frameon=False)  

plt.subplots_adjust(bottom=0)

plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值