python pyplot的plot( )函数

函数原型

plt.plot(x,y,format_string,**kwargs)
  • x:x轴数据,列表或数组,可选
  • y:y轴数据,列表或数组
  • format_string:控制曲线的格式字符串,可选
  • **kwargs:其实就是可以画多条曲线的意思,追加(,x,y,format_string)就可以了

1、有无 x 轴数据示例

import numpy as np
import matplotlib.pyplot as plt

fig1 = plt.figure(num=1,figsize=(7,5))
x = np.linspace(0.0,np.pi*2,12)
y = np.sin(x)
plt.plot(x,y,'go:')     # 指定x轴数据
fig2 = plt.figure(num=2)
plt.plot(y,'rx-')       # 无x轴数据
plt.show()

在这里插入图片描述
无 x 轴数据则默认把 y 轴数据的下标当成 x 轴

2、画多条曲线

import numpy as np
import matplotlib.pyplot as plt

fig1 = plt.figure(num=1,figsize=(7,5))
x = np.linspace(0.0,np.pi*2,20)
y = np.sin(x)
plt.plot(x, y,'rx-', x,2*x,'go-.')     # 每条都指定x轴数据
fig2 = plt.figure(num=2)
plt.plot(x, y,'rx-',2*x,'go-.')       # 一条指定x轴数据,其他不指定
fig2 = plt.figure(num=3)
plt.plot(y,'rx-',2*x,'go-.')     # 都不指定
plt.show()

figure1

figure1
figure2

在这里插入图片描述

figure3
在这里插入图片描述
3、format_string:控制曲线的格式字符串,可选,由颜色字符、风格字符和标记字符组成。

颜色字符 说明 颜色字符 说明 颜色字符 说明
‘r’ 红色 ‘g’ 绿色 ‘b’ 蓝色
‘c’ 青绿色 ‘k’ 黑色 ‘y’ 黄色
‘w’ 白色 ‘m’ 洋红色
风格字符 说明
‘-‘ 实线
‘–’ 破折线
‘-.’ 点画线
‘:’ 虚线
标记字符 说明 标记字符 说明 标记字符 说明
‘.’ 点标记 ‘,’ 像素标记 ‘o’ 实心圈标记
‘v’ 倒三角标记 ‘^’ 上三角标记 ‘>’ 右三角标记
‘<’ 左三角标记 ‘h’ 竖六边形标记 ‘H’ 横六边形标记
‘+’ 十字标记 ‘x’ x标记 ‘D’ 菱形标记
‘d’ 瘦菱形标记 ‘|’ 垂直线标记 ‘*’ 星形标记
‘p’ 实心五角标记 ’s’ 实心方形标记 ‘4’ 右花三角标记
‘3’ 左花三角标记 ‘2’ 上花三角标记 ‘1’ 下花三角标记

使用方式:

format_string 是三个的组合

plt.plot(x, y,'rx-') 
plt.plot(x, y,'go-.') 

或者只有其中两个或一个

plt.plot(x, y,'w:') 
plt.plot(x, y,'y') 

都可以

或者说其他方式控制线性颜色

color : 控制颜色,color=’green’

linestyle : 线条风格,linestyle=’dashed’

marker : 标记风格,marker = ‘o’

markerfacecolor : 标记颜色,markerfacecolor = ‘blue’

markersize : 标记尺寸,markersize = ‘20’

使用方式

plt.plot(x, y,color='green')

在这里插入图片描述

plt.plot(x, y,color='green',marker = 'o',markerfacecolor = 'blue') 

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43657442/article/details/108246191