python用matplotlib画图(单个,多个子图)(颜色,线型,线粗细)

Target:

从文件中读取数据,用python画出对应的图,单个图以及多个子图的情况

1. 单个图多条线

从txt文件中读取数据,一列数据画一条线,一共六列,txt文件数据类型如下:

-0.009817 0.009817 -0.001963 -0.002700 0.005400 -0.002600
-0.007854 0.011781 0.000000 -0.002400 0.004900 -0.002400
...
...
...
-0.007854 0.009817 0.000000 -0.002200 0.004400 -0.002100

画图代码如下:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import os
from matplotlib.backends.backend_pdf import PdfPages

model_path = "/home/will/PycharmProjects/Plot_data/record.txt"
actual_wheel_R = []
actual_wheel_F = []
actual_wheel_L = []

desired_wheel_R = []
desired_wheel_F = []
desired_wheel_L = []

for line in open(model_path,"r"):
    num = 0
    print(type(line),line.split(' '))

    actual_wheel_R.append(np.float(line.split(' ')[0]))
    actual_wheel_F.append(np.float(line.split(' ')[1]))
    actual_wheel_L.append(np.float(line.split(' ')[2]))
    desired_wheel_R.append(np.float(line.split(' ')[3]))
    desired_wheel_F.append(np.float(line.split(' ')[4]))
    desired_wheel_L.append(np.float(line.split(' ')[5].split('\n')[0]))
    # vy.append(np.float(line.split(' ')[6]))
    # vz.append(np.float(line.split(' ')[7]))
    # comb_v.append(np.float(line.split(' ')[8].split('\n')[0]))

#print(len(comb_v))
#t = np.arange(0,99,1)
t = np.arange(0,len(actual_wheel_R),1)

#plt.plot(t, s, linewidth=3, label='S')

plt.plot(t, actual_wheel_R, '-o',linewidth=3, label='actual_wheel_R')
plt.plot(t, actual_wheel_F, '-^',linewidth=3, label='actual_wheel_F')
plt.plot(t, actual_wheel_L, linewidth=3, label='actual_wheel_L')

plt.plot(t, desired_wheel_R,'-o', linewidth=3, label='desired_wheel_R')
plt.plot(t, desired_wheel_F, '-^',linewidth=3, label='desired_wheel_F')
plt.plot(t, desired_wheel_L, linewidth=3, label='desired_wheel_L')
#plt.plot(t, comb_v, linewidth=3, label='comb_v')

#plt.tick_params(labelsize=16)

plt.legend(loc=1, fontsize = '20')

plt.ylabel('Velocity(m/10ms)', fontsize = 20) # 横坐标轴的标题
plt.xlabel('time(10ms)', fontsize = 20) # 纵坐标轴的标题
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)


plt.show()

效果如图所示:
impending

2. 多个子图

从txt文件中读取数据,一列数据画一条线,一共12列,前六列画一张子图,后六列画另一张子图,txt文件数据类型如下:

0.000000 0.009817 0.000000 0.000000 -0.000100 0.000000 0.005144 -0.003416 0.001060 0.001702 -0.000093 -0.001574
0.000000 0.009817 0.000000 0.000000 -0.000100 0.000000 0.005144 -0.003318 0.001060 0.001702 -0.000094 -0.001574
...
...
...
0.000000 0.007854 0.000000 0.000000 -0.000100 0.000000 0.005144 -0.003240 0.001060 0.001702 -0.000095 -0.001574

画图代码如下:

import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import os
from matplotlib.backends.backend_pdf import PdfPages

#my_font = matplotlib.font_manager.FontProperties(fname = '/usr/share/fonts/opentype/noto/simsun.ttc')
#model_path = '/home/will/catkin_ws/src/gazebo_mobile_manipulator/data/Record_sim_data_trans_v4_results.txt'
model_path = "/home/will/PycharmProjects/Plot_data/record.txt"
actual_wheel_R = []
actual_wheel_F = []
actual_wheel_L = []

desired_wheel_R = []
desired_wheel_F = []
desired_wheel_L = []

actual_ang_R = []
actual_ang_F = []
actual_ang_L = []

desired_ang_R = []
desired_ang_F = []
desired_ang_L = []

