简体   繁体   English

为 Pandas Dataframe Boxplot() 设置 y 轴比例,3 个偏差?

[英]Set y-axis scale for pandas Dataframe Boxplot(), 3 Deviations?

I'm trying to make a single boxplot chart area per month with different boxplots grouped by (and labeled) by industry and then have the Y-axis use a scale I dictate.我正在尝试每月制作一个单独的箱线图区域,其中包含按行业分组(并标记)的不同箱线图,然后让 Y 轴使用我指定的比例。

In a perfect world this would be dynamic and I could set the axis to be a certain number of standard deviations from the overall mean.在完美的世界中,这将是动态的,我可以将轴设置为与总体平均值相差一定数量的标准差。 I could live with another type of dynamically setting the y axis but I would want it to be standard on all the 'monthly' grouped boxplots created.我可以接受另一种类型的动态设置 y 轴,但我希望它成为所有创建的“每月”分组箱线图的标准。 I don't know what the best way to handle this is yet and open to wisdom - all I know is the numbers being used now are way to large for the charts to be meaningful.我不知道处理这个问题的最佳方法是什么并且对智慧持开放态度 - 我所知道的是现在使用的数字太大了,图表才有意义。

I've tried all kinds of code and had zero luck with the scaling of axis and the code below was as close as I could come to the graph.我已经尝试了各种代码,并且在轴的缩放方面运气为零,下面的代码与我可以得出的图形非常接近。

Here's a link to some dummy data: https://drive.google.com/open?id=0B4xdnV0LFZI1MmlFcTBweW82V0k这是一些虚拟数据的链接: https : //drive.google.com/open?id=0B4xdnV0LFZI1MmlFcTBweW82V0k

And for the code I'm using Python 3.5:对于我使用 Python 3.5 的代码:

import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
import pylab    
df =  pd.read_csv('Query_Final_2.csv')
df['Ship_Date'] = pd.to_datetime(df['Ship_Date'], errors = 'coerce')
df1 = (df.groupby('Industry'))
print(
df1.boxplot(column='Gross_Margin',layout=(1,9), figsize=(20,10), whis=[5,95])
,pylab.show()
)

Here is a cleaned up version of your code with the solution:这是带有解决方案的代码的清理版本:

import pandas as pd
import matplotlib.pyplot as plt

df =  pd.read_csv('Query_Final_2.csv')
df['Ship_Date'] = pd.to_datetime(df['Ship_Date'], errors = 'coerce')
df1 = df.groupby('Industry')

axes = df1.boxplot(column='Gross_Margin',layout=(1,9), figsize=(20,10),
                   whis=[5,95], return_type='axes')
for ax in axes.values():
    ax.set_ylim(-2.5, 2.5)

plt.show()

The key is to return the subplots as axes objects and set the limits individually.关键是将子图作为轴对象返回并单独设置限制。

Once you have established variables for the mean and the standard deviation, use:为均值和标准差建立变量后,请使用:

plt.ylim(ymin, ymax)

to set the y-axis.设置y轴。

Thanks @Padraig, Please notice if you are using plt as a figure without subplot, you can use:谢谢@Padraig,请注意,如果您使用 plt 作为没有子图的图形,您可以使用:

plt.ylim(ymin, ymax)

But if you want to adjust Y-axis of one sub plot this one works (@AlexG)但是,如果您想调整一个子图的 Y 轴,则此方法有效(@AlexG)

ax.set_ylim(ymin, ymax)

for instance if your subplot is ax2, and you want to have Y-axis from 0.5 to 1.0 your code will be like this:例如,如果您的子图是 ax2,并且您希望 Y 轴从 0.5 到 1.0,您的代码将如下所示:

ax2.set_ylim(0.5, 1.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM