plt.plot()函数解析(最清晰的解释)

欢迎关注WX公众号:【程序员管小亮】

plt.plot()函数用于对图形进行一些更改。

plt.plot(x, y, format_string, **kwargs) 

参数

  • x:x轴数据,列表或数组,可选
  • y:y轴数据,列表或数组
  • format_string:控制曲线的格式字符串,可选,由颜色字符、风格字符和标记字符组成。
颜色字符说明颜色字符说明
‘b’蓝色‘m’洋红色
‘g’绿色‘y’黄色
‘r’红色‘k’黑色
‘c’青绿色 cyan‘w’白色
‘#008000’RGB某颜色‘0.8’灰度值字符串
风格字符说明
‘-‘实线
‘–’破折线
‘-.’点划线
‘:’虚线
’ ’ ’ ‘无线条
标记字符说明标记字符说明
‘.’点标记‘1’下花三角标记
‘,’像素标记(极小点)‘2’上花三角标记
‘o’实心圈标记‘3’左花三角标记
‘v’倒三角标记‘4’右花三角标记
‘^’上三角标记’s’实心方形标记
‘>’右三角标记‘p’实心五角标记
‘<’左三角标记‘*’星形标记
‘h’竖六边形标记‘d’瘦菱形标记
‘H’横六边形标记‘|’垂直线标记
‘+’十字标记‘D’菱形标记
‘x’x标记
  • **kwargs:第二组或更多,(x,y,format_string)

常用的参数:

  • color:控制颜色,color=’green’
  • linestyle:线条风格,linestyle=’dashed’
  • marker:标记风格,marker = ‘o’
  • markerfacecolor:标记颜色,markerfacecolor = ‘blue’
  • markersize:标记尺寸,markersize = ‘20’

例子1:

使用数组在一个命令中绘制具有不同格式样式的多行。

import matplotlib.pyplot as plt
import numpy as np

# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()

在这里插入图片描述

例子2:

import matplotlib.pyplot as plt
import numpy as np

names = ['group_a', 'group_b', 'group_c']
values = [1, 10, 100]
plt.figure(figsize=(9, 3))
plt.subplot(131)
plt.bar(names, values)
plt.subplot(132)
plt.scatter(names, values)
plt.subplot(133)

plt.plot(names, values)
plt.suptitle('Categorical Plotting')
plt.show()

在这里插入图片描述

python课程推荐。
在这里插入图片描述

参考文章

import numpy as np import matplotlib.pyplot as plt from scipy.optimize import curve_fit #position plt.close('all') data=np.loadtxt('DATAA (1).txt',delimiter=',') t=data[:,0] x=data[:,1] t = t[130:790] x = x[130:790] plt.figure() plt.plot(t,x) plt.xlabel('time') plt.ylabel('position') max_val=max(x) max_i=list(x).index(max_val) #position up plt.figure() t_up=t[:max_i] x_up=x[:max_i] plt.plot(t_up,x_up,'r*') def fit1(t,v0,a1,x0): return x0+v0*t+0.5*a1*t**2 popt,pcov = curve_fit(fit1, t_up, x_up) plt.plot(t_up, fit1(t_up,*popt),'k', linewidth=2) #position down plt.figure() t_down=t[max_i:] x_down=x[max_i:] plt.plot(t_down,x_down,'r*') popt,pcov = curve_fit(fit1, t_down, x_down) plt.plot(t_down, fit1(t_down,*popt),'k', linewidth=2) #velocity n1=20 data=[] delta=t[1]-t[0] for i in range (n1,len(t)-n1): deri=(x[i+n1]-x[i-n1])/(2*n1*delta) data.append(deri) v=np.array(data) t= t[n1:-n1] plt.figure() plt.plot(t,v,'r*') #velocity up plt.figure() t_up=t[:max_i-n1] v_up=v[:max_i-n1] plt.plot(t_up,v_up,'r*') def fit2(t,v0,a): return v0+a*t popt,pcov = curve_fit(fit2, t_up, v_up) plt.plot(t_up, fit2(t_up,*popt),'k', linewidth=2) #velocity down plt.figure() t_down=t[max_i-n1:] v_down=v[max_i-n1:] plt.plot(t_down,v_down,'r*') popt,pcov = curve_fit(fit2, t_down, v_down) plt.plot(t_down, fit2(t_down,*popt),'k', linewidth=2) #acceleration n2=2 data2=[] for i in range (n2,len(v)-n2): deri=(v[i+n2]-v[i-n2])/(2*n2*delta) data2.append(deri) a=np.array(data2) t= t[n2:-n2] plt.figure() plt.plot(t,a,'r*') import statistics a_up_mean=statistics.mean(a[:max_i-n1-n2]) a_down_mean=statistics.mean(a[max_i-n1-n2:])出现这个错误ValueError: could not convert string to float: '0.008\t-1.2126E-4'
05-22
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是管小亮

一口吃掉你的打赏,嗝~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值