我想在下面的方框图中加宽晶须线。
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])
,但出现错误
ValueError: shape mismatch: objects cannot be broadcast to a single shape
理想情况下,我希望晶须百分比线的宽度与盒子的宽度相同。我该怎么办?
答案 0 :(得分:3)
您已经注意到,每个框有6条线(因此,您的p*6
索引)。
索引为p*6+4
的行具有框的宽度(这是框内的中线)。因此,我们可以使用它来设置其他线条的宽度。
要更改的行具有索引p*6+2
和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())
这也适用于带有多个框的示例:
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())