本文主要是介绍【论文复刻】 堆叠柱状图变形,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
复刻了一下这篇论文里的fig4a:Impacts of COVID-19 and fiscal stimuli on global emissions and the Paris Agreement | Nature Climate Change
效果图:
主要步骤:
1. 数据准备:随机赋值
2. 数据处理:计算了一个百分数
3. 图像绘制:(1)绘制变形的堆叠柱状图
(2)灵活控制文本位置
(3)用箭头突出显示变化
详细代码:着急的直接拖到最后有完整代码
步骤一:导入库包及图片存储路径并设置中文字体为宋体,西文为新罗马(没有的库包要先下好奥)
# 导入库及文件
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/'# 随机赋值数据
a0 = 140.9
a = np.array([0.3, 0.5, 0.6, 0.4, 0.2])x = range(1, 10, 1)# 计算百分比并进行格式化
proportion1 = a/np.sum(a)*100
proportion1 =['{:.2f}%'.format(i) for i in proportion1]# 文本标签
proportion2 = ['aaaaaaaaaaaa','bbbbbbbbbbbb','cccccccccccc','dddddddddddd','eeeeeeeeeeee','ffffffffffff','gggggggggggg']colors = ['#656565', '#D27D75', '#81BAC8', '#64A99C', '#64A99C', '#A49384']
步骤三:绘制柱状图及其上下文本
###############################################################################
width = 0.8fig = plt.figure(figsize=(8, 8))
ax = fig.add_axes([0.1, 0.25, 0.6, 0.4])# 绘制第一个bar
ax.bar(x[0], a0, width, align='center', color=colors[0], bottom=0)
ax.text(x[0], 140.1 , 'xxxxxxx', color='w', fontsize=10, rotation=90, horizontalalignment="center")# 绘制第二到五个bar
sum0 = a0
for i in range(0, 5, 1):ax.bar(x[i+1], a[i], width, align='center', color=colors[i+1], bottom=sum0)sum0 += a[i]if i < 2: ax.text(x[i+1], sum0-a[i]-0.2 , proportion1[i], fontsize=10, rotation=0, horizontalalignment="center")ax.text(x[i+1], sum0+0.1 , proportion2[i], fontsize=10, rotation=90, horizontalalignment="center")elif i >= 2 and i < 5:ax.text(x[i+1], sum0+0.1, proportion1[i], fontsize=10, rotation=0, horizontalalignment="center")ax.text(x[i+1], sum0-a[i]-1.0 , proportion2[i], fontsize=10, rotation=90, horizontalalignment="center")# 添加6 7 文本
ax.text(x[6], sum0-0.5, proportion2[5], fontsize=10, rotation=90, horizontalalignment="center")
ax.text(x[7], sum0-0.5, proportion2[6], fontsize=10, rotation=90, horizontalalignment="center")# 绘制第八个bar
ax.bar(x[8], sum0, width, align='center', color=colors[0], bottom=0)
ax.text(x[8], 140.1 , 'xxxxxxxxxxxxxxx', color='w', fontsize=10, rotation=90, horizontalalignment="center")
步骤四:设置标题文本,坐标轴信息,及箭头
# 添加标题信息
ax.text(5, 143.5 , 'xxxxxxxxxxx\nyyyyyyyyyyyyyy', color='k', fontsize=10, rotation=0, horizontalalignment="center")# 设置坐标轴信息
ax.set(xlim=(0, 10), xticks=[],ylim=(140, 144), yticks=range(140, 145, 1), yticklabels=range(140, 145, 1), ylabel = 'y轴')# 添加箭头及箭头上方文本
plt.arrow(2,140.2, 6, 0, width=0.01, head_width=0.06, facecolor='gray', edgecolor='gray')
ax.text(5, 140.3 , '+ '+'%3.2f' %((sum0-a0)/a0) + '%', color='k', fontsize=10, rotation=0, horizontalalignment="center")
步骤五:保存图像
###############################################################################
# 保存图像
plt.savefig(figpath+'305 COVID-19 Fig4', 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/'# 随机赋值数据
a0 = 140.9
a = np.array([0.3, 0.5, 0.6, 0.4, 0.2])x = range(1, 10, 1)# 计算百分比并进行格式化
proportion1 = a/np.sum(a)*100
proportion1 =['{:.2f}%'.format(i) for i in proportion1]# 文本标签
proportion2 = ['aaaaaaaaaaaa','bbbbbbbbbbbb','cccccccccccc','dddddddddddd','eeeeeeeeeeee','ffffffffffff','gggggggggggg']colors = ['#656565', '#D27D75', '#81BAC8', '#64A99C', '#64A99C', '#A49384']
###############################################################################
width = 0.8fig = plt.figure(figsize=(8, 8))
ax = fig.add_axes([0.1, 0.25, 0.6, 0.4])# 绘制第一个bar
ax.bar(x[0], a0, width, align='center', color=colors[0], bottom=0)
ax.text(x[0], 140.1 , 'xxxxxxx', color='w', fontsize=10, rotation=90, horizontalalignment="center")# 绘制第二到五个bar
sum0 = a0
for i in range(0, 5, 1):ax.bar(x[i+1], a[i], width, align='center', color=colors[i+1], bottom=sum0)sum0 += a[i]if i < 2: ax.text(x[i+1], sum0-a[i]-0.2 , proportion1[i], fontsize=10, rotation=0, horizontalalignment="center")ax.text(x[i+1], sum0+0.1 , proportion2[i], fontsize=10, rotation=90, horizontalalignment="center")elif i >= 2 and i < 5:ax.text(x[i+1], sum0+0.1, proportion1[i], fontsize=10, rotation=0, horizontalalignment="center")ax.text(x[i+1], sum0-a[i]-1.0 , proportion2[i], fontsize=10, rotation=90, horizontalalignment="center")# 添加6 7 文本
ax.text(x[6], sum0-0.5, proportion2[5], fontsize=10, rotation=90, horizontalalignment="center")
ax.text(x[7], sum0-0.5, proportion2[6], fontsize=10, rotation=90, horizontalalignment="center")# 绘制第八个bar
ax.bar(x[8], sum0, width, align='center', color=colors[0], bottom=0)
ax.text(x[8], 140.1 , 'xxxxxxxxxxxxxxx', color='w', fontsize=10, rotation=90, horizontalalignment="center")# 添加标题信息
ax.text(5, 143.5 , 'xxxxxxxxxxx\nyyyyyyyyyyyyyy', color='k', fontsize=10, rotation=0, horizontalalignment="center")# 设置坐标轴信息
ax.set(xlim=(0, 10), xticks=[],ylim=(140, 144), yticks=range(140, 145, 1), yticklabels=range(140, 145, 1), ylabel = 'y轴')# 添加箭头及箭头上方文本
plt.arrow(2,140.2, 6, 0, width=0.01, head_width=0.06, facecolor='gray', edgecolor='gray')
ax.text(5, 140.3 , '+ '+'%3.2f' %((sum0-a0)/a0) + '%', color='k', fontsize=10, rotation=0, horizontalalignment="center")
###############################################################################
# 保存图像
plt.savefig(figpath+'305 COVID-19 Fig4', dpi=600, bbox_inches = 'tight')
plt.show()
这篇关于【论文复刻】 堆叠柱状图变形的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!