简体   繁体   中英

Create multile boxplots from nested list in one plot

I have a nested list like this:

nested_list = [[1,2,3], [8,9,1,3], [4,5,6,1]]

Now I want to make a boxplot for each list in the list, but the boxplots should be in the same graphic. If possible, without a dataframe because of the nans.

import matplotlib.pyplot as plt

nested_list = [[1,2,3], [8,9,1,3], [4,5,6,1]]

fig,ax = plt.subplots()

for i,lst in enumerate(nested_list): 
    ax.boxplot(lst,positions=[i]) 

plt.show()

Result:

在此处输入图像描述

plt.boxplot can take a list of vectors and positions, so its a one-liner:

import matplotlib.pyplot as plt
nested_list = [[1,2,3], [8,9,1,3], [4,5,6,1]]

plt.boxplot(nested_list, positions=range(len(nested_list)))
plt.show()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM