import matplotlib.pyplot as plt
import numpy as np
def y_subplot(x,i):
return np.cos(i * np.pi *x)
x = np.linspace(1, 100, num= 25, endpoint = True)
#使用subplots 画图
f, ax = plt.subplots(2,2)
#type(f) #matplotlib.figure.Figure
style_list = ["g+-", "r*-", "b.-", "yo-"]
ax[0][0].plot(x, y_subplot(x, 1), style_list[0])
ax[0][1].plot(x, y_subplot(x, 2), style_list[1])
ax[1][0].plot(x, y_subplot(x, 3), style_list[2])
ax[1][1].plot(x, y_subplot(x, 4), style_list[3])
plt.show()
# 参数仅仅为一个列表的时候表示图中点的Y坐标列表
# X坐标是0-3,纵坐标是1-4(即参数[1, 2, 3, 4])
# 这是因为如果你只提供给plot()函数一个列表或数组,matplotlib会认为这是一串Y值(Y向量),并且自动生成X值(X向量)。
# 而Python一般是从0开始计数的,所以X向量有和Y向量一样的长度(此处是4),但是是从0开始,所以X轴的值为[0,1,2,3]。
plt.plot([1, 2, 3, 4])
plt.ylabel('some numbers 1')
plt.show()
# 下面的例子和上面一个意思
plt.plot([0, 1, 2, 3], [1, 2, 3, 4])
plt.ylabel('some numbers 2')
plt.show()
# X坐标是0-3,纵坐标是2,2,3,4(即参数[2, 2, 3, 4])
plt.plot([2, 2, 3, 4])
plt.ylabel('some numbers')
plt.show()
# 使用格式字符串
# o表示圆标记 b表示蓝色
x = np.arange(1, 11)
y = 2 * x + 5
plt.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x, y, "ob")
plt.show()
# 计算正弦曲线上点的 x和 y坐标
x = np.arange(0, 3 * np.pi, 0.1) # 步长为0.1
y = np.sin(x)
plt.title("sine wave form")
# 使用 matplotlib 来绘制点
plt.plot(x, y)
plt.show()
t = np.arange(0., 5., 0.2)
plt.plot(t, t, 'r--', t, t ** 2, 'bs', t, t ** 3, 'g^')
plt.show()
#生成条形图
x = [5,8,10]
y = [12,16,6]
x2 = [6,9,11]
y2 = [6,15,7]
plt.bar(x, y, align = 'center')
plt.bar(x2, y2, color = 'g', align = 'center')
plt.title('Bar graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
#直方图
a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
plt.hist(a, bins = [0,20,40,60,80,100])
plt.title("histogram")
plt.show()