【Matplotlib】科研绘图——折线图

1、导入

import matplotlib
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import numpy as np
import pandas as pd
%matplotlib inline

2、定义Font及Style

plt.style.use("seaborn-v0_8-whitegrid")
palette = plt.get_cmap('Set1')
font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size'   : 60, #32
}
font2 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size'   : 38, #24/28
}

3、设置图像大小及坐标刻度

fig=plt.figure(figsize=(18,12.6)) #12,8
my_x_ticks=np.arange(10,70,10)
lab = [r'10',r'20',r'30',r'40',r'50',r'60']#
#my_y_ticks=np.arange(50,90,10)

plt.xticks(ticks=my_x_ticks,labels=lab)
plt.yticks()

4、数据准备

iters=[10, 20, 30, 40,50,60]

y1 = [79,77,74,76,73 ,71 ]

y2 = [68,66 ,65,64,61,62 ] 

y3 =[48,47,48 ,45  ,44,42 ]

y4 = [58,56,55 ,53 ,54,50]

y5 = [39,38 ,37,36  ,34 ,30]
#y_formatter = FormatStrFormatter('%1.1f')

#下面数据可以用numpy根据任务需求算出
std1 =[1.47,1.46,2.01,1.11,2.07,1.95] 
std2 = [2.78,2.77, 2.34,2.28,2.34,2.89]
std3 = [1.5,2.7,2.0,1.7,2.0,2.3]
std4 = [2.64,3.64,1.69,3.32,2.29,1.97]
std5 = [3.00,2.49,3.40,3.10, 2.47,3.46]


r11 = list(map(lambda x: x[0]-x[1], zip(y1,std1)))#上方差
r12 = list(map(lambda x: x[0]+x[1], zip(y1, std1)))#下方差

r21 = list(map(lambda x: x[0]-x[1], zip(y2,std2)))#上方差
r22 = list(map(lambda x: x[0]+x[1], zip(y2, std2)))#下方差

r31 = list(map(lambda x: x[0]-x[1], zip(y3,std3)))#上方差
r32 = list(map(lambda x: x[0]+x[1], zip(y3, std3)))#下方差


r41 = list(map(lambda x: x[0]-x[1], zip(y4,std4)))#上方差
r42 = list(map(lambda x: x[0]+x[1], zip(y4, std4)))#下方差

r51 = list(map(lambda x: x[0]-x[1], zip(y5,std5)))#上方差
r52 = list(map(lambda x: x[0]+x[1], zip(y5, std5)))#下方差

5、自定义draw

def draw_line(name_of_alg, color_index, y,r1,r2, marker):
    if isinstance(color_index, str) :
        color = color_index
    else: color=palette(color_index)
    #avg=np.mean(datas,axis=0)
    #std=np.std(datas,axis=0)
    #r1 = list(map(lambda x: x[0]-x[1], zip(avg, std)))#上方差
    #r2 = list(map(lambda x: x[0]+x[1], zip(avg, std)))#下方差
    plt.plot(iters, y, color=color,label=name_of_alg,linewidth=8,
             marker=marker, markeredgecolor=color, markersize='24', markeredgewidth=4,markerfacecolor='none')
    plt.fill_between(iters, r1, r2, color=color, alpha=0.2)

draw_line("A",4,y1,r11,r12, 'D')
draw_line("B",2,y2,r21,r22, 's')
draw_line("C",1,y3,r31,r32, 'o')
draw_line("D",3,y4,r41,r42, 'v')
draw_line("E",7,y5,r51,r52, 'p')

6、其他设置

plt.xticks(fontsize=50)#25
plt.yticks(fontsize=50)#25
plt.xlabel('X(%)',font1)
plt.ylabel('Y(%)',font1)
#plt.legend(loc='lower left',prop=font2, frameon=True, fancybox=True,framealpha=0.5)
#网格
plt.legend(loc='lower left',prop=font2, frameon=True, fancybox=True,framealpha=0.5)
#标题
plt.title("EXAMPLE",fontsize=60)
#保存为pdf
plt.savefig('./EXAMPLE.pdf')
plt.show()

7、效果图

在这里插入图片描述

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲸可落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值