Plotting box plots in python without grouping the data

旧巷老猫 提交于 2019-12-08 11:21:35

问题


I want to plot a box plot for a variable in a data frame xldata['yaxis_data'] according to 1,0 mapping stored in another array (one_zero_map).

I have a working code for this I am just not sure if this is the best way. Any help would be great.

Reason I am unsure is I am guessing there should be a direct way for boxplot to understand what I want if I input directly one_zero_map and xldata['yaxis_data'] without creating good_ones and bad_ones and then putting them in a list called final_list

%matplotlib inline
import matplotlib.pyplot as plt

good_ones=[val for ind, val in zip(one_zero_map,xldata['yaxis_data']) if  ind==1]
bad_ones=[val for ind, val in zip(one_zero_map,xldata['yaxis_data']) if ind==0]

final_list=[good_ones,bad_ones]

plt.boxplot(final_list)

Just to be more clear on what I am looking for, I am looking for Python equivalent of R which is like this

# Boxplot of MPG by Car Cylinders 
boxplot(mpg~cyl,data=mtcars, main="Car Milage Data", 
xlab="Number of Cylinders", ylab="Miles Per Gallon")

or phython equivalent of graphlab as

sales.show(view='BoxWhisker Plot',x='zipcode',y='price')

回答1:


You can use the boxplot method directly from pandas DataFrames. This code is equivalent to your R example:

# statsmodels only needed to get the R mtcars dataset
import statsmodels.api as sm
mtcars = sm.datasets.get_rdataset('mtcars').data

mtcars.boxplot('mpg', by='cyl')


来源:https://stackoverflow.com/questions/35365217/plotting-box-plots-in-python-without-grouping-the-data

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