How can I adjust the size of the whiskers in a Seaborn boxplot?

廉价感情. 提交于 2020-06-27 15:19:18

问题


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')

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?


回答1:


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

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.

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())



来源:https://stackoverflow.com/questions/55851011/how-can-i-adjust-the-size-of-the-whiskers-in-a-seaborn-boxplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!