我无法将中间标签正确放置在箱线图中。我想在每个箱线图的中线处放置中值。
我已经在标签盒图上进行了广泛的搜索,当色相不大于1时可以放置一个值。当色相的值大于1时,中位数放置不正确,并且图形上仅显示一个值。
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
import matplotlib.ticker as ticker
#Create box plots with median values
dims = (11.7, 8.27)
fig, ax = plt.subplots(figsize=dims)
sns.boxplot(x= "inquiryReason", y="Time(hr)", data=yr1, hue="Country",
order=['Disease-Specific','Other'])
#Calculate medians
medians = yr1.groupby(['responseClass', 'Country'])
['Time(hr)'].median().values
median_labels = [str(np.round(s, 2)) for s in medians]
#Place median values on plot
pos = range(len(medians))
for tick,label in zip(pos,ax.get_xticklabels()):
ax.text(pos[tick], medians[tick], median_labels[tick],
horizontalalignment='center', size='small', color='b',
weight='semibold')
# rotate x-axis by 20 degrees
ax.set_xticklabels(ax.get_xticklabels(), rotation=20)