https://matplotlib.org/stable/tutorials/introductory/usage.html#sphx-glr-tutorials-introductory-usage-py import matplotlib.pyplot as plt import numpy as np
fig, ax = plt.subplots() # 创建一个画布 ax.plot( [1, 2, 3, 4], [1, 4, 2, 3] ) 这个代码是 import matplotlib.pyplot as plt import numpy as np
plt.plot( [1,2,3,4], [1,4,2,3] ) 以上两个代码都是可以生成同样的图像 第二个代码对于matlab的使用者来说应该是熟悉的 在文档的开篇,学一个图形构成的元素很有必要 axs是轴的意思,就是在这个语境里面是坐标轴的意思 为了学习方便我进行了翻译,部分我翻译的不准 比如spines,这个意思的原意思为脊柱 那我这里就引申为骨架 部分有未标注的,这里补全 https://matplotlib.org/stable/api/axes_api.html#matplotlib.axes.Axes
import matplotlib.pyplot as plt import numpy as np
# plt.plot( [1, 2, 3, 4], [1, 4, 2, 3] # ) fig = plt.figure() # 一个无axe的空画布 fig, ax = plt.subplots() # 一个大图 fig, axs = plt.subplots(2, 2) # 2X2的子图 生成的图样
其实就是这么简单,而且这个过程是线性的,也是阻塞的。输入要“干净”,输出才有保障。所以我们的主题转入了->输入。 期望输入一个 数组或者是操作掩码数组 import numpy.ma as ma
import numpy as np x = np.array( [1, 2, 3, 5, 7, 4, 3, 2, 9, 0] ) print(x) mask = x < 5 mx = ma.array(x, mask=mask) print(mask) print(mx)
输出的结果 看第二个的方法 掩码数组具有三个属性:data、mask、fill_value; data表示原始数值数组, mask表示获得掩码用的布尔数组, fill_value表示的填充值替代无效值之>后的数组,该数组通过filled()方法查看; https://numpy.org.cn/reference/routines/ma.html https://numpy.org/doc/stable/reference/generated/numpy.array.html#numpy.array https://numpy.org/doc/stable/reference/generated/numpy.ma.masked_array.html#numpy.ma.masked_array https://github.com/numpy/numpy 在此之前安装一下pandas import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2, 100) print(x)
# Note that even in the OO-style, we use `.pyplot.figure` to create the figure. fig, ax = plt.subplots() # Create a figure and an axes. ax.plot(x, x, label='linear') # Plot some data on the axes. ax.plot(x, x**2, label='quadratic') # Plot more data on the axes... ax.plot(x, x**3, label='cubic') # 三次方 ax.set_xlabel('x label') # Add an x-label to the axes. ax.set_ylabel('y label') # Add a y-label to the axes. ax.set_title("Simple Plot") # 添加标题 ax.legend() # 添加图例 图像的结果 x = np.linspace(0, 10, 10) x = np.linspace(0, 10, 5) x = np.linspace(0, 5, 5) 注意这些步长的设置,可以让你的图更加的平缓 注意代码之前的序列生成器, 语法格式:
import numpy as np import matplotlib.pyplot as plt y = np.zeros(5) print(y) x1 = np.linspace(0, 10, 5) print(x1) x2 = np.linspace(0, 10, 5) print(x2) plt.plot(x1, y, 'o') plt.plot(x2, y + 0.5, 'o') plt.ylim([-0.5, 1]) plt.show() import numpy as np import matplotlib.pyplot as plt y = np.zeros(5) print(y) x1 = np.linspace(0, 10, 5) print(x1) x2 = np.linspace(0, 10, 5) print(x2) plt.plot(x1, y, 'o') plt.plot(x2, y + 0.5, 'o') plt.ylim([-0.5, 1]) plt.show() 注意linespace里面的取数 # import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2, 10) y = np.linspace(0, 10, 5)
print(x) print("--------------------------------------------------") print(y) 你看都是浮点数的输出 如果不想要最后的一个值,可以使用参数。 用关键字参数 numpy.linspace (start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0) 这个函数的参数 import numpy as np
p = np.array([[1, 2], [3, 4]]) print(p) q = np.array([[5, 6], [7, 8]]) print("--------------------------------------------------------") print(q) r = np.linspace(p, q, 3, axis=0) print("--------------------------------------------------------")
print(r)
s = np.linspace(p, q, 3, axis=1) print("--------------------------------------------------------")
print(s)
最后这里,留个小尾巴。这个参数我有点没有看懂 import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 5, 5) # print(x) # Note that even in the OO-style, we use `.pyplot.figure` to create the figure. fig, ax = plt.subplots() # Create a figure and an axes. ax.plot(x, x, label='linear') # Plot some data on the axes. ax.plot(x, x**2, label='quadratic') # Plot more data on the axes... ax.plot(x, x**3, label='cubic') # 三次方 ax.set_xlabel('x label') # Add an x-label to the axes. ax.set_ylabel('y label') # Add a y-label to the axes. ax.set_title("Simple Plot") # 添加标题 ax.legend() # 添加图例
|
|