科研论文配图的绘制与配色基础2- Matplotlib

2.1 Matplotlib

2.1.1 图形元素

利用Matplotlib绘制图形的主要图形元素包括:

  • 画布 Figure
  • 坐标图形 Axes
    • 图名 Title
    • 刻度 Tick
    • 刻度坐标 tick label
    • 轴标签 axis label
    • 轴脊 spine
    • 图例 legend
    • 网格 grid
    • 线 line
    • 数据标记 maker
  • 轴 Axis
  • 艺术对象 Aritist

在使用 Matplotlib 绘图时,用户要重点理解不同图形元素的含义和使用方法
在这里插入图片描述

2.1.2 图层顺序

Images - Order 1
Patch - Order 2
Line2D - Order 3
Text - Order 4
Inset Axes & Legend - Order 5

2.1.3 轴比例和刻度

Matplotlib 亦是如此。Matplotlib 中的每个坐标图形对象至少包含两个轴对象,它们分别用来表示 X 轴和 Y 轴。轴对象还可以控制轴比例(axis scale)、刻度位置(tick locator)和刻度格式(tick formatter)。

2.1.4 坐标系

常见的坐标系有直角坐标系(rectangular coordinate system)、极坐标系(polar coordinate system)和地理坐标系(geographic coordinate system),其中直角坐标系和地理坐标系在科研论文绘图中出现的频率较高。图 2-1-7 所示为 Matplotlib 中使用的 3 种坐标系的示意图。
在这里插入图片描述

2.1.5 多子图的绘制

  • Subplot() 函数
import matplotlib.pyplot as plt
ax1 = fig.add_subplot(212)
ax2 = fig.add_subplot(221)
ax3 = fig.add_subplot(222)
  • add_subplot()函数
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(212)
ax2 = fig.add_subplot(221)
ax3 = fig.add_subplot(222)

  • subplots()函数
fig, axs = plt.subplots(2, 3, sharex=True, sharey=True)
axs[0,0].text(0.5, 0.5, "subplots(0,0)")
axs[0,1].text(0.5, 0.5, "subplots(0,1)")
axs[0,2].text(0.5, 0.5, "subplots(0,2)")
axs[1,0].text(0.5, 0.5, "subplots(1,0)")
axs[1,1].text(0.5, 0.5, "subplots(1,1)")
axs[1,2].text(0.5, 0.5, "subplots(1,2)")

  • axes()
import numpy as np
import matplotlib.pyplot as plt
from colormaps import parula
np.random.seed(19680801)
plt.subplot(211)
plt.imshow(np.random.random((100, 100)),cmap=parula)
plt.subplot(212)
plt.imshow(np.random.random((100, 100)),cmap=parula)
plt.subplots_adjust(bottom=0.1, right=0.8, top=0.9)
cax = plt.axes(rect=[0.8, 0.15, 0.05, 0.6])
plt.colorbar(cax=cax)

  • subplot2grid()
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3, 3), (2, 0))
ax5 = plt.subplot2grid((3, 3), (2, 1))

  • gridspec.GridSpec()
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(constrained_layout=True)
gspec = gridspec.GridSpec(ncols=3, nrows=3, figure=fig)
ax1=plt.subplot(gspec[0,:])
ax2=plt.subplot(gspec[1,0:2])
ax3=plt.subplot(gspec[1:,2])
ax4=plt.subplot(gspec[2,0])
ax5=plt.subplot(gspec[-1,-2])

  • subplot_mosaic()
subplot_mosaic(mosaic,)

def annotate_axes(ax, text, fontsize=fontsize):
	ax.text(0.5, 0.5, text, transform=ax.transAxes,
		fontsize=fontsize,alpha=0.75, ha="center",
		va="center", weight="bold")
fig, axd = plt.subplot_mosaic([['upper left', 'right'],
		['lower left', 'right']],figsize=(6,3), 
		constrained_layout=True)
for k in axd:
	annotate_axes(axd[k], f'axd["{k}"]', fontsize=14)

2.1.6 常见的图类型

Matplotlib 可用于绘制常见的图,如误差图、散点图、柱形图、饼图、直方图和箱线图等。绘制每类图的函数都提供了详细的参数,可用于设置图中的颜色、线条宽度、透明度、形状等。想要使用 Matplotlib 绘制高度定制化的图,就必须了解不同绘制函数的参数。表 2-1-2 列出了 Matplotlib 中常见的绘图函数及其核心参数,以及对应的图类型。

在这里插入图片描述

2.1.7 结果保存

Matplotlib 绘制的图对象可以保存为多种格式,如 PNG、JPG、TIFF、PDF 和 SVG 等。注意,结果保存函数 savefig() 必须出现在 show() 函数之前,可避免保存结果为空白等问题。另外,在使用 savefig() 的过程中,我们需要设置参数 bbox_inches=‘tight’,去除图表周围的空白部分。将图对象保存为 PDF 文件和 PNG 文件的示例代码如下。

fig.savefig('结果.pdf',bbox_inches='tight')
fig.savefig('结果.png', bbox_inches='tight',dpi=300)
plt.show()

该文章源自《科研论文配图的绘制与配色基础》书籍的 学习笔记

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值