利用matplotlib为论文画图(二)-柱状图

上一篇文章简单介绍了会用matplotlib的官网提供的模板作图,本篇文章介绍几个常用图(本人计算机网络方向)。

我个人认为一般图表描述一些基本的数据,对Python的能力要求不高。选中合适的模板,进行代码的修改。需要问题,针对具体的需求去查询具体的函数。只需要分析特定的函数和相关的参数,不断累积,就可以满足各种数据图的需求。

目录

 

1、简单的柱状图

柱状图是论文中的基础图形,很多数据的结果都是基于柱状图描述的。

下面是一个简单的柱状图例子

源代码如下所示:

from matplotlib import pyplot as plt
from matplotlib import font_manager
import matplotlib as mpl
import numpy as np
plt.rcParams['font.sans-serif'] = ['Times New Roman']
#设置字体
font = {'family': 'Times New Roman',
         'weight': 'normal',
         'size': 14,
         }
# 设置横纵坐标的名称以及对应字体格式
font2 = {'family': 'Times New Roman',
         'weight': 'normal',
         'size': 18,
         }

#创建数据
# 先得到labels长度, 再得到下标组成列表
#x = range(len(labels))
labels = ['Z', 'U', 'S', 'D', 'X', 'Q', 'D', 'J', 'R', 'R', 'R']
x = np.arange(len(labels))

y = [5.72, 7.26, 3.64, 12.06, 15.36, 19.26, 25.2, 20.9, 0.98, 12.14, 30.67]
width = 0.4      #the width of the bars: can also be len(x) sequence

plt.xlim(-0.1,10.5)
#error_kw=dict(lw=5, capsize=5, capthick=3)
plt.bar(x, y, width = width, color='b')
plt.xticks([i+0.2 for i in x], labels)
#set label
plt.xlabel('protocol role',font2)
plt.ylabel('start time(us)',font2)       

#输出图片
plt.savefig("fig.pdf", dpi=1400, format='pdf')#eps
plt.legend()
plt.show()

 

2、复杂的柱状图

下面是一个简单的事例。

事例的代码如下:

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Times New Roman']
styles = ['normal', 'italic', 'oblique']
weights = ['light', 'normal', 'medium', 'semibold', 'bold', 'heavy', 'black']
font = {'family': 'Times New Roman',
         'weight': 'normal',
         'size': 14,
         }
#设置图例坐标的名称以及对应字体格式
font1 = {'family': 'Times New Roman',
         'weight': 'bold',
         'color':  'black', 
         'size': 16,
         }
# 设置横纵坐标的名称以及对应字体格式
font2 = {'family': 'Times New Roman',
         'weight': 'normal',
         'size': 18,
         }
font3 = {'family': 'Times New Roman',
         'weight': 'bold',
         'style':'italic',
         'size': 20,
         }
#横纵坐标
labels = ['S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9']
x = np.arange(len(labels))
y0 = [201.632, 201.624, 201.632, 201.632, 201.624, 201.632, 201.632, 201.632, 201.624]
y1 = [20.024, 10.016, 20.024, 20.024, 10.016, 20.024, 20.024, 20.024, 10.016]
y2 = [120.824, 120.800, 121.032, 121.032, 121.000, 121.224, 121.224, 121.224, 121.200]
y3 = [60.408, 120.504, 60.512, 60.512, 120.704, 60.608, 60.608, 60.608, 60.760, 120.904]
y4 = [60.456, 0, 60.608, 60.608, 0, 60.760, 60.760, 60.608, 0]
y5 = [900.000, 800.056, 900.000, 900.000, 800.104, 900.000, 900.000, 900.000, 800.160]
v0 = [i for i in y0] 
v1 = [i + j for i, j in zip(v0, y1)]
v2 = [i + j for i, j in zip(v1, y2)]
v3 = [i + j for i, j in zip(v2, y3)]
v4 = [i + j for i, j in zip(v3, y4)]
v5 = [i + j for i, j in zip(v4, y5)]

width = 0.6

plt.bar(x, v5, width = width, label="L1", color='red'        )
plt.bar(x, v4, width = width, label="L2",color='orange'   )
plt.bar(x, v3, width = width, label="L3",color='salmon'      )
plt.bar(x, v2, width = width, label="L4",color='green'      )
plt.bar(x, v1, width = width, label="L4",color='snow'      )
plt.bar(x, v0, width = width, label="L5",color='olive' )

plt.xlim(-0.1,8.8)
plt.xticks([i+0.3 for i in x], labels)

plt.text(0.4, 1300.000, "Expe.1", font3)
plt.text(3.4, 1300.000, "Expe.2", font3)
plt.text(6.4, 1300.000, "Expe.3", font3)

#界限
plt.vlines(1.8, 0, 1400.000, colors = "r", linestyles = "dashed")
plt.vlines(4.8, 0, 1400.000, colors = "r", linestyles = "dashed")

plt.xlabel('Xlabel',font2)
plt.ylabel('Ylabel',font2)

plt.legend(loc='upper center', bbox_to_anchor=(0.5,0.75),ncol=3,fancybox=True,shadow=True)
plt.savefig("fig5.pdf", dpi=1400, format='pdf')#eps
plt.show()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值