如何调整Seaborn箱图中的晶须尺寸? [英] How can I adjust the size of the whiskers in a Seaborn boxplot?

查看:147
本文介绍了如何调整Seaborn箱图中的晶须尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在以下方框图中加宽晶须线。

I'd like to make the whisker lines wider in the following boxplot.

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

data = pd.DataFrame({'Data': np.random.random(100), 'Type':['Category']*100})

fig, ax = plt.subplots()

# Plot boxplot setting the whiskers to the 5th and 95th percentiles
sns.boxplot(x='Type', y='Data', data=data, color = 'gray', whis = [5,95])

# Adjust boxplot and whisker line properties
for p, artist in enumerate(ax.artists):
    artist.set_edgecolor('blue')
    for q in range(p*6, p*6+6):
        line = ax.lines[q]
        line.set_color('pink')

我知道如何调整晶须的颜色和线宽,但是我还无法弄清楚如何增加晶须的长度。我最接近的尝试使用 line.set_xdata([q / 60-0.5,q / 60 + 0.5])但出现错误

I know how to adjust the whisker color and linewidth, but I haven't been able to figure out how to increase the length of the whiskers. The closest I've come is trying to use line.set_xdata([q/60-0.5, q/60+0.5]) but I get the error

ValueError: shape mismatch: objects cannot be broadcast to a single shape    

理想情况下,我希望晶须百分比线的宽度与盒子的宽度相同。我该怎么做?

Ideally, I'd like to have the whisker percentile lines be the same width as the box. How can I do this?

推荐答案

您已经注意到,每个框都有6条线(因此您的 p * 6 索引)。

As you have noticed, there are 6 lines plotted for each box (hence your p*6 indexing).

索引为 p * 6 + 4 的行具有框的宽度(即框)。因此,我们可以使用它来设置其他线条的宽度。

The line with index p*6+4 has the width of the box (that's the median line inside the box). So we can use that to set the widths of the other lines.

要更改的行的索引为 p * 6 + 2 p * 6 + 3

The lines you want to change have the index p*6+2 and p*6+3.

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

data = pd.DataFrame({'Data': np.random.random(100), 'Type':['Category']*100})

fig, ax = plt.subplots()

# Plot boxplot setting the whiskers to the 5th and 95th percentiles
sns.boxplot(x='Type', y='Data', data=data, color = 'gray', whis = [5,95])

# Adjust boxplot and whisker line properties
for p, artist in enumerate(ax.artists):
    artist.set_edgecolor('blue')
    for q in range(p*6, p*6+6):
        line = ax.lines[q]
        line.set_color('pink')

    ax.lines[p*6+2].set_xdata(ax.lines[p*6+4].get_xdata())
    ax.lines[p*6+3].set_xdata(ax.lines[p*6+4].get_xdata())

这也适用于具有多个框的示例:

This also works with an example with multiple boxes:

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

sns.set(style="whitegrid")
tips = sns.load_dataset("tips")
ax = sns.boxplot(x="day", y="total_bill", data=tips)

# Adjust boxplot and whisker line properties
for p, artist in enumerate(ax.artists):
    artist.set_edgecolor('blue')
    for q in range(p*6, p*6+6):
        line = ax.lines[q]
        line.set_color('pink')

    ax.lines[p*6+2].set_xdata(ax.lines[p*6+4].get_xdata())
    ax.lines[p*6+3].set_xdata(ax.lines[p*6+4].get_xdata())

这篇关于如何调整Seaborn箱图中的晶须尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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