极简编程,极简生活,简到极致,就是完美!
Python数据分析基础第二课—绘图库Matplotlib,绘制常用的直方图、折线图、饼图…… Lets go !
# 导入
from matplotlib import pyplot as plt
import numpy as np
# 1.方程直线图
x = np.arange(0, 11, 1)
y = 2*x + 1
plt.title(" y=2x+1 ")
plt.xlabel("x alias")
plt.ylabel("y alias")
plt.plot(x, y)
plt.show()
# 2.点图
x = np.arange(0, 11, 1)
y = x+1
plt.title(" y=x+1 ")
plt.xlabel("x alias")
plt.ylabel("y alias")
plt.plot(x, y, 'ob')
plt.show()
# 3.正弦波图
x3 = np.arange(0, 3 * np.pi, 0.1)
y3 = np.sin(x3)
y32 = np.cos(x3)
# 一页多图 subplot()
plt.subplot(1, 2, 1)
plt.plot(x3, y3)
plt.title(" y=sin(x) ")
plt.subplot(2, 2, 2)
plt.title(" y=cos(x) ")
plt.plot(x3, y32)
plt.show()
# 4.直方图
arr4 = np.asarray([1, 11, 22, 33, 32, 45, 90, 100, 44,
76, 23, 2, 3, 44, 44, 44, 22, 11])
plt.hist(arr4)
plt.title(" hist ")
plt.show()
# 5.折线图
x5 = np.arange(1, 9, 1)
y5 = np.asarray([1, 2, 4, 7, 9, 15,4,2])
plt.plot(x5, y5, 'g-.')
plt.show()
# 6.饼图
plt.figure(figsize=(6, 6)) # 将画布设定为正方形
explode = [0.01, 0.01, 0.01] # 设定各项距离圆心n个半径
arr6 = np.asarray([2, 3, 5])
plt.pie(arr6, explode=explode, autopct='%1.1f%%') # 绘制饼图
plt.title('饼图')
plt.show()
这期到这里了,下期结构化数据分析库Pandas,下期见,Byebye!