boxplot用法 python,[Python画图笔记]利用Python画箱型图boxplot

[Python画图笔记]利用Python画箱型图boxplot

[Python画图笔记]利用Python画箱型图boxplot

最近在学习使用Python画图,想用subplot画两幅箱型图,分别用来表示三组数据在空间滤波前后WRMS值的变化情况。关于Pycharm的安装和配置可以自行百度,直接贴上代码,给出效果图。代码中给出标注以便日后查询。

import pandas as pd #导入pandas

import matplotlib.pyplot as plt

import numpy as np

from scipy import interpolate

#fig,axes = plt.subplots(1,2,figsize=(8,4))

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2,sharex=True, figsize=(6, 3))

n_wrms_before_filtering=[]

e_wrms_before_filtering=[]

u_wrms_before_filtering=[]

n_wrms_after_filtering=[]

e_wrms_after_filtering=[]

u_wrms_after_filtering=[]

lines = open("D:\软件安装\SoftwareFile\Pycharm\PycharmProjects\\boxplot\data\wrms_neu1.dat", 'r').readlines()

for i in range(len(lines)):

# split data

fields = lines[i].split(' ')

n_wrms_before_filtering.append(float(fields[0]))

e_wrms_before_filtering.append(float(fields[1]))

u_wrms_before_filtering.append(float(fields[2]))

lines = open("D:\软件安装\SoftwareFile\Pycharm\PycharmProjects\\boxplot\data\wrms_neu2.dat", 'r').readlines()

for i in range(len(lines)):

# split data

fields = lines[i].split(' ')

n_wrms_after_filtering.append(float(fields[0]))

e_wrms_after_filtering.append(float(fields[1]))

u_wrms_after_filtering.append(float(fields[2]))

labels = 'N','E','U' #图例

p1=ax1.boxplot([n_wrms_before_filtering, e_wrms_before_filtering, u_wrms_before_filtering],widths = 0.8,labels = labels,patch_artist = True)

color = ['#515151', '#f14040', '#1a6fdf'] # 有多少box就对应设置多少颜色

for box, c in zip(p1['boxes'], color):

# 箱体边框颜色

box.set(color=c, linewidth=1.5)

# 箱体内部填充颜色

box.set(facecolor=c)

# 这里设置的是各个box的其他属性

for whisker in p1['whiskers']:

whisker.set(color='#180405', linewidth=1.5)

for cap in p1['caps']:

cap.set(color='#180405', linewidth=1.5)

for median in p1['medians']:

median.set(color='#180405', linewidth=1.5)

for flier in p1['fliers']:

flier.set(marker='o', color='y', alpha=0.5)

labels = 'N','E','U' #图例

p2=ax2.boxplot([n_wrms_after_filtering, e_wrms_after_filtering, u_wrms_after_filtering],widths = 0.8,labels = labels,patch_artist = True)

color = ['#515151', '#f14040', '#1a6fdf'] # 有多少box就对应设置多少颜色

for box, c in zip(p2['boxes'], color):

# 箱体边框颜色

box.set(color=c, linewidth=1.5)

# 箱体内部填充颜色

box.set(facecolor=c)

# 这里设置的是各个box的其他属性

for whisker in p2['whiskers']:

whisker.set(color='#180405', linewidth=1.5)

for cap in p2['caps']:

cap.set(color='#180405', linewidth=1.5)

for median in p2['medians']:

median.set(color='#180405', linewidth=1.5)

for flier in p2['fliers']:

flier.set(marker='o', color='y', alpha=0.5)

ax1.set(xlabel='Directions', ylabel='WRMS/mm')

ax2.set(xlabel='Directions', ylabel='WRMS/mm')

ax1.set_title('Before filtering')

ax2.set_title('After filtering')

plt.tight_layout()

#plt.subplots_adjust(left=0.129, bottom=0.11, right=0.9, top=0.88,wspace=0.2, hspace=0.2)

#plt.show()

plt.savefig('D:\软件安装\SoftwareFile\Pycharm\PycharmProjects\\boxplot\\123', dpi=600)

效果图:

b68a115cc61a9a183f917fb1c81feb74.png

本次绘图参考的博客有:

Matplotlib - 箱线图、箱型图 boxplot () 所有用法详解

在一幅图中为箱线图设置不同颜色

matplotlib官方手册

注:本次绘图中的颜色搭配来自于:学术文章绘图常用颜色搭配(附RGB值)

由于python画图设置颜色的时候用到的为16进制的表示方式,所以我还借鉴了如何把rgb颜色值转换成16进制??所说的,在这里ColorHaxa完成了颜色表达方式的转化。

[Python画图笔记]利用Python画箱型图boxplot相关教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要美化 Python 中的箱线图(boxplot),你可以使用 matplotlib 库来进行定制化。下面是一些常见的美化技巧: 1. 修改颜色:使用 `color` 参数来指定箱线图的颜色。例如,`color='red'` 将箱线图的颜色设置为红色。 2. 更改线型:通过 `linestyle` 参数来设置线的样式,如实线(`'-'`)、虚线(`'--'`)、点划线(`':'`)等。 3. 调整线宽:使用 `linewidth` 参数来调整线的宽度。例如,`linewidth=2` 将线的宽度设置为2个单位。 4. 添加标题和标签:使用 `title` 参数来添加标题,使用 `xlabel` 和 `ylabel` 参数来添加 x 轴和 y 轴标签。 5. 自定义坐标轴范围:使用 `ylim` 和 `xlim` 参数来设置 y 轴和 x 轴的范围。 6. 隐藏坐标轴:通过 `ax.spines['top'].set_visible(False)`、`ax.spines['right'].set_visible(False)`、`ax.spines['bottom'].set_visible(False)`、`ax.spines['left'].set_visible(False)` 来隐藏不需要显示的坐标轴。 下面是一个例子,展示如何美化一个箱线图: ```python import matplotlib.pyplot as plt # 创建一个箱线图 data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] plt.boxplot(data) # 设置颜色 plt.boxplot(data, color='red') # 设置线型和线宽 plt.boxplot(data, linestyle='--', linewidth=2) # 添加标题和标签 plt.title('Boxplot Example') plt.xlabel('X Axis') plt.ylabel('Y Axis') # 自定义坐标轴范围 plt.ylim(0, 12) plt.xlim(0.5, 1.5) # 隐藏坐标轴 ax = plt.gca() ax.spines['top'].set_visible(False) ax.spines['right'].set_visible(False) ax.spines['bottom'].set_visible(False) ax.spines['left'].set_visible(False) # 显示箱线图 plt.show() ``` 你可以根据需要使用这些技巧来美化你的箱线图。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值