Matplotlib箱形图-使用for循环绘制分组值

时间:2019-03-15 09:48:36

标签: python-3.x pandas matplotlib

我正在尝试使用for循环绘制按唯一列值分组的箱形图,而不知道该列中唯一值的数量。

您可以在下面看到示例代码(无for循环)和所需的输出。

import pandas as pd
from numpy import random
import matplotlib.pyplot as plt

#------------------------------ Preparing the data frame -------------------------------#

df = pd.DataFrame(data = random.randn(5,4), index = ['A','B','C','D','E'],
columns = ['W','X','Y','Z'])

df['W'] = ['10/01/2018 12:00:00','10/03/2018 13:00:00',
           '10/03/2018 12:30:00','10/04/2018 12:05:00',
           '10/08/2018 12:00:15']

df['W']=pd.to_datetime(df['W'])

df['Entity'] = ['C201','C201','C201','C202','C202']
print(df)

#-----------------------------------Plotting the data ----------------------------------#

fig, ax = plt.subplots()

data_to_plot = [df['Y'][df['Entity']=='C201'], df['Y'][df['Entity']=='C202']]

# Create the boxplot
bp = ax.boxplot(data_to_plot, showfliers=False)
ax.set_xticklabels(['C201' ,'C202'])      
plt.show()

所需的输出: enter image description here

1 个答案:

答案 0 :(得分:1)

你能做这样的事吗?

#-----------------------------------Plotting the data ----------------------------------#

fig, ax = plt.subplots()

data_to_plot = []
tick_labels = []
for entity in df['Entity'].unique():
    data_to_plot.append(df['Y'][df['Entity']==entity])
    tick_labels.append(entity)

# Create the boxplot
bp = ax.boxplot(data_to_plot, showfliers=False)
ax.set_xticklabels(tick_labels)      
plt.show()