[英]Annotate Min/Max/Median in Matplotlib Violin Plot
Given this example code:鉴于此示例代码:
import pandas as pd
import matplotlib.pyplot as plt
data = 'https://raw.githubusercontent.com/marsja/jupyter/master/flanks.csv'
df = pd.read_csv(data, index_col=0)
# Subsetting using Pandas query():
congruent = df.query('TrialType == "congruent"')['RT']
incongruent = df.query('TrialType == "incongruent"')['RT']
# Combine data
plot_data = list([incongruent, congruent])
fig, ax = plt.subplots()
xticklabels = ['Incongruent', 'Congruent']
ax.set_xticks([1, 2])
ax.set_xticklabels(xticklabels)
ax.violinplot(plot_data, showmedians=True)
Which results in the following plot:结果如下 plot:
How can I annotate the min, max, and mean lines with their respective values?如何用各自的值注释最小值、最大值和平均值线?
I haven't been able to find examples online that allude to how to annotate violin plots in this way.我无法在网上找到暗示如何以这种方式注释小提琴图的示例。 If we set
plot = ax.violinplot(plot_data, showmedians=True)
then we can access attributes like plot['cmaxes']
but I cant quite figure out how to use that for annotations.如果我们设置
plot = ax.violinplot(plot_data, showmedians=True)
那么我们可以访问plot['cmaxes']
类的属性,但我不太清楚如何将其用于注释。
Here is an example of what I am trying to achieve:这是我要实现的目标的示例:
So this was as easy as getting the medians/mins/maxes and then enumerating, adding the annotation with plt.text
, and adding some small values for positioning:所以这就像获取中位数/最小值/最大值然后枚举一样简单,添加带有
plt.text
的注释,并添加一些用于定位的小值:
medians = results_df.groupby(['model_cat'])['test_f1'].median()
for i, v in enumerate(medians):
plt.text((i+.85), (v+.001), str(round(v, 3)), fontsize = 12)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.