点击上方“码农的后花园”,选择“星标” 公众号
精选文章,第一时间送达
来源:https://zhuanlan.zhihu.com/p/258106097
plt.plot()函数是matplotlib.pyplot模块下的一个函数, 用于画图它可以绘制点和线, 并且对其样式进行控制。
我们经常用这个模块绘制神经网络模型训练和验证时绘制各种变化曲线,以查看训练情况,下面由浅入深对其介绍和绘制各种点线图、折线图、曲线图等。
plt.plot()函数介绍
图线绘制
import matplotlib.pyplot as plt
x=[3,4,5] # [列表]
y=[2,3,2] # x,y元素个数N应相同
plt.plot(x,y)
plt.show()
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(),画在同一张图上
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.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
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()
点和线的格式可以用"格式控制字符串"设置
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.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()
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()
除了"格式控制字符串", 还可以在后面添加关键字=参数
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()
往期精彩分享:
论文查找: arXiv,论文阅读:知云文献翻译, 完美组合 !
什么是a站、b站、c站、d站、e站、f站、g站、h站、i站、j站、k站、l站、m站、n站…z站?
更多优质内容?等你点在看