Boxplot

本文通过使用Python的matplotlib和seaborn库,详细展示了如何创建和自定义箱形图,以及如何处理和可视化分类数据。从基本的箱形图绘制到高级的自定义颜色设置,再到利用FacetGrids进行复杂的数据展示,本文提供了丰富的实例和代码片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In [123]: import numpy as np

               import matplotlib.pyplot as plt

In [124]: data = np.random.randn(100)

In [125]: plt.boxplot(data) #The red bar is the median of the distribution.

               plt.show()

https://i-blog.csdnimg.cn/blog_migrate/1014b0fd34b81f9923a4dd6a8a350a85.png

In [126]: import numpy as np

               import matplotlib.pyplot as plt

In [127]: data = np.random.randn(100,5)

In [128]: plt.boxplot(data)

               plt.show()

https://i-blog.csdnimg.cn/blog_migrate/72d160e7024716214b260d871ec3bd76.png

5 Using custom colors for boxplots

In [38]: import numpy as np

             import matplotlib.pyplot as plt

In [47]: values = np.random.randn(100)

             b = plt.boxplot(values)      #Plotting functions returns a dictionary

             #The key of the dictionary is the name of the graphical elements.

            #such elements will be medians, fliers, whiskers, boxes, and caps.

 #we iterate every graphic primitive that is a part of the boxplot and set its color to black.

            for name, line_list in b.items():                   #b={name:key}

                   for line in line_list:

                          line.set_color('k') #Making a boxplot appear totally black

            plt.show()

Facet Grids分面网格 and Categorical Data类型数据

import seaborn as sns

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

tips = pd.read_csv('../examples/tips.csv')

tips.head()

tips['tip_pct'] = tips['tip'] / (tips['total_bill'] - tips['tip'])

tips.head()

                                         #categorical data                        

sns.factorplot(x='day', y='tip_pct', hue='time', col='smoker', kind='bar', data=tips[tips.tip_pct <1])

 

plt.show()

                                         #categorical data                        

sns.factorplot(x='day', y='tip_pct', row='time', col='smoker', kind='bar', data=tips[tips.tip_pct <1])

 

plt.show()

 

sns.factorplot(x='tip_pct', y='day', kind='box', data=tips[tips.tip_pct<0.5])

plt.show()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LIQING LIN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值