gpt4 book ai didi

python - 曲线末尾的标签 (matplotlib-seaborn)

转载 作者:太空宇宙 更新时间:2023-11-03 21:38:48 24 4
gpt4 key购买 nike

我有多个这种格式的数据框:

year    count   cum_sum
2001 5 5
2002 15 20
2003 14 34
2004 21 55
2005 44 99
2006 37 136
2007 55 191
2008 69 260
2009 133 393
2010 94 487
2011 133 620
2012 141 761
2013 206 967
2014 243 1210
2015 336 1546
2016 278 1824
2017 285 2109
2018 178 2287

我生成了如下图: enter image description here

以下代码已用于此目的:

fig, ax = plt.subplots(figsize=(12,8))

sns.pointplot(x="year", y="cum_sum", data=china_papers_by_year_sorted, color='red')
sns.pointplot(x="year", y="cum_sum", data=usa_papers_by_year_sorted, color='blue')
sns.pointplot(x="year", y="cum_sum", data=korea_papers_by_year_sorted, color='lightblue')
sns.pointplot(x="year", y="cum_sum", data=japan_papers_by_year_sorted, color='yellow')
sns.pointplot(x="year", y="cum_sum", data=brazil_papers_by_year_sorted, color='green')

ax.set_ylim([0,2000])
ax.set_ylabel("Cumulative frequency")

fig.text(x = 0.91, y = 0.76, s = "China", color = "red", weight = "bold") #Here I have had to indicate manually x and y coordinates
fig.text(x = 0.91, y = 0.72, s = "South Korea", color = "lightblue", weight = "bold") #Here I have had to indicate manually x and y coordinates

plt.show()

问题在于向绘图添加文本的方法无法识别数据坐标。因此,我必须手动指示每个数据框标签的坐标(请参阅“中国”和“韩国”)。有聪明的方法吗?我看过一个使用“.last_valid_index()”方法的示例。但是,由于无法识别数据坐标,因此它不起作用。

最佳答案

您无需重复调用pointplot并手动添加标签。相反,在数据框中添加一个国家/地区列来指示国家/地区,组合数据框,然后使用国家/地区作为色调简单地绘制累积总和与年份的关系。

相反,请执行以下操作:

# Add a country label to dataframe itself
china_papers_by_year_sorted['country'] = 'China'
usa_papers_by_year_sorted['country'] = 'USA'
korea_papers_by_year_sorted['country'] = 'Korea'
japan_papers_by_year_sorted['country'] = 'Japan'
brazil_papers_by_year_sorted['country'] = 'Brazil'

# List of dataframes with same columns
frames = [china_papers_by_year_sorted, usa_papers_by_year_sorted,
korea_papers_by_year_sorted, japan_papers_by_year_sorted,
brazil_papers_by_year_sorted]

# Combine into one dataframe
result = pd.concat(frames)

# Plot.. hue will make country name a label
ax = sns.pointplot(x="year", y="cum_sum", hue="country", data=result)
ax.set_ylim([0,2000])
ax.set_ylabel("Cumulative frequency")
plt.show()

编辑:编辑添加,如果您想注释行本身而不是使用图例,则 this existing question 的答案指示如何注释行尾。

关于python - 曲线末尾的标签 (matplotlib-seaborn),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53051201/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com