Matplotlib的详细介绍
0、线条颜色,标记形状,线形
字符 | 含义 |
---|---|
b,g,r,y,k,w | 蓝色,绿色,红色,黄色,黑色,白色 |
o,*,+,x | 标记形状:圆形,*型,+型,x型 |
-,–,-. , : | 实线,虚线,点实线,点线 |
1、线型图
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
np.random.seed(10)
a = np.random.randn(30)
b = np.random.randn(30)
c = np.random.randn(30)
plt.plot(a,'r--o',b,'g-*',c,'y-.+')
#a:红色,虚线,圆点
#b:绿色,实线,*型
#c:黄色,点实线,+型
2、加标签
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
np.random.seed(10)
x = np.random.rand(30)
y = np.random.rand(30)
plt.title("example") # 图的名称
plt.xlabel("ni") #横坐标轴的名称
plt.ylabel("wo") # 纵坐标轴的名称
X,=plt.plot(x,'r--o')
Y,=plt.plot(y,'g--*') #规定线条类型,颜色
plt.legend([X,Y],["X","Y"]) #线条旁边的注释
3、子图
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
np.random.seed(10)
a = np.random.randn(30)
b = np.random.randn(30)
c = np.random.randn(30)
d = np.random.randn(30)
fig = plt.figure()
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)
A, = ax1.plot(a,'r--o')
ax1.legend([A],["A"])
B, = ax2.plot(b,'g:*')
ax2.legend([B],["B"])
C, = ax3.plot(c,'y-+')
ax3.legend([C],["C"])
D, = ax4.plot(a,'b-.o')
ax4.legend([D],["D"])
4、散点图
绘制散点图时,x和y的长度要相同,即值是(a,b)类型
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
np.random.seed(10)
x = np.random.rand(30)
y = np.random.rand(30)
plt.scatter(x,y,c='g',marker='*',label='(X,Y)') #c是颜色,marker是点的形状,label是图例注释
plt.title("example")
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
5、直方图
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
np.random.seed(10)
x = np.random.randn(1000)
plt.hist(x,bins=20,color='g',label='dd')
plt.title("example")
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.show()
6、饼图
import matplotlib.pyplot as plt
labels = ['ni','ta','wo']
sizes = [15,20,65]
#explode:是对应区域的偏移量,autopct将sizes中的数以浮点精度表示
plt.pie(sizes,explode=(0,0,0.1),labels=labels,autopct='%1.1f%%',startangle=90)
plt.axis('equal')#必不可少,使X与Y轴保持一致只有这样才能使饼图是圆形
plt.show()
致谢
[1] 深度学习之pytorch实战计算机视觉