Python科研绘图第一期——线型图(Line)、条型图(Bar)、散点图(Scatter)、子图(subplot)

速览:

 


目录

一、线性图

二、条形图

三、散点图

四、子图


一、线型图

 图1、2代码

import matplotlib.pyplot as plt
import numpy as np
plt.rc('font',family='Times New Roman')

estimation = [42.8, 35.6, 57.24, 67.14, 107.19, 133.84, 175.02, 249.91, 333.99, 371.07]
prediction = [50, 39.199999999999996, 30.200000000000003, 65.34, 76.14,
			  131.69, 159.39000000000001, 208.37, 304.01, 403.09000000000003]

# 绘图 #
fig, ax1 = plt.subplots(figsize=(5, 4))
x = np.linspace(0,400,len(estimation))
ax1.axis('auto')	# 参数 'scale','auto','square'
ax1.plot(x, estimation, 'd--', c='navy', linewidth=2, markerfacecolor='w',
		 markeredgecolor='k', label='kalman estimation')
ax1.plot(x, prediction, '^--', c='purple', linewidth=2, markerfacecolor='w',
		 markeredgecolor='k', label='kalman prediction')
ax1.set_xlabel('time', fontsize=12)
ax1.set_ylabel('estimation/prediction value', fontsize=12)
ax1.set_title('Figure. 1',fontweight='bold', fontsize=12)
ax1.set_xticks(x)
ax1.set_xticklabels(['the' + str(i) for i in range(1, 11, 1)])
ax1.tick_params(top=True,right=True,direction='in')		# 右边和上边的刻度都显示,且刻度向内。
ax1.legend(fontsize=10, edgecolor='black')
ax1.spines['bottom'].set_linewidth(1.5)
ax1.spines['left'].set_linewidth(1.5)
ax1.spines['right'].set_linewidth(1.5)
ax1.spines['top'].set_linewidth(1.5)
ax1.grid(axis='both', ls='--')	# axis = 'both','x','y'

fig2, ax2 = plt.subplots(figsize=(5, 4))
x = np.linspace(0,400,len(estimation))
ax2.axis('auto')	# 参数 'scale','auto','square'
ax2.plot(x, estimation, 'd--', c='navy', linewidth=2, markerfacecolor='w',
		 markeredgecolor='k', label='kalman estimation')
ax2.plot(x, prediction, '^--', c='purple', linewidth=2, markerfacecolor='w',
		 markeredgecolor='k', label='kalman prediction')
ax2.set_xlabel('time', fontsize=12)
ax2.set_ylabel('estimation/prediction value', fontsize=12)
ax2.set_title('Figure. 2',fontweight='bold', fontsize=12)
ax2.set_xticks(x)
ax2.set_xticklabels(['the' + str(i) for i in range(1, 11, 1)])
ax2.legend(fontsize=10, edgecolor='black')
ax2.tick_params(direction='in')
ax2.spines['right'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.spines['bottom'].set_linewidth(1.5)
ax2.spines['left'].set_linewidth(1.5)
ax2.spines['right'].set_linewidth(1.5)
ax2.spines['top'].set_linewidth(1.5)
# ax2.grid(axis='y', ls='--')	# axis = 'both','x','y'

plt.show()

 图3 代码

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_excel("时序.xlsx")
time = df["time"]
time_ls = []
for i in range(len(time)):
	temp = str(time[i])
	temp = temp.replace(" 00:00:00", "")
	time_ls.append(str(temp))

cygnss = df['A']
cygnss = np.array(cygnss)
smap = df['B']
smap = np.array(smap)
M = len(smap)
# ========绘图=========== #
plt.rc('font',family='Times New Roman')
fig,ax = plt.subplots(figsize=(6,4))
ax.axis('auto')
ax.plot(cygnss,'-',c='navy',label='cygnss')
ax.plot(smap,'-',c='purple',label='smap')
ax.set_xlabel('time',fontsize=12)
ax.set_ylabel('cygnss/smap value',fontsize=12)
ax.set_title('Figure',fontsize=12,fontweight='bold')
ax.tick_params(direction='in')
ax.legend(fontsize=10,edgecolor='black')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_linewidth(1.5)
ax.spines['left'].set_linewidth(1.5)
ax.spines['right'].set_linewidth(1.5)
ax.spines['top'].set_linewidth(1.5)
ax.set_xticks(np.arange(0,M,30),[str(i)+'st' for i in np.arange(0,M,30)])
plt.show()

 

二、条形图

 图4,5,6代码(图6 将误差参数去掉即为图5)

import matplotlib.pyplot as plt
import numpy as np
plt.rc('font',family='Times New Roman')

fig, ax = plt.subplots(figsize=(5,4))
ax.axis('auto')
fruits = ['apple', 'blueberry', 'cherry', 'orange','banana','others']
counts = [40, 100, 30, 55, 70, 35]
ax.bar(fruits,counts,width=0.4,edgecolor='black',color='mediumblue')
ax.set_xlabel('Category',fontsize=12)
ax.set_ylabel('Value',fontsize=12)
ax.tick_params(direction='in')
ax.set_title('Figure. 1',fontsize=12,fontweight='bold')
for i in ['bottom','left','right','top']:
	ax.spines[i].set_linewidth(1.5)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)


