gpt4 book ai didi

python-3.x - 如何在 Seaborn 箱线图中调整 mustache 的大小?

转载 作者:行者123 更新时间:2023-12-04 01:31:17 26 4
gpt4 key购买 nike

我想让下面的箱线图中的 mustache 线更宽。

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

enter image description here

我知道如何调整 mustache 颜色和线宽,但我一直无法弄清楚如何增加 mustache 的长度。我最接近的是尝试使用 line.set_xdata([q/60-0.5, q/60+0.5])但我得到了错误
ValueError: shape mismatch: objects cannot be broadcast to a single shape    

理想情况下,我希望 mustache 百分位线与盒子的宽度相同。我怎样才能做到这一点?

最佳答案

正如您所注意到的,每个框都绘制了 6 条线(因此您的 p*6 索引)。

带索引的线 p*6+4具有框的宽度(即框内的中线)。所以我们可以用它来设置其他线条的宽度。

要更改的行具有索引 p*6+2p*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())

enter image description here

这也适用于具有多个框的示例:
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())

enter image description here

关于python-3.x - 如何在 Seaborn 箱线图中调整 mustache 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55851011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com