在 matplotlib 中绘制连续箱线图(对照组和治疗组) [英] Plot sequential box plots in matplotlib (control and treatment groups)

查看:58
本文介绍了在 matplotlib 中绘制连续箱线图(对照组和治疗组)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在连续的时间获得了对照组和治疗组的测量值,我想每次沿x轴按时间绘制测量值的箱形图.

I have measurements from control and treatment groups at sequential times, and I would like to plot the box plots of the measurements at each time with the times in order along the x-axis.

我该怎么做?看起来有多个箱形图彼此相邻的示例,但是根据某个时间变量对它们进行组织却让我望而却步.

How do I do this? It looks like there are examples out there of multiple box plots next to each other, but having them organized according to some time variable is eluding me.

我将以整洁"的形式给出一些示例数据.数据框.X 为测量值,T 为时间,G 为组.

I'll give some example data in a "tidy" data frame. X is the measurement, T is the time, and G is the group.

X | T | G
==========
1 | 1 | 0
2 | 1 | 1
3 | 1 | 0
2 | 1 | 1
3 | 2 | 0
7 | 2 | 1
6 | 2 | 0
3 | 2 | 1
9 | 3 | 0
5 | 3 | 1
1 | 3 | 0
1 | 3 | 1

此示例在时间1,时间2和时间3会有两个彼此相邻的箱形图.

This example would have two box plots next to each other at time 1, time 2, and time 3.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(2020)
df = pd.DataFrame({
     "X": [1,2,3,2,3,7,6,3,9,5,1,1],
     "T": [1,1,1,1,2,2,2,2,3,3,3,3],    
     "G": [0,1,0,1,0,1,0,1,0,1,0,1]
})
for i in range(1,4):
    for j in range(0,2):
        plt.boxplot(df["X"][(df["T"] == i) & (df["G"] == j)])
plt.show()

这种堆叠在一起不是我想要的.我正在寻找更类似于以下内容的内容.

This stacking on top of each other is not what I want. I'm looking for something more like the following.

推荐答案

您可以做的是利用 by ='列名'参数来指定您希望将数据分组的列.此外,通过传递 column = [column_1,column_2] 参数,您可以指定要针对'T'变量进行评估的列.下面的代码为每列(X 和 G)创建了 2 个箱线图可视化.在这两种情况下,您的数据均按所需的列"T"分组.

What you can do is to utilize the by = 'column name' argument to specify by which column you wish to group your data. In addition, passing the column = [column_1, column_2] argument allows you to specify which columns you wish to evaluate against you 'T' variable. The code below creates 2 box plot visualizations for each column (X and G). In both cases, your data is grouped by your desired column 'T'.

# Create boxplots for columns X and G, each grouped by column T
df.boxplot(column = ['X', 'G'], # specify columns you wish to analyze
           by = 'T',            # specify column by which you wish to group data
           vert = False,        # specify whethere you want vertical or horizontal output
           figsize = (16, 8))   # specify the size of your output

# Show the result
plt.show()

以上代码的输出如下:

这篇关于在 matplotlib 中绘制连续箱线图(对照组和治疗组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