pandas groupby箱形图的样式 [英] Styling of Pandas groupby boxplots

查看:126
本文介绍了 pandas groupby箱形图的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python中的普通matplotlib boxplot命令返回一个字典,其中包含框,中位数,晶须,飞行物和上限的键.这使得样式真的很容易.

The normal matplotlib boxplot command in Python returns a dictionary with keys for the boxes, median, whiskers, fliers, and caps. This makes styling really easy.

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Create a dataframe and subset it for a boxplot
df1 = pd.DataFrame(rand(10), columns=['Col1'] )
df1['X'] = pd.Series(['A','B','A','B','A','B','A','B','A','B'])
boxes= [df1[df1['X'] == 'A'].Col1, df1[df1['X'] == 'B'].Col1]

# Call the standard matplotlib boxplot function,
# which returns a dictionary including the parts of the graph
mbp = plt.boxplot(boxes)
print(type(mbp))

# This dictionary output makes styling the boxplot easy
plt.setp(mbp['boxes'], color='blue')
plt.setp(mbp['medians'], color='red')
plt.setp(mbp['whiskers'], color='blue')
plt.setp(mbp['fliers'], color='blue')

Pandas库对其分组的(分层索引的)数据帧具有优化的"箱线图功能.但是,它不会返回每个组的多个词典,而是返回一个matplotlib.axes.AxesSubplot对象.这使得样式非常困难.

The Pandas library has an "optimized" boxplot function for its grouped (hierarchically indexed ) dataframes. Instead of returning several dictionaries for each group, however, it returns an matplotlib.axes.AxesSubplot object. This makes styling very difficult.

# Pandas has a built-in boxplot function that returns
# a matplotlib.axes.AxesSubplot object
pbp = df1.boxplot(by='X')
print(type(pbp))

# Similar attempts at styling obviously return TypeErrors
plt.setp(pbp['boxes'], color='blue')
plt.setp(pbp['medians'], color='red')
plt.setp(pbp['whiskers'], color='blue')
plt.setp(pbp['fliers'], color='blue')

由熊猫df.boxplot(by ='X')函数产生的AxisSubplot对象可以访问吗?

Is this AxisSubplot object produced by the pandas df.boxplot(by='X') function accessible?

推荐答案

恐怕您必须进行硬编码.以pandas为例: http://pandas.pydata .org/pandas-docs/stable/visualization.html#box-plotting

I am afraid you have to hard code. Take the pandas example: http://pandas.pydata.org/pandas-docs/stable/visualization.html#box-plotting

from pandas import *
import matplotlib
from numpy.random import rand
import matplotlib.pyplot as plt
df = DataFrame(rand(10,2), columns=['Col1', 'Col2'] )
df['X'] = Series(['A','A','A','A','A','B','B','B','B','B'])
bp = df.boxplot(by='X')
cl=bp[0].get_children()
cl=[item for item in cl if isinstance(item, matplotlib.lines.Line2D)]

现在,让我们确定框,中位数是哪个,等等:

Now lets identify which one is the boxes, median's, etc:

for i, item in enumerate(cl):
    if item.get_xdata().mean()>0:
        bp[0].text(item.get_xdata().mean(), item.get_ydata().mean(), str(i), va='center', ha='center')

情节看起来像这样:

每个栏包含8个项目.例如,第五项是中位数.第七和第八项可能是传单,我们在这里没有.

Each bar consists of 8 items. e.g, The 5th item is the median. The 7th and 8th items are probably the fliers, which we don't have any here.

了解这些内容后,修改条形图的某些部分很容易.如果我们要将中位数设置为2的linewidth

Knowing these, to modify some part of the bar is easy. If we want to set the median to have linewidth of 2:

for i in range(_your_number_of_classes_2_in_this_case):
    cl[5+i*8].set_linewidth(2.)

这篇关于 pandas groupby箱形图的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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