Plot sequential box plots in matplotlib (control and treatment groups)

泄露秘密 提交于 2021-01-28 05:12:58

问题


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.

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

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.


回答1:


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()

The output of the above code will be as follows:



来源:https://stackoverflow.com/questions/63512119/plot-sequential-box-plots-in-matplotlib-control-and-treatment-groups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!