【发布时间】:2021-08-07 14:48:09
【问题描述】:
我使用下面的“示例数据框”(df) 和代码在 matplotlib 中制作了一个哑铃图。
结果看起来不错,但到目前为止我无法在 df["avg"] 列中用它们的平均值注释哑铃图。
有人可以指导我如何将每个观察值的平均值添加到各自的红点上方吗?非常感谢!
代码如下:
#example data
data = {'Brand': ['HC','TC','FF','AA'],
'2019Price': [22000,25000,27000,35000],
'2020Price':[25000, 30000, 29000, 39000]}
df = pd.DataFrame(data)
df["avg"] = (df['2019Price'] + df[ '2020Price'])/2
df = df.sort_values("2020Price", ascending = False)
#dumb bell plot
plt.hlines(y = df["Brand"], xmin = df["2019Price"], xmax =
df["2020Price"], color = "grey", alpha = 0.4)
plt.scatter(y = df["Brand"], x = df["2019Price"], color = "blue",
label = "2019")
plt.scatter(y = df["Brand"], x = df["2020Price"], color = "blue",
label = "2020")
plt.scatter(y = df["Brand"], x = df["avg"], color = "red", label =
"average")
plt.legend()
【问题讨论】:
标签: python pandas matplotlib seaborn annotate