for line in open(model_path,"r"):
    num = 0
    print(type(line),line.split(' '))

    actual_wheel_R.append(np.float(line.split(' ')[0]))
    actual_wheel_F.append(np.float(line.split(' ')[1]))
    actual_wheel_L.append(np.float(line.split(' ')[2]))
    desired_wheel_R.append(np.float(line.split(' ')[3]))
    desired_wheel_F.append(np.float(line.split(' ')[4]))
    desired_wheel_L.append(np.float(line.split(' ')[5]))

    actual_ang_R.append(np.float(line.split(' ')[6]))
    actual_ang_F.append(np.float(line.split(' ')[7]))
    actual_ang_L.append(np.float(line.split(' ')[8]))
    desired_ang_R.append(np.float(line.split(' ')[9]))
    desired_ang_F.append(np.float(line.split(' ')[10]))
    desired_ang_L.append(np.float(line.split(' ')[11].split('\n')[0]))

#print(len(comb_v))
#t = np.arange(0,99,1)
t = np.arange(0,len(actual_wheel_R),1)

#plt.plot(t, s, linewidth=3, label='S')

# subplot(numRows, numCols, plotNum)
ax1 = plt.subplot(2,1,1)

ax2 = plt.subplot(2,1,2)

plt.sca(ax1)
plt.plot(t, actual_wheel_R, '-o',linewidth=3, label='actual_wheel_R')
plt.plot(t, actual_wheel_F, '-^',linewidth=3, label='actual_wheel_F')
plt.plot(t, actual_wheel_L, linewidth=3, label='actual_wheel_L')

plt.plot(t, desired_wheel_R,'-o', linewidth=3, label='desired_wheel_R')
plt.plot(t, desired_wheel_F, '-^',linewidth=3, label='desired_wheel_F')
plt.plot(t, desired_wheel_L, linewidth=3, label='desired_wheel_L')
#plt.plot(t, comb_v, linewidth=3, label='comb_v')

#plt.tick_params(labelsize=16)

plt.legend(loc=1, fontsize = '20')

plt.ylabel('Velocity(m/10ms)', fontsize = 20) # 横坐标轴的标题
plt.xlabel('time(10ms)', fontsize = 20) # 纵坐标轴的标题
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)

plt.sca(ax2)
plt.plot(t, actual_ang_R, '-o',linewidth=3, label='actual_ang_R')
plt.plot(t, actual_ang_F, '-^',linewidth=3, label='actual_ang_F')
plt.plot(t, actual_ang_L, linewidth=3, label='actual_ang_L')

plt.plot(t, desired_ang_R,'-o', linewidth=3, label='desired_ang_R')
plt.plot(t, desired_ang_F, '-^',linewidth=3, label='desired_ang_F')
plt.plot(t, desired_ang_L, linewidth=3, label='desired_ang_L')
#plt.plot(t, comb_v, linewidth=3, label='comb_v')

#plt.tick_params(labelsize=16)

plt.legend(loc=1, fontsize = '20')

plt.ylabel('Angle(Radian)', fontsize = 20) # 横坐标轴的标题
plt.xlabel('time(10ms)', fontsize = 20) # 纵坐标轴的标题
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)

plt.show()

效果如下图所示:
rows2


20211014 update

3. 颜色

只要在plot的命令中加入对应的颜色编号即可,如下

plt.plot(t, err_x, linewidth=3, label='err_x', color='g')

更多的颜色可以参照下表,表从另一篇blog中获得,感谢,具体可以看引用第一篇
colour

4. 线型

在plot指令中加入linestyle以及指定线型即可,如下,--是虚线:

plt.plot(t, nx, label='nx',linestyle='-')
plt.plot(t, nx_d, label='nx_d', linestyle='--')

linestyle

更多的线型可以参照下表,表从另一篇blog中获得,感谢,具体可以看引用第一篇
linestyle

5. 线粗细

在plot指令中加入linewidth以及线的宽度,如下

plt.plot(t, nx, linewidth=3, label='nx')
plt.plot(t, nx_d, linewidth=3, label='nx_d')

linewidth

Reference

  1. python画图(线条颜色、大小、类型:点、虚线等)(图文详细入门教程四):
    https://juejin.cn/post/6844904145032331272
  • 2
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值