python-matplotlib绘图总结(面对函数和面对对象绘图技巧)

环境 : matplotlib 3.1.0,  numpy 1.15.4

目录

使用matplotlib作图的两大方法

一 面对函数绘图(pyplot模块有大量函数,供用户调用)

1. 主要分为四个步骤:

2. 代码实例(单图和多图)

3. 图片展示

二 面对对象绘图(主要操作Figure和Axes对象)(推荐)

1. 主要分为四个步骤:

2. 代码实例(单图和多图)

3. 图片展示


使用matplotlib作图的两大方法

本教程可以作为科研作图模板, 涵盖了作图中很多小细节, 使用了matplotlib作图的常用函数和对象

一 面对函数绘图(pyplot模块有大量函数,供用户调用)

functions大都是从matlab移植过来的,熟悉matlab绘图的读者能够很快入门 

1. 主要分为四个步骤:

(1) 准备数据和创建画布; (2) 绘制线条、散点图; (3) 对线条、散点图、坐标轴进行描述;(4) 显示画布。

2. 代码实例(单图和多图)

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

# 1. 准备数据
np.random.seed(19680801)
N, fonts = 50, 8

x1 = np.linspace(0, 2*np.pi, num=1000)
y1, yy1 = 0.2*np.sin(x1*10) + 0.75, 0.15*np.sin(x1*10+0.2) + 0.25
x2, y2 = np.random.rand(N), np.random.rand(N)

# 创建画布
plt.figure(figsize=(6, 4), dpi=300)

# 2.绘制线条、散点图
plt.plot(x1, y1, color='r', linewidth=2, label="line 1")
plt.plot(x1, yy1, color='g', linewidth=2, label="line 2")
plt.scatter(x2, y2, s=8, c='b', label="scatter", marker='*')
# 3. 对线条、散点图、坐标轴进行描述
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('xlable', fontsize=fonts)
plt.ylabel('ylabel', fontsize=fonts)
xticklabels = np.arange(0, 1.2, 0.2)
yticklabels = np.arange(0, 1.2, 0.2)
plt.xticks(ticks=xticklabels, labels=[str(round(xx, 1)) for xx in xticklabels])
plt.yticks(ticks=yticklabels, labels=[str(round(xx, 1)) for xx in yticklabels])
plt.title("figure 1", fontsize=fonts)
plt.legend(loc='upper left', fontsize=fonts)
plt.grid(True)
plt.tight_layout(pad=0.3)

# 4. 显示并保存画布
plt.savefig("./1.tiff")
plt.show()

##############################################################################################################
# 1.准备数据( 参上)
# 创建画布
plt.figure(figsize=(6, 3), dpi=300)

# 2.对线条、散点图、坐标轴进行描述
# 绘制第一个子图
plt.subplot(121)
plt.plot(x1, y1, color='r', linewidth=2, label="line 1")
plt.plot(x1, yy1, color='g', linewidth=2, label="line 2")
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('xlable', fontsize=fonts)
plt.ylabel('ylabel', fontsize=fonts)
plt.title("figure 2", fontsize=fonts)
plt.legend(loc='upper left', fontsize=fonts)
plt.grid(True)
plt.tight_layout(pad=0.3)

# 绘制第二个子图
plt.subplot(122)
plt.scatter(x2, y2, s=8, c='b', label="scatter", marker='*')
plt.xlim(0, 1)
plt.ylim(0, 1)
plt.xlabel('xlable', fontsize=fonts)
plt.ylabel('ylabel', fontsize=fonts)
plt.title("figure 3", fontsize=fonts)
plt.legend(loc='upper left', fontsize=fonts)
plt.grid(True)
plt.tight_layout(pad=0.3)

# 4. 显示并保存画布
plt.savefig("./2.tiff")
plt.show()

3. 图片展示

二 面对对象绘图(主要操作Figure和Axes对象)(推荐)

理解了matplotliblib包里面的Figure和Axes对象, 绘制漂亮的科研图就能够运用自如、得心应手

1. 主要分为四个步骤:

(1) 准备数据;(2) 创建Figure和Axes对象;(3) 进行线条、散点图、坐标轴绘制和描述;(4) 显示画布。

2. 代码实例(单图和多图)

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

# 1. 准备数据
np.random.seed(19680801)
N, fonts = 50, 8
x1 = np.linspace(0, 2*np.pi, num=1000)
y1 = 0.2*np.sin(x1*10) + 0.75
yy1 = 0.15*np.sin(x1*10+0.2) + 0.25
x2, y2 = np.random.rand(N), np.random.rand(N)

# 2. 创建一个Figure对象和一个Axes对象
fig, ax = plt.subplots(figsize=(6, 4), dpi=300)

# 3 进行线条、散点图、坐标轴绘制和描述
ax.plot(x1, y1, color='g', linewidth=2, label="line 1")
ax.plot(x1, yy1, color='r', linewidth=2, label="line 2")
ax.scatter(x2, y2, s=8, c='b', label="scatter", marker='*')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.set_xlabel('xlable', fontsize=fonts)
ax.set_ylabel('ylabel', fontsize=fonts)
xticklabels = np.arange(0, 1.2, 0.2)
yticklabels = np.arange(0, 1.2, 0.2)
ax.set_xticks(xticklabels)
ax.set_yticks(yticklabels)
ax.set_xticklabels(labels=[str(round(xx, 1)) for xx in xticklabels], fontdict={'fontsize': fonts})
ax.set_yticklabels(labels=[str(round(xx, 1)) for xx in yticklabels], fontdict={'fontsize': fonts})
ax.set_title("figure 4", fontsize=fonts)
ax.legend(loc='upper left', fontsize=fonts)
ax.grid(True)

fig.tight_layout(pad=0.3)
fig.savefig("./3.tiff")

# 4.显示画布
plt.show()
###############################################################################
# 1.准备数据(参上)

# 2.创建一个Figure对象和两个Axes对象
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(6, 3), dpi=300)

# 3.对轴描述
# 对轴一描述
ax[0].plot(x1, y1, color='g', linewidth=2, label="line 1")
ax[0].plot(x1, yy1, color='r', linewidth=2, label="line 2")
ax[0].set_xlim(0, 1)
ax[0].set_ylim(0, 1)
ax[0].set_xlabel('xlable', fontsize=fonts)
ax[0].set_ylabel('ylabel', fontsize=fonts)
ax[0].set_title("figure 2", fontsize=fonts)
ax[0].legend(loc='upper left', fontsize=fonts)
ax[0].set_title("figure 5", fontsize=fonts)
ax[0].grid(True)

# 对轴二描述
ax[1].scatter(x2, y2, s=8, c='b', label="scatter", marker='*')
ax[1].set_xlim(0, 1)
ax[1].set_ylim(0, 1)
ax[1].set_xlabel('xlable', fontsize=fonts)
ax[1].set_ylabel('ylabel', fontsize=fonts)
ax[1].set_title("figure 3", fontsize=fonts)
ax[1].legend(loc='upper left', fontsize=fonts)
ax[1].set_title("figure 6", fontsize=fonts)
ax[1].grid(True)

fig.tight_layout(pad=0.3)
fig.savefig("./4.tiff")
# 4.显示画布
plt.show()

3. 图片展示

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值