我只发现代码可以将中位数放入箱线图,然后尝试了一下。但是由于我的箱线图是多个,所以它无法获取x-tick获取定位器。我如何找到箱形图的次要刻度定位器,我已经尝试过了,但仍无法获取多个箱形图位置的位置。有什么建议可以改善这个情节吗?
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
// alert.setOnShowing(event -> alert.setResult(null));
alert.setTitle("Delete similar files?");
ButtonType okButton = new ButtonType("Yes", ButtonBar.ButtonData.YES);
ButtonType noButton = new ButtonType("No", ButtonBar.ButtonData.NO);
ButtonType cancelButton = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(okButton, noButton, cancelButton);
Optional<ButtonType> choose = alert.showAndWait();
// String text = new String("");
// if (choose.get() != null)
// text = choose.get().getText();
// if (text != null && (text.equals("YES") || text.equals("Yes")))
if (choose.get() == okButton){
答案 0 :(得分:2)
众所周知,Seaborn很难使用。下面的代码有效,但如果其中一个类别为空并且未绘制任何箱线图,则可能会中断,例如,使用后果自负:
df = pd.DataFrame([['Apple', 10, 'A'],['Apple', 8, 'B'],['Apple', 10, 'C'],
['Apple', 5, 'A'],['Apple', 7, 'B'],['Apple', 9, 'C'],
['Apple', 3, 'A'],['Apple', 5, 'B'],['Apple', 4, 'C'],
['Orange', 3, 'A'],['Orange', 4, 'B'],['Orange', 6, 'C'],
['Orange', 2, 'A'],['Orange', 8, 'B'],['Orange', 4, 'C'],
['Orange', 8, 'A'],['Orange', 10, 'B'],['Orange', 1, 'C']])
df.columns = ['item', 'score', 'grade']
width = 0.8
hue_col = 'grade'
fig, plt.figure(figsize=(6, 3), dpi=150)
ax = sns.boxplot(x='item', y='score', data=df, hue=hue_col, palette=sns.color_palette('husl'), width=width)
ax.legend(loc='lower right', bbox_to_anchor=(1.11, 0), ncol=1, fontsize = 'x-small').set_title('')
# get the offsets used by boxplot when hue-nesting is used
# https://github.com/mwaskom/seaborn/blob/c73055b2a9d9830c6fbbace07127c370389d04dd/seaborn/categorical.py#L367
n_levels = len(df[hue_col].unique())
each_width = width / n_levels
offsets = np.linspace(0, width - each_width, n_levels)
offsets -= offsets.mean()
medians = df.groupby(['item','grade'])['score'].median()
for x0,(_,med0) in enumerate(medians.groupby(level=0)):
for off,(_,med1) in zip(offsets,med0.groupby(level=1)):
ax.text(x0+off, med1.item(), '{:.0f}'.format(med1.item()),
horizontalalignment='center', va='center', size='xx-small', color='w', weight='semibold', bbox=dict(facecolor='#445A64'))
通常,为避免发生任何意外情况,如果要修改海洋图,建议您指定order
和hue_order
,以便以预定顺序绘制图。这是另一个能够处理缺失类别的版本:
df = pd.DataFrame([['Apple', 8, 'B'],['Apple', 10, 'C'],
['Apple', 7, 'B'],['Apple', 9, 'C'],
['Apple', 5, 'B'],['Apple', 4, 'C'],
['Orange', 3, 'A'],['Orange', 6, 'C'],
['Orange', 2, 'A'],['Orange', 4, 'C'],
['Orange', 8, 'A'],['Orange', 1, 'C']])
df.columns = ['item', 'score', 'grade']
order = ['Apple', 'Orange']
hue_col = 'grade'
hue_order = ['A','B','C']
width = 0.8
fig, plt.figure(figsize=(6, 3), dpi=150)
ax = sns.boxplot(x='item', y='score', data=df, hue=hue_col, palette=sns.color_palette('husl'), width=width,
order=order, hue_order=hue_order)
ax.legend(loc='lower right', bbox_to_anchor=(1.11, 0), ncol=1, fontsize = 'x-small').set_title('')
# get the offsets used by boxplot when hue-nesting is used
# https://github.com/mwaskom/seaborn/blob/c73055b2a9d9830c6fbbace07127c370389d04dd/seaborn/categorical.py#L367
n_levels = len(df[hue_col].unique())
each_width = width / n_levels
offsets = np.linspace(0, width - each_width, n_levels)
offsets -= offsets.mean()
medians = df.groupby(['item','grade'])['score'].median()
medians = medians.reindex(pd.MultiIndex.from_product([order,hue_order]))
for x0,(_,med0) in enumerate(medians.groupby(level=0)):
for off,(_,med1) in zip(offsets,med0.groupby(level=1)):
if not np.isnan(med1.item()):
ax.text(x0+off, med1.item(), '{:.0f}'.format(med1.item()),
horizontalalignment='center', va='center', size='xx-small', color='w', weight='semibold', bbox=dict(facecolor='#445A64'))