For Loop to create boxplots with Matplotlib

孤街浪徒 提交于 2019-12-12 04:43:44

问题


I am trying to loop through a list to create a series of boxplots using Matplotlib. Each item in the list should print a plot that has 2 boxplots, 1 using df1 data and 1 using df2 data.

I am successfully plotting x1, but x2 is blank and I don't know why.

I am using jupyter notebook with Python 3. Any help is appreciated!

df1 = df[df.order == 1]
df2 = df[df.order == 0]

lst = ['device', 'ship', 'bill']

i = 0

for item in lst:
    plt.figure(i)

    x1= df1[item].values
    x2 = df2[item].values

    plt.boxplot([x1, x2])
    plt.title(item)

    i = i+1

The series that I'm trying to plot have the following format with several thousand observations each:

df[order] == 1

df['device']      df['ship']      df['bill']
     0.0              0.0            0.0
    19.0              5.0            0.0
   237.0             237.0         237.0

df[order] == 0

df['device']      df['ship']      df['bill']
     1.0              21.0           0.0
    75.0              31.0         100.0
     5.0              18.0          71.0

The dataframe contains data for orders. The columns listed in lst is made up of dtype float64


回答1:


Solved it...there were a couple of NaN values appear to have prevented me from plotting.



来源:https://stackoverflow.com/questions/47397717/for-loop-to-create-boxplots-with-matplotlib

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