import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots() # Create a figure containing a single axes.
plt.plot([1, 2, 3, 4], [1, 4, 2, 3]) # Plot some data on the axes.
fig = plt.figure() # an empty figure with no Axes
fig, ax = plt.subplots() # a figure with a single Axes
fig, axs = plt.subplots(2, 2) # a figure with a 2x2 grid of Axes
plt.show()
a = np.linspace(1,5,5, endpoint=False)
b = np.linspace(1,5,5, endpoint=True)
print(a )
print(b)
X = np.linspace(-np.pi, np.pi, 256,endpoint=True) #endpoint=True(默认),包含stope的数字
C,S = np.cos(X), np.sin(X)
plt.plot(X,C)
plt.plot(X,S)
plt.show()
# 创建一个 8 * 6 点(point)的图,并设置分辨率为 80
plt.figure(figsize=(8,6), dpi=80)
# 创建一个新的 1 * 1 的子图,接下来的图样绘制在其中的第 1 块(也是唯一的一块)
plt.subplot(1,1,1) #subplot()行;列;第几个
X = np.linspace(-np.pi, np.pi, 256,endpoint=True)
C,S = np.cos(X), np.sin(X)
# 绘制余弦曲线,使用蓝色的、连续的、宽度为 1 (像素)的线条
plt.plot(X, C, color="blue", linewidth=1.0, linestyle="-")
# 绘制正弦曲线,使用绿色的、连续的、宽度为 1 (像素)的线条
plt.plot(X, S, color="green", linewidth=1.0, linestyle="-")
# 设置横轴的上下限
plt.xlim(-4.0,4.0)
# plt.show()
# 设置横轴记号
plt.xticks(np.linspace(-4,4,9,endpoint=True))
# plt.show()
# 设置纵轴的上下限
plt.ylim(-1.0,1.0)
# 设置纵轴记号
plt.yticks(np.linspace(-1,1,5,endpoint=True))
# plt.show()
# 以分辨率 72 来保存图片
plt.savefig("exercice_2.png",dpi=72)
# 在屏幕上显示
plt.show()
n = 256
X = np.linspace(-np.pi,np.pi,endpoint = True)
Y = np.sin(2*X)
plt.plot(X,Y+1,color = 'yellow',alpha = 1)
plt.plot(X,Y-1,color = 'blue',alpha = 1)
plt.show()
n = 1024
X = np.random.normal(0,1,n)
Y = np.random.normal(0,1,n)
#正态分布numpy.random.normal(loc=0,scale=1e-2,size=shape),从左到右分别是均值,标准差,输出的值赋在shape里,默认为None。
plt.scatter(X,Y)
plt.show()
plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
plt.axis([0, 6, 0, 20]) #x轴0-6,y轴0-20
plt.show()
t = np.arange(0., 5., 0.2)
# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g-')
plt.show()
官网:
https://matplotlib.org/3.3.4/index.html