【论文复刻】堆叠柱状图+饼图

复刻了一下这篇论文里的fig2c:Impacts of COVID-19 and fiscal stimuli on global emissions and the Paris Agreement | Nature Climate Change

效果图:

主要步骤:

1. 数据准备:随机赋值

2. 图像绘制:绘制堆叠柱状图+饼状图

详细代码:着急的直接拖到最后有完整代码

步骤一:导入库包及图片存储路径并设置中文字体为宋体,西文为新罗马(没有的库包要先下好奥)

# 导入库及文件
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
# 设置西文字体为新罗马字体,中文宋体
config = {
            "font.family": 'serif',
            "font.size": 12,
            "mathtext.fontset": 'stix',
            "font.serif": ['SimSun'],
         }
rcParams.update(config)
rcParams['axes.unicode_minus']=False

步骤二:赋值数据

############################################################
figpath = r'H:/00.csdn/02fig/'
# 赋值数值
a = np.array([[40, 57, 84, 32, 45, 45],
              [41, 75, 65, 84, 52, 63], 
              [46, 63, 45, 63, 24, 74], 
              [16, 42, 35, 45, 52, 63], 
              [14, 45, 46, 63, 45, 63]])
x = np.arange(1, 6, 1)

month = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN',]
year = ['2021', '2022', '2023', '2024', '2025']

colors = plt.get_cmap('tab20')(range(6))

步骤三:绘制堆叠柱状图


###############################################################################
width = 0.6
fig1 = plt.figure(figsize=(15, 8))

# 绘制堆叠柱状图
ax1 = fig1.add_axes([0.1, 0.25, 0.6, 0.4]) # 设置柱状图位置
sum0 = 0
for i in range(0, len(month), 1):
    ax1.bar(x, a[:, i], width, align='center', label=month[i], color=colors[i], bottom=sum0)
    sum0 += a[:, i]

步骤四:设置图中信息及图例

# 设置图中信息
# xlim,ylim:设置x轴y轴显示得最大最小值
# xticks,yticks: 设置坐标轴刻度,可控制隔几个标注,本图为两个一标
# xticklabels,yticklabels:设置坐标轴刻度名称,维度要和xticks,yticks一致奥
# xlabel,ylabel:坐标轴名称
# title:图名
ax1.set(xlim=(0.5, 6), xticks=range(1, 6, 1), xticklabels=year, xlabel = 'x轴',
        ylim=(0, 400), yticks=range(0, 401, 100), yticklabels=range(0, 401, 100), ylabel = 'y轴',
        title='图名')

# 添加图例
ax1.legend(frameon=False)

步骤五:绘制饼图

###############################################################################
# 绘制饼图
ax2 = fig1.add_axes([0.054, 0.01, 0.2, 0.2])# 设置饼图位置
ax2.pie(a[0,:], colors=colors, autopct="%1.1f%%", textprops={'size':10, 'color':'k'})# 绘制饼图
ax3 = fig1.add_axes([0.163, 0.01, 0.2, 0.2])
ax3.pie(a[1,:], colors=colors, autopct="%1.1f%%", textprops={'size':10, 'color':'k'})
ax4 = fig1.add_axes([0.272, 0.01, 0.2, 0.2])
ax4.pie(a[2,:], colors=colors, autopct="%1.1f%%", textprops={'size':10, 'color':'k'})
ax5 = fig1.add_axes([0.381, 0.01, 0.2, 0.2])
ax5.pie(a[3,:], colors=colors, autopct="%1.1f%%", textprops={'size':10, 'color':'k'})
ax6 = fig1.add_axes([0.491, 0.01, 0.2, 0.2])
ax6.pie(a[4,:], colors=colors, autopct="%1.1f%%", textprops={'size':10, 'color':'k'})

 步骤六:保存图像

#保存图像
plt.savefig(figpath+'304 COVID-19 Fig2', dpi=600, bbox_inches = 'tight')
plt.show()


完整代码在这里:

# 导入库及文件
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams
# 设置西文字体为新罗马字体,中文宋体
config = {
            "font.family": 'serif',
            "font.size": 12,
            "mathtext.fontset": 'stix',
            "font.serif": ['SimSun'],
         }
rcParams.update(config)
rcParams['axes.unicode_minus']=False
############################################################
figpath = r'H:/00.csdn/02fig/'
# 赋值数值
a = np.array([[40, 57, 84, 32, 45, 45],
              [41, 75, 65, 84, 52, 63], 
              [46, 63, 45, 63, 24, 74], 
              [16, 42, 35, 45, 52, 63], 
              [14, 45, 46, 63, 45, 63]])
x = np.arange(1, 6, 1)

month = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN',]
year = ['2021', '2022', '2023', '2024', '2025']

colors = plt.get_cmap('tab20')(range(6))

###############################################################################
width = 0.6
fig1 = plt.figure(figsize=(15, 8))

# 绘制堆叠柱状图
ax1 = fig1.add_axes([0.1, 0.25, 0.6, 0.4]) # 设置柱状图位置
sum0 = 0
for i in range(0, len(month), 1):
    ax1.bar(x, a[:, i], width, align='center', label=month[i], color=colors[i], bottom=sum0)
    sum0 += a[:, i]

# 设置图中信息
# xlim,ylim:设置x轴y轴显示得最大最小值
# xticks,yticks: 设置坐标轴刻度,可控制隔几个标注,本图为两个一标
# xticklabels,yticklabels:设置坐标轴刻度名称,维度要和xticks,yticks一致奥
# xlabel,ylabel:坐标轴名称
# title:图名
ax1.set(xlim=(0.5, 6), xticks=range(1, 6, 1), xticklabels=year, xlabel = 'x轴',
        ylim=(0, 400), yticks=range(0, 401, 100), yticklabels=range(0, 401, 100), ylabel = 'y轴',
        title='图名')

# 添加图例
ax1.legend(frameon=False)
###############################################################################
# 绘制饼图
ax2 = fig1.add_axes([0.054, 0.01, 0.2, 0.2])# 设置饼图位置
ax2.pie(a[0,:], colors=colors, autopct="%1.1f%%", textprops={'size':10, 'color':'k'})# 绘制饼图
ax3 = fig1.add_axes([0.163, 0.01, 0.2, 0.2])
ax3.pie(a[1,:], colors=colors, autopct="%1.1f%%", textprops={'size':10, 'color':'k'})
ax4 = fig1.add_axes([0.272, 0.01, 0.2, 0.2])
ax4.pie(a[2,:], colors=colors, autopct="%1.1f%%", textprops={'size':10, 'color':'k'})
ax5 = fig1.add_axes([0.381, 0.01, 0.2, 0.2])
ax5.pie(a[3,:], colors=colors, autopct="%1.1f%%", textprops={'size':10, 'color':'k'})
ax6 = fig1.add_axes([0.491, 0.01, 0.2, 0.2])
ax6.pie(a[4,:], colors=colors, autopct="%1.1f%%", textprops={'size':10, 'color':'k'})
###############################################################################
#保存图像
plt.savefig(figpath+'304 COVID-19 Fig2', dpi=600, bbox_inches = 'tight')
plt.show()


  • 32
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值