fig2,ax2 = plt.subplots(figsize=(5,4))
ax2.axis('auto')
fruits = ['apple', 'blueberry', 'cherry', 'orange','banana','others']
count1 = [40, 100, 30, 55, 70, 35]
count2 = [60, 80, 38, 65, 29, 42]
x = np.arange(len(count2))
width = 0.4
error = np.array([3,5,6,10,7,9])
for i in ['bottom','left','right','top']:
	ax2.spines[i].set_linewidth(1.5)
ax2.bar(x-width/2,count1,width=width,yerr=error,label='male',color='mediumblue',edgecolor='black')
ax2.bar(x+width/2,count2,width=width,yerr=error,label='famale',color='maroon',edgecolor='black')
ax2.legend(fontsize=10,edgecolor='black')
ax2.set_xticks(x,fruits)
ax2.set_xlabel('Category',fontsize=12)
ax2.tick_params(top=True,right=True,direction='in')
ax2.set_ylabel('Value',fontsize=12)
ax2.set_title('Figure. 2',fontsize=12,fontweight='bold')
plt.show()

三、散点图 

 图7 代码

import matplotlib.pyplot as plt
import numpy as np
x = np.random.rand(50,1)
y = np.random.rand(50,1)

plt.rc('font',family='Times New Roman')

fig,ax = plt.subplots(figsize=(5,4))
for i in ['bottom','left','right','top']:
	ax.spines[i].set_linewidth(1.5)
ax.scatter(x,y,marker='x',color='r',label='item 1')

a = np.random.rand(50,1)
b = np.random.rand(50,1)
ax.scatter(a,b,marker='.',color='b',label='item 2')
ax.set_xlabel('Value',fontsize=12)
ax.set_ylabel('Value',fontsize=12)
ax.tick_params(right=True,top=True,direction='in')
ax.set_title('Figure. 1',fontsize=12)
ax.legend(loc='best',fontsize=10,edgecolor='black')
plt.show()

四、子图

图8 代码 

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
plt.rc('font',family='Times New Roman')

df = pd.read_excel("时序.xlsx")
time = df["time"]
time_ls = []
for i in range(len(time)):
	temp = str(time[i])
	temp = temp.replace(" 00:00:00", "")
	time_ls.append(str(temp))

cygnss = df['A']
cygnss = np.array(cygnss)
smap = df['B']
smap = np.array(smap)
M = len(smap)

fig,[ax1,ax2] = plt.subplots(2,1,figsize=(8,5))

ax1.axis('auto')
ax1.plot(cygnss,'-',color='darkred',label='cygnss',)
ax1.spines['right'].set_visible(False)
ax1.spines['top'].set_visible(False)
ax1.set_ylabel('Cygnss',fontsize=12)
ax1.legend(fontsize=10,edgecolor='black',loc='upper right')
ax1.set_title('Figure. 1',fontweight='bold',loc='left')
ax1.set_xlabel('Time',fontsize=12)

for i in ['bottom','left','right','top']:
	ax1.spines[i].set_linewidth(1.5)
	ax2.spines[i].set_linewidth(1.5)

ax2.plot(smap,label='smap',color='mediumblue')
ax2.spines['right'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax2.set_ylabel('Smap',fontsize=12)
ax2.set_xlabel('Time',fontsize=12)
ax2.legend(fontsize=10,edgecolor='black',loc='upper right')
ax2.set_title('Figure. 2',fontweight='bold',loc='left')

fig.tight_layout()  # 紧凑布局
plt.show()

  • 2
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
python的智能算法_Python智能优化算法库⼩汇总 最近查了⼀圈python的智能优化算法库,发现在python⾥⾯这样的库相对⼀些传统的语⾔还真是不太多(⽐如Matlab)。总的看起来似乎起 步都还⽐较晚(个⼈认为有可能是因为智能算法本⾝相对复杂并且过于专业,多数应该还是集中在专业做优化的圈⼦⾥)。 ⽬前总结了以下库: 1、DEAP 2、mealpy 3、scikit-opt (国产良⼼) 4、Geatpy2(国产⽤⼼) 5、pygmo2 6、pyswarms 7、SciPy 1、DEAP 安装: pip install deap 优点:起点⾼,发表在Journal of Machine Learning Research ⽤法灵活,所有模块均可⾃定义 缺点:上⼿⿇烦⽐较⿇烦 2、mealpy 安装: pip install meaply 优点:算法丰富,集成了现有的62种算法,⽬测应该还在继续更新添加 上⼿容易,代码简单,⽐如对标准函数库的函数进⾏优化: 缺点:整体设计似乎不太规范,⽂档解释不够 语⾔上似乎还存在⼀点⼩问题,英语应⽤不够规范(这可能也是作者没有发表很好的杂志的原因之⼀) 集成了多种算法但未列举参考⽂献,不⽅便论⽂引⽤ 3、scikit-opt (国产良⼼) 安装: pip install scikit-opt 优点:上⼿容易,代码简单,尤其许多⽤法很像Matlab。⽐如官⽅⽂档就提供了⼀些例⼦:使⽤遗传算法进⾏曲线拟合 中⽂⽂档,并且⽂档很全。⼤佬的CSDN主页。(作者 @幼鹰me 曾经是京东算法⼯程师,现在是蚂蚁算法⼯程师,这个库也的确感觉得到 ⼀些情怀,⽅便实⽤的感觉) 在使⽤⽅便的基础上,也提供了不少接⼝⽤于⾃⾏修改。尤其可以⾃定义算⼦。 ⼀些较好玩的特性:GPU加速、断点运⾏等。 缺点:⽬前似乎还没有集成⾜够多的⽅法。⼤类有3类,共7种算法。 算法本⾝的优化似乎还不⾜(未仔细测试) 4、Geatpy2(国产⽤⼼) 安装: pip install geatpy 或者强制版本 pip install geatpy==2.5.1 优点:上⼿容易,实现简单 ⽂档完整,⽰例丰富(中⽂⽂档) 功能齐全,除算法以外也封装了许多实⽤的功能,⽐如数据可视化等 缺点:代码风格诡异 英⽂⽤语不规范,变量命名相对随意,⽐如⼊门⽂档中,将⽬标函数翻译成"aimFunc",变量名称XM?等。 ⽂档开展还显得⽐较稚嫩,⽐如主页⽂档就⽤插件显⽰,官⽹体验还⽋些⽕候 注:这个项⽬得多补充⼏句,⽬测应该是硕⼠⽣写的。总体的设计、DEMO、⽂档上给⼈感觉都远不如上⾯⼏个⽼练(尤其和DEAP相⽐)。 但⼏个学⽣跨校合作,在不长的时间内能够完成如此完整的⼀个⼯作也实⾮易事。也希望他们能够继续进步。综合来看,这个项⽬个⼈认为 属于国产⽤⼼。 5、pygmo2 安装: pip install pygmo 注:安装还依赖如下环境 其它可能会⽤到的环境: 优点:功能丰富 ⽀持并⾏和分布式计算(本⾝运⾏效率也很⾼) ⽤法灵活 感觉测试相对稳定(未仔细测试) 缺点:上⼿相对⿇烦 依赖项较多,尤其要求对应C++版的软件环境 算法有限 注:这个库也吐槽⼀句,⽹传很厉害,但真没看出来除了看起来专业以外有何厉害之处。当然通常以C++为底层的东西有可能在⼤量级问题 上的处理更加容易部署,但⼀般的⽤户也⽤不到。 6、pyswarms 安装: pip install pyswarms 优点:简单,易上⼿,基本上也属于⼏⾏代码⼊门型 可视化,尤其动态可视化做得好。(亮点),⽐如画出形,基本上就是⼀句代码: 缺点:算法少。只有基于PSO的各类问题的算法(本质上其实就只是⼀个算法) 灵活度不够。 起点低。发表的论⽂既没进⼊CCF推荐,也没进⼊SCI。同时虽然号称有许多研究论⽂⽤过,但仔细看级别都不⾼(基本都属于三⽆型) 7、SciPy(想不到吧) 安装(当然其实当你装上Python的时候它就在了): pip install scipy 特别说明: scipy作为⼀个相对严谨和'传统'的数值计算库,基本上常⽤的数值计算⽅法都有涉及。当然数值计算型的库通常都不会把智 能优化(启发式算法)作为重点。它集成了⼗分有限的⼏种优化算法:差分进化、模拟退⽕等。但它仍然有不少优点: 优点:作为严谨的数值计算库,它的正确性和稳定性值得信赖(超过800个contributor,其中有不少是名校的教师、博⼠等) 使⽤⽅便、简单 scipy也可以⾃定义优化算法 缺点:⾃然就是算法太少了 虽然可以⾃定义,但⿇烦程度⼏乎相当于完全⾃⼰造轮⼦ 后续如果再发现好⽤的库我们会继续更新,如果⼤家有好的建议也欢迎评论补充! 写在后⾯没想到这么个⼩破⽂章居然招来了两位作者( @幼鹰me @秋宏 )的亲⾃

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值