在python中绘制框图而不对数据进行分组 [英] Plotting box plots in python without grouping the data
问题描述
我想根据存储在另一个数组(one_zero_map
)中的1,0映射在数据帧xldata['yaxis_data']
中绘制变量的箱形图.
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.
我不确定的原因是,如果我直接输入one_zero_map
和xldata['yaxis_data']
而不创建good_ones和bad_ones,然后将它们放入名为
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)
为了更清楚我要寻找的内容,我正在寻找与 R等效的Python 就是这样
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")
或graphlab的phython等价物
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')
这篇关于在python中绘制框图而不对数据进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!