Matplotlib入门到精通
1.先安装Matplotlib的库
pip install Matplotlib
Matplotplot是一个绘图库,在绘制2D图上非常方便
2.使用方法
import matplotlib.pyplot as plt
import numpy as np
# 1.绘制曲线图
x = np.arange(0,4*np.pi,0.1) #START,STOP,STEP
y = np.sin(x)
z = np.cos(x)
plt.plot(x,y,x,z)
plt.show()
# 2.绘制实点图
x = np.array([1,3,5,7])
y = np.array([1,2,3,4])
plt.plot(x,y,'o')
plt.show()
# 3.使用Matplotlib 绘图标记
xpoints = np.array([1,2,10,4,5,6,7,8,9,10,11,12,13])
ypoints = np.array([1,3,4,5,8,9,6,1,3,4,5,2,4])
plt.plot(xpoints,ypoints, marker = 'D')# marker可以更换参数,定义不同类型的标记点
plt.show()
4.定义不同颜色的折线图
ypoints = np.array([6, 2, 13, 10])
plt.plot(ypoints, color = 'r') #通过color属性来设置折现颜色
plt.show()
# 5.设置轴标签和标题
x = np.array([1,2,3,4])
y = np.array([1,4,9,16])
plt.plot(x,y)
plt.title("view") #设置标题
plt.xlabel("x-label") #设置轴标签
plt.ylabel("y-label")
plt.show()
# 6.Matplotlib网格线
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])
plt.title("RUNOOB grid() Test")
plt.xlabel("x - label")
plt.ylabel("y - label")
plt.plot(x, y,marker='o')
plt.grid(color='r',linestyle='--',axis='x',linewidth= 0.5) #设置网格图
plt.show()
# 7.Matplotlib绘制多图
xpoints = np.array([0, 6])
ypoints = np.array([0, 100])
plt.subplot(1, 2, 1) #定义一行二列,第三个参数表示(1,1)
plt.plot(xpoints,ypoints)
plt.title("plot 1")
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])
plt.subplot(1, 2, 2) #定义一行二列,第三个参数表示(1,2)
plt.plot(x,y)
plt.title("plot 2")
plt.suptitle("RUNOOB subplot Test") #父亲标题
plt.show()
-----绘制多图在深度学习上预测展示用的比较多
# 8.绘制Matplotlib散点图
x = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([1, 4, 9, 16, 7, 11, 23, 18])
colors = np.array(["red","green","black","orange","purple","beige","cyan","magenta"])
plt.scatter(x, y, c=colors)
plt.show()
# 9.Matplotlib 柱形图
# 9.1竖直方向的bar图
x = np.array(["Runoob-1", "Runoob-2", "Runoob-3", "C-RUNOOB"])
y = np.array([12, 22, 6, 18])
plt.bar(x,y)
plt.show()
# 9.2垂直方向的图
x = np.array(["Runoob-1", "Runoob-2", "Runoob-3", "C-RUNOOB"])
y = np.array([12, 22, 6, 18])
plt.barh(x,y,height=0.1) #bar()方法使用width设置,barh()是用height设置的
# 10.Matplotlib 饼图
y = [35, 25, 25, 15]
plt.pie(y,labels=['A','B','C','D'],colors=["#d5695d", "#5d8ca8", "#65a479", "#a564c9"])
plt.title("RUNOOB Pie Test") # 设置标题
plt.show()
3.运行截图,懒得截图了,我是懒狗
本文来自博客园,作者:TCcjx,转载请注明原文链接:https://www.cnblogs.com/tccjx/articles/16481187.html