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')
解决方案
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')