plt.plot()函数是matplotlib.pyplot模块下的一个函数, 用于画图
它可以绘制点和线, 并且对其样式进行控制. 由浅入深介绍如下
1.plt.plot(x, y)
1.1 x为x轴数据, y为y轴数据
import matplotlib.pyplot as plt
x=[3,4,5] # [列表]
y=[2,3,2] # x,y元素个数N应相同
plt.plot(x,y)
plt.show()
1.2 x, y可传入(元组), [列表], np.array, pd.Series
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
x=(3,4,5) # (元组)
y1=np.array([3,4,3]) # np.array
y2=pd.Series([4,5,4]) # pd.Series
plt.plot(x,y1)
plt.plot(y2) # x可省略,默认[0,1..,N-1]递增
plt.show() # plt.show()前可加多个plt.plot(),画在同一张图上
1.3 可传入多组x, y
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
x=(3,4,5)
y1=np.array([3,4,3])
y2=pd.Series([4,5,4])
plt.plot(x,y1,x,y2) # 此时x不可省略
plt.show()
1.4 x或y传入pd.DataFrame
1.4.1 x, y可以不等长, x短
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dic1={'x列0':[0,1,2],'x列1':[3,4,5]}
x=pd.DataFrame(dic1)
dic2={'y列0':[2,3,2],'y列1':[3,4,3],'y列2':[4,5,4],'y列3':[5,6,5]}
y=pd.DataFrame(dic2)
print(x)
print(y)
plt.plot(x,y)
plt.show()
x最短可为(元组), [列表], np.array, pd.Series
1.4.2 x, y可以不等长, x长
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dic1={'x列0':[0,1,2],'x列1':[3,4,5],'x列2':[6,7,8],'x列3':[9,10,11]}
x=pd.DataFrame(dic1)
dic2={'y列0':[2,3,2],'y列1':[3,4,3]}
y=pd.DataFrame(dic2)
print(x)
print(y)
plt.plot(x,y)
plt.show()
y最短可为(元组), [列表], np.array, pd.Series
1.5 x或y传入二维数组
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
lst1=[[0,1,2],[3,4,5],[6,7,8]]
x=np.array(lst1)
lst2=[[2,3,2],[3,4,3],[4,5,4]]
y=np.array(lst2)
print(x)
print(y)
plt.plot(x,y)
plt.show()
看不到点在哪里呀. 别急, 往下看.
2.plt.plot(x, y, "格式控制字符串")
点和线的格式可以用"格式控制字符串"设置
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
lst1=[[0,1,2],[3,4,5],[6,7,8]]
x=np.array(lst1)
lst2=[[2,3,2],[3,4,3],[4,5,4]]
y=np.array(lst2)
plt.plot(x,y,"ob:") #"b"为蓝色, "o"为圆点, ":"为点线
plt.show()
2.1 "格式控制字符串"最多可以包括三部分, "颜色", "点型", "线型"
2.1.1 "颜色"与"线型"
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
color=['b','g','r','c','m','y','k','w']
linestyle=['-','--','-.',':']
dic1=[[0,1,2],[3,4,5]]
x=pd.DataFrame(dic1)
dic2=[[2,3,2],[3,4,3],[4,5,4],[5,6,5]]
y=pd.DataFrame(dic2)
# 循环输出所有"颜色"与"线型"
for i in range(2):
for j in range(4):
plt.plot(x.loc[i],y.loc[j],color[i*4+j]+line_style[j])
plt.show()
如果只控制"颜色", 格式控制字符串还可以输入英文全称, 如"red", 甚至是十六进制RGB字符串, 如"#FF0000". python可用的"颜色"大全
2.1.2 "点型"
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
marker=['.',',','o','v','^','<','>','1','2','3','4','s','p','*','h','H','+','x','D','d','|','_','.',',']
dic1=[[0,1,2],[3,4,5],[6,7,8],[9,10,11],[12,13,14],[15,16,17]]
x=pd.DataFrame(dic1)
dic2=[[2,3,2.5],[3,4,3.5],[4,5,4.5],[5,6,5.5]]
y=pd.DataFrame(dic2)
# 循环输出所有"点型"
for i in range(6):
for j in range(4):
plt.plot(x.loc[i],y.loc[j],"b"+marker[i*4+j]+":") # "b"蓝色,":"点线
plt.show()
3.plt.plot(x, y, "格式控制字符串", 关键字=参数)
除了"格式控制字符串", 还可以在后面添加关键字=参数
import matplotlib.pyplot as plt
y=[2,3,2]
# 蓝色,线宽20,圆点,点尺寸50,点填充红色,点边缘宽度6,点边缘灰色
plt.plot(y,color="blue",linewidth=20,marker="o",markersize=50,
markerfacecolor="red",markeredgewidth=6,markeredgecolor="grey")
plt.show()
可选的关键字还有很多, 详情见文末
详解
本文讲解的函数定义为plt.plot(*args, **kwargs)
import matplotlib.pyplot as plt
help(plt.plot) # 查看英文函数定义
部分运行结果
*args, 可变位置参数, 以元组形式存放了很多无名参数.
**kwargs, 可变关键字参数, 以字典形式存放了很多关键字及参数. 调用时可传入
*args要放在**kwargs之前.
本函数的*args, 允许传入多对x和y和一个可选的"格式控制字符串".
本函数的**kwargs, 允许传入多个可选的关键字参数.