我是理工科的学生,论文里面当然需要大量的实验仿真图啦。在此总结整理出一个模板,以后方便使用

1. 源代码

import matplotlib.pyplot as plt
fig, ax = plt.subplots(1, 1) #这个是只有一个图的情形 可加参数figsize=(6, 4)

ax.plot(x_1,y_1,color=(0/255,0/255,0/255),marker='s',markersize=10,label='label_1')
ax.plot(x_2,y_2,color=(100/255,29/255,161/255),marker='^',markersize=10,label='label_2')
ax.plot(x_3,y_3,color=(0/255, 51/255, 102/255),marker='D',markersize=10,label='label_3')
ax.plot(x_4,y_4,color=(163/255, 11/255, 25/255),marker='o',markersize=10,label='label_4')
ax.legend()
#ax.set_ylim((-50,1500))
ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')



#以下代码是添加一个放大的子图,一般绘图不需要
from mpl_toolkits.axes_grid1.inset_locator import mark_inset
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

axins = inset_axes(ax, width="40%", height="30%",loc='center', #子图的大小和位置
                   bbox_to_anchor=(0, 0, 1, 1), #别动吧。。。
                   bbox_transform=ax.transAxes)
axins.plot(x_3,y_3,color=(0/255, 51/255, 102/255),marker='D',markersize=10,label='label_3')
axins.plot(x_4,y_4,color=(163/255, 11/255, 25/255),marker='o',markersize=10,label='label_4')
zone_left = 10   #需要放大的区间
zone_right = 14
x_ratio = 0.5
y_ratio = 0.5
x=dims
y_1=np.array(y_3)
y_2=np.array(y_4)

xlim0 = x[zone_left]-(x[zone_right]-x[zone_left])*x_ratio
xlim1 = x[zone_right]+(x[zone_right]-x[zone_left])*x_ratio
y = np.hstack((y_1[zone_left:zone_right], y_2[zone_left:zone_right]))
ylim0 = np.min(y)-(np.max(y)-np.min(y))*y_ratio
ylim1 = np.max(y)+(np.max(y)-np.min(y))*y_ratio
axins.set_xlim(xlim0, xlim1)
axins.set_ylim(ylim0, ylim1)
# loc1 loc2: 坐标系的四个角
# 1 (右上) 2 (左上) 3(左下) 4(右下)
mark_inset(ax, axins, loc1=3, loc2=1, fc="none", ec='k', lw=1)


plt.savefig('figures.pdf')#dpi=500
plt.show()

1. line

linestyle=None,linewidth=None
Linestyle Description
'-' or 'solid' solid line
'--' or 'dashed' dashed line
'-.' or 'dashdot' dash-dotted line
':' or 'dotted' dotted line
'None' or ' ' or ''draw nothing  

2. marker

marker=None

有以下这些参数,可以自由选择

('o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd', 'P', 'X')

下面的对比图就非常直观

figures

3. color

颜色的话,默认的颜色都不是特别好看,一般自己搭配颜色把。比如上面源代码里的

(100/255,29/255,161/255) : 清华紫 (0/255, 51/255, 102/255) :港科蓝 (163/255, 11/255, 25/255):上科大红

除以255是因为matplotlib只接受0到1的RGB值

这也是我们老板呆过的三个学校,也是他最喜欢的颜色。

之后可以在别人论文中看看比较好看的配色,或者从网上找配色方案。基本截屏工具都有屏幕取色的功能。

推荐1:

(217,82,24)、(237,176,31)、(0,113,188)、(125,46,141)、(119,172,47)

4. 局部放大

我参考的:【Matplotlib】 局部放大图画法

其他参考:mpl_toolkits.axes_grid1.inset_locator.inset_axes

5.使用标签和标题里使用latex数学共识

import matplotlib.pyplot as plt
plt.rcParams["mathtext.fontset"] = "cm"  #指定字符集,这个字符集更全
plt.rcParams["text.usetex"] =True  #开启使用latex编译

plt.title(r'$a+b=c$')

注:

  • 字符串前面加r,即不使用转义字符
  • 本机需要安装latex 编译器