Matplotlib库介绍
Matplotlib库:是Python优秀的数据可视化第三方库。
地址:Thumbnail gallery — Matplotlib 2.0.2 documentation
导入:
import matplotlib.pyplot as plt
使用:
plt.plot([3,1,4,5,2])
plt.ylabel("Grade")
plt.savefig('test',dpi=600) #plt.savefig()将输出图形存储为文件,默认PNG格式,可以通过dpi修改输出质量。
plt.show()
plot()函数
官方文档:matplotlib.pyplot.plot — Matplotlib 3.5.1 documentation
格式:
plt.plot(x, y, ls='-', lw=2, label='xxx', color='g' )
- x: x轴上的值
- y: y轴上的值
- ls:线条风格 (linestyle)
- lw:线条宽度 (linewidth)
- label:标签文本
matplotlib.pyplot 示例
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.5, 10, 1000)
y = np.cos(x)
plt.plot(x, y, ls='-', lw=2, label='cosine', color='purple')
plt.legend()
plt.xlabel('independent variable')
plt.ylabel('dependent variable')
plt.show()
pylab
#figure for 2D data
from pylab import plot, show
from sec import *
plot(data[target=='setosa',0],data[target=='setosa',2],'bo')#data来自一个数据集
plot(data[target=='versicolor',0],data[target=='versicolor',2],'r+')
plot(data[target=='virginica',0],data[target=='virginica',2],'g*')
show()
#figure for all 4D(4个维度) data, 同色一类,圈是花萼,加号花瓣
setosa_sepal_x = ssx = data[target == 'setosa', 0]
setosa_sepal_y = ssy = data[target == 'setosa', 1]
setosa_petal_x = spx = data[target == 'setosa', 2]
setosa_petal_y = spy = data[target == 'setosa', 3]
versicolor_sepal_x = vsx = data[target == 'versicolor', 0]
versicolor_sepal_y = vsy = data[target == 'versicolor', 1]
versicolor_petal_x = vpx = data[target == 'versicolor', 2]
versicolor_petal_y = vpy = data[target == 'versicolor', 3]
virginica_sepal_x = vgsx = data[target == 'virginica', 0]
virginica_sepal_y = vgsy = data[target == 'virginica', 1]
virginica_petal_x = vgpx = data[target == 'virginica', 2]
virginica_petal_y = vgpy = data[target == 'virginica', 3]
plot(ssx, ssy, 'bo', spx, spy, 'b+')
plot(vsx, vsy, 'ro', vpx, vpy, 'r+')
plot(vgsx, vgsy, 'go', vgpx, vgpy, 'g+')
show()