matplotlib多纵轴_一份简短的Matplotlib教程

7b5ebb98305a25d854908684ae26e28c.png
Yunfan REN 
renyunfan@outlook.com

------------------------------------2020/11/08更新---------------------------------

最近发现了一个python库,可以自动帮你调整matplotlib的画图风格,喜欢的读者可以去试试

https://github.com/garrettj403/SciencePlots​github.com

------------------------------------------原内容------------------------------------

受到制裁的影响,工大被BAN了MATLAB,最近又要写论文,于是不得不速成了一手Matplotlib,这里留下以下笔记送给和我一样遭遇的人。

Overview - Matplotlib 3.3.2 documentation​matplotlib.org

大体上来讲Matplotlib中的各个函数功能都和Matlib大体一致,现在我们分两个大模块讲解一下。Overview - Matplotlib 3.3.2 documentation大体上来讲Matplotlib中的各个函数功能都和Matlib大体一致,现在我们分两个大模块讲解一下。

1 利用Matplotlib进行2D绘图

首先要导入Matplotlib库

import matplotlib as mpl
import matplotlib.pyplot as plt

我们大多数使用的函数都来自于子模块,我们取别名为plt

为了测试方便,我们先利用numpy创建一组数据,分别是一条sin和一条cos曲线

t = np.linspace(0,11,num=100)
data = np.sin(t)
data2 = np.cos(t)

随后我们利用简单的plt函数进行绘图

plt.plot(t , data, color='coral', label="Estimated of $x$")
plt.plot(t , data2, 'b', label="Ground truth of $x$", linestyle="dashed")
plt.legend(loc='upper right', fontsize=8)
# plt.xlim((-0.38,0.27))
# plt.ylim((-0.38,0.27))
plt.xlabel("Time/(s)", fontsize=10)
plt.ylabel("Position/(m)", fontsize=10)

这个函数常用的参数就包括

绘图:plt.plot(横轴,纵轴,color="色彩", label="标签", linestyle="线形")
绘制图例: plt.legend()
绘制标签: plt.xlable()
设置坐标轴范围:plt.ylim((-0.38,0.27))

绘制后的图像如下

32d78245ea9dc2742023ae19496dc981.png

当然大家知道Matlab绘图还有一个很常用的功能subplot,这个特性也在Matplotlib中杯完全继承了下来。

t = np.linspace(0,11,num=100)
data = np.sin(t)
data2 = np.cos(t)
data3 = np.cos(t) + np.random.rand((t.shape[0]))
plt.subplot(3,1,1)
plt.plot(t , data, color='coral', label="Estimated of $x$")
plt.legend(loc='upper right', fontsize=8)
plt.xlabel("Time/(s)", fontsize=10)
plt.ylabel("Position/(m)", fontsize=10)

plt.subplot(3,1,2)
plt.plot(t , data2, 'b', label="Ground truth of $x$", linestyle="dashed")
plt.legend(loc='upper right', fontsize=8)
plt.xlabel("Time/(s)", fontsize=10)
plt.ylabel("Position/(m)", fontsize=10)

plt.subplot(3,1,3)
plt.plot(t , data3, 'b', label="Ground truth of $x$", linestyle="dashed")
plt.legend(loc='upper right', fontsize=8)
plt.xlabel("Time/(s)", fontsize=10)
plt.ylabel("Position/(m)", fontsize=10)
plt.show()

fc959bfd73c13067a86eae6f2b800c38.png

2 利用Matplotlib进行3D绘图

当然在机器人领域我们经会用到3D绘图进行机器人轨迹的展示,这里我给出一个例程来绘制我们实验室的一个轨迹。

我觉得比较重要的功能有

  • 设置画幅大小 fig = plt.figure(figsize=(9, 7))
  • 设置初始3D视角 ax.view_init(elev=50, azim=-55)
  • 设置坐标轴范围 ax.set_zlim3d(-1, 1)

以及提供一个小函数给大家,实现3D图的各个轴等比例

def set_axes_equal(ax):
    '''Make axes of 3D plot have equal scale so that spheres appear as spheres,
    cubes as cubes, etc..  This is one possible solution to Matplotlib's
    ax.set_aspect('equal') and ax.axis('equal') not working for 3D.

    Input
      ax: a matplotlib axis, e.g., as output from plt.gca().
    '''
    x_limits = ax.get_xlim3d()
    y_limits = ax.get_ylim3d()
    z_limits = ax.get_zlim3d()

    x_range = abs(x_limits[1] - x_limits[0])
    x_middle = np.mean(x_limits)
    y_range = abs(y_limits[1] - y_limits[0])
    y_middle = np.mean(y_limits)
    z_range = abs(z_limits[1] - z_limits[0])
    z_middle = np.mean(z_limits)

    # The plot bounding box is a sphere in the sense of the infinity
    # norm, hence I call half the max range the plot radius.
    plot_radius = 0.5*max([x_range, y_range, z_range])

    ax.set_xlim3d([x_middle - plot_radius, x_middle + plot_radius])
    ax.set_ylim3d([y_middle - plot_radius, y_middle + plot_radius])
    ax.set_zlim3d([z_middle - plot_radius, z_middle + plot_radius])
data_raw = np.loadtxt("./fout_nrsl.txt")
data1 = data_raw[:,0:3]
data2 = data_raw[:,3:6]
fig = plt.figure(figsize=(9, 7))
lineWidth = 1
ax = fig.gca(projection='3d')
ax.plot(data1[:,0], data1[:,1], data1[:,2],color = 'coral', label='Estimated',linewidth = lineWidth)
ax.plot(data2[:,0],data2[:,1],data2[:,2],'b', label='Ground Truth',linestyle="dashed", linewidth = lineWidth)
ax.set_xlabel('$X$', fontsize=15, rotation=0)
ax.set_ylabel('$Y$',fontsize=15, rotation=0)
ax.set_zlabel('$Z$', fontsize=15, rotation=0)
# ax.set_zlabel(r'$gamma$', fontsize=30, rotation=60)
ax.legend(fontsize=10)
ax.view_init(elev=50, azim=-55)
# set_axes_equal(ax)
ax.set_zlim3d(-1, 1)  # viewrange for z-axis should be [-4,4]
ax.set_ylim3d(-0.5, 2.5)  # viewrange for y-axis should be [-2,2]
ax.set_xlim3d(0, 4)
plt.savefig("part1.pdf")
plt.show()

dd3fea83d568e7beaa73d78563db9c9e.png

好了今天就先到这里,大概的方法也就这么多,以后有用到再更新吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值