预估世界人口的Seaborn注释线图

如何解决预估世界人口的Seaborn注释线图

“”“ 我正在尝试重现显示1950年至2100年世界人口增长的情节。

  1. 理想情况下,我想在线图中显示两种不同的颜色,1950年至2019年为深绿色,因为它们是实际数据,而投影数据为浅绿色(2019年至2100年)
  2. 我想注释对应于1950、1987、2019和2050的特定点。我尝试使用markers = True但失败了。

我正在寻找类似以下情节的内容(没有以红色表示的年增长率)

enter image description here

预先感谢您对我的帮助。

“”“

data = {'Year': {0: 1950,1: 1951,2: 1952,3: 1953,4: 1954,5: 1955,6: 1956,7: 1957,8: 1958,9: 1959,10: 1960,11: 1961,12: 1962,13: 1963,14: 1964,15: 1965,16: 1966,17: 1967,18: 1968,19: 1969,20: 1970,21: 1971,22: 1972,23: 1973,24: 1974,25: 1975,26: 1976,27: 1977,28: 1978,29: 1979,30: 1980,31: 1981,32: 1982,33: 1983,34: 1984,35: 1985,36: 1986,37: 1987,38: 1988,39: 1989,40: 1990,41: 1991,42: 1992,43: 1993,44: 1994,45: 1995,46: 1996,47: 1997,48: 1998,49: 1999,50: 2000,51: 2001,52: 2002,53: 2003,54: 2004,55: 2005,56: 2006,57: 2007,58: 2008,59: 2009,60: 2010,61: 2011,62: 2012,63: 2013,64: 2014,65: 2015,66: 2016,67: 2017,68: 2018,69: 2019,70: 2020,71: 2091,72: 2092,73: 2093,74: 2094,75: 2095,76: 2096,77: 2097,78: 2098,79: 2099,80: 2100},'billion': {0: 2.5,1: 2.6,2: 2.6,3: 2.7,4: 2.7,5: 2.8,6: 2.8,7: 2.9,8: 2.9,9: 3.0,10: 3.0,11: 3.1,12: 3.2,13: 3.2,14: 3.3,15: 3.3,16: 3.4,17: 3.5,18: 3.6,19: 3.6,20: 3.7,21: 3.8,22: 3.9,23: 3.9,24: 4.0,25: 4.1,26: 4.2,27: 4.2,28: 4.3,29: 4.4,30: 4.5,31: 4.5,32: 4.6,33: 4.7,34: 4.8,35: 4.9,36: 5.0,37: 5.1,38: 5.1,39: 5.2,40: 5.3,41: 5.4,42: 5.5,43: 5.6,44: 5.7,45: 5.7,46: 5.8,47: 5.9,48: 6.0,49: 6.1,50: 6.1,51: 6.2,52: 6.3,53: 6.4,54: 6.5,55: 6.5,56: 6.6,57: 6.7,58: 6.8,59: 6.9,60: 7.0,61: 7.0,62: 7.1,63: 7.2,64: 7.3,65: 7.4,66: 7.5,67: 7.5,68: 7.6,69: 7.7,70: 7.8,71: 10.8,72: 10.8,73: 10.8,74: 10.8,75: 10.9,76: 10.9,77: 10.9,78: 10.9,79: 10.9,80: 10.9}}
df = pd.DataFrame(data)
print(df)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns

fig,ax = plt.subplots(figsize=(10,8))
sns.lineplot(x='Year',y='billion',data=df,ax=ax,color='b')
ax.set_ylim([2,11])
plt.fill_between(df['Year'].values,df['billion'].values,color='lightgreen')

plt.text(1950,2.5,'2.5 Billion\nin 1950',horizontalalignment='left')
plt.text(1987,5,'5 Billion\nin 1987',horizontalalignment='right')
plt.text(2019,7.7,'7.7 Billion\nin 2019',horizontalalignment='right')
plt.text(2050,9.7,'9.7 Billion\nin 2050',horizontalalignment='right')

ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)#hiding y spine
plt.gca().axes.get_yaxis().set_visible(False) #hiding y axis
ax.spines['right'].set_visible(False)
plt.show()
plt.close()

“”“ 这就是我到目前为止

enter image description here

“”“

解决方法

您可以使用where=填充年份:

ax.fill_between(df['Year'],df['billion'],color='darkgreen',where=df['Year'] <= 2019)
ax.fill_between(df['Year'],color='lightgreen',where=df['Year'] >= 2019)

您可以使用np.interp()插入年份的值:

marked_years = [1950,1987,2019,2050]
ax.scatter(marked_years,np.interp(marked_years,df['Year'],df['billion']),marker='o',color='black',s=50)

可以类似的方式放置文本:

for year,value in zip(marked_years,df['billion'])):
    ax.text(year,value,f'{value:.1f} Billion\nin {year}\n',ha='left' if year < 1970 else 'right',va='bottom')

(可选)您每10年为x轴设置一个刻度线,而忽略填充:

ax.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax.margins(x=0,tight=True) # zero padding for the x-axis```

resulting plot

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-