[581]matplotlib(四)|Python中用matplotlib绘制盒状图(Boxplots)和小提琴图(Violinplots)

简单的盒状图

import matplotlib.pyplot as plt
import numpy as np
 
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
 
fig = plt.figure(figsize=(8,6))
 
plt.boxplot(all_data,
            notch=False, # box instead of notch shape
            sym='rs',    # red squares for outliers
            vert=True)   # vertical box aligmnent
 
plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.xlabel('measurement x')
t = plt.title('Box plot')
plt.show()

image.png

黑白盒状图

import matplotlib.pyplot as plt
import numpy as np
 
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
 
fig = plt.figure(figsize=(8,6))
 
bplot = plt.boxplot(all_data,
            notch=False, # box instead of notch shape
            sym='rs',    # red squares for outliers
            vert=True)   # vertical box aligmnent
 
plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.xlabel('measurement x')
 
for components in bplot.keys():
    for line in bplot[components]:
        line.set_color('black')     # black lines
 
t = plt.title('Black and white box plot')
 
plt.show()

image.png

水平盒状图

import matplotlib.pyplot as plt
import numpy as np
 
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
 
fig = plt.figure(figsize=(8,6))
 
plt.boxplot(all_data,
            notch=False,  # box instead of notch shape
            sym='rs',     # red squares for outliers
            vert=False)   # horizontal box aligmnent
 
plt.yticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.ylabel('measurement x')
t = plt.title('Horizontal Box plot')
plt.show()

image.png

圆柱盒状图

import matplotlib.pyplot as plt
import numpy as np
 
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
 
fig = plt.figure(figsize=(8,6))
 
plt.boxplot(all_data,
            notch=True,  # notch shape
            sym='bs',     # blue squares for outliers
            vert=True,   # vertical box aligmnent
            patch_artist=True)   # fill with color
 
plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.xlabel('measurement x')
t = plt.title('Box plot')
plt.show()

image.png

自定义颜色填充盒状图

import matplotlib.pyplot as plt
import numpy as np
 
all_data = [np.random.normal(0, std, 100) for std in range(1, 4)]
 
fig = plt.figure(figsize=(8,6))
 
bplot = plt.boxplot(all_data,
            notch=False,  # notch shape
            vert=True,   # vertical box aligmnent
            patch_artist=True)   # fill with color
 
colors = ['pink', 'lightblue', 'lightgreen']
for patch, color in zip(bplot['boxes'], colors):
    patch.set_facecolor(color)
 
plt.xticks([y+1 for y in range(len(all_data))], ['x1', 'x2', 'x3'])
plt.xlabel('measurement x')
t = plt.title('Box plot')
plt.show()

image.png

绘制小提琴图

import matplotlib.pyplot as plt
import numpy as np
 
fig, axes = plt.subplots(nrows=1,ncols=2, figsize=(12,5))
 
all_data = [np.random.normal(0, std, 100) for std in range(6, 10)]
 
#fig = plt.figure(figsize=(8,6))
 
axes[0].violinplot(all_data,
               showmeans=False,
               showmedians=True
               )
axes[0].set_title('violin plot')
 
axes[1].boxplot(all_data,
               )
axes[1].set_title('box plot')
 
# adding horizontal grid lines
for ax in axes:
    ax.yaxis.grid(True)
    ax.set_xticks([y+1 for y in range(len(all_data))], )
    ax.set_xlabel('xlabel')
    ax.set_ylabel('ylabel')
 
plt.setp(axes, xticks=[y+1 for y in range(len(all_data))],
        xticklabels=['x1', 'x2', 'x3', 'x4'],
        )
 
plt.show()

image.png

来源:http://blog.topspeedsnail.com/archives/737#more-737

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值