为pandas数据框Boxplot()设置y轴刻度,是否有3个偏差? [英] Set y-axis scale for pandas Dataframe Boxplot(), 3 Deviations?

查看:96
本文介绍了为pandas数据框Boxplot()设置y轴刻度,是否有3个偏差?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图每月制作一个箱形图区域,每个箱形图都按行业分组(并标记),然后让Y轴使用我指定的比例.

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. 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.

这里是一些虚拟数据的链接: https://drive.google.com/open?id=0B4xdnV0LFZI1MmlFcTBweW82V0k

Here's a link to some dummy data: https://drive.google.com/open?id=0B4xdnV0LFZI1MmlFcTBweW82V0k

对于我正在使用Python 3.5的代码:

And for the code I'm using 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.

这篇关于为pandas数据框Boxplot()设置y轴刻度,是否有3个偏差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