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() # 創(chuàng)建一個(gè)畫布
ax.plot(
[1, 2, 3, 4],
[1, 4, 2, 3]
)
這個(gè)代碼是
import matplotlib.pyplot as plt
import numpy as np
plt.plot(
[1,2,3,4],
[1,4,2,3]
)
以上兩個(gè)代碼都是可以生成同樣的圖像
第二個(gè)代碼對(duì)于matlab的使用者來(lái)說(shuō)應(yīng)該是熟悉的
在文檔的開篇,學(xué)一個(gè)圖形構(gòu)成的元素很有必要
axs是軸的意思,就是在這個(gè)語(yǔ)境里面是坐標(biāo)軸的意思
為了學(xué)習(xí)方便我進(jìn)行了翻譯,部分我翻譯的不準(zhǔn)
比如spines,這個(gè)意思的原意思為脊柱
那我這里就引申為骨架
部分有未標(biāo)注的,這里補(bǔ)全
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() # 一個(gè)無(wú)axe的空畫布
fig, ax = plt.subplots() # 一個(gè)大圖
fig, axs = plt.subplots(2, 2) # 2X2的子圖
生成的圖樣
我們基礎(chǔ)知識(shí)就先說(shuō)這么多,我們來(lái)考慮更加現(xiàn)實(shí)的問(wèn)題。對(duì)于一個(gè)繪圖的流程怎么做?我這里給出自己的流程:
輸入
傳給繪圖單元
顯示
其實(shí)就是這么簡(jiǎn)單,而且這個(gè)過(guò)程是線性的,也是阻塞的。輸入要“干凈”,輸出才有保障。所以我們的主題轉(zhuǎn)入了->輸入。
期望輸入一個(gè)
數(shù)組或者是操作掩碼數(shù)組
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)
輸出的結(jié)果
看第二個(gè)的方法
掩碼數(shù)組具有三個(gè)屬性:data、mask、fill_value;
data表示原始數(shù)值數(shù)組,
mask表示獲得掩碼用的布爾數(shù)組,
fill_value表示的填充值替代無(wú)效值之>后的數(shù)組,該數(shù)組通過(guò)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") # 添加標(biāo)題
ax.legend() # 添加圖例
圖像的結(jié)果
x = np.linspace(0, 10, 10)
x = np.linspace(0, 10, 5)
x = np.linspace(0, 5, 5)
注意這些步長(zhǎng)的設(shè)置,可以讓你的圖更加的平緩
注意代碼之前的序列生成器, numpy.linspace()
函數(shù)用于在線性空間中以均勻步長(zhǎng)生成數(shù)字序列。
語(yǔ)法格式: array = numpy.linspace(start, end, num=num_points)
將在start
和end
之間生成一個(gè)統(tǒng)一的序列,共有num_points
個(gè)元素。
start -> Starting point (included) of the rangestart ->范圍的起點(diǎn)(包括)
end -> Endpoint (included) of the rangeend ->范圍的端點(diǎn)(包括)
num -> Total number of points in the sequencenum >序列中的總點(diǎn)數(shù)
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里面的取數(shù)
# 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)
你看都是浮點(diǎn)數(shù)的輸出
如果不想要最后的一個(gè)值,可以使用參數(shù)。
用關(guān)鍵字參數(shù)endpoint
,可以將其設(shè)置為False
。(默認(rèn)為True
)
numpy.linspace
(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
這個(gè)函數(shù)的參數(shù)
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)
最后這里,留個(gè)小尾巴。這個(gè)參數(shù)我有點(diǎn)沒(méi)有看懂
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") # 添加標(biāo)題
ax.legend() # 添加圖例
要?jiǎng)澐謝軸的分度值,這個(gè)值取決于你的要求,我這里只說(shuō)x軸,因?yàn)槲覀円恢闭J(rèn)為是x就是屬于自變量
接著就是要選擇你寫代碼的時(shí)候,具體的一個(gè)風(fēng)格。是面向?qū)ο驩O,還是傳統(tǒng)的matlab繪制法。如果是想對(duì)繪制的圖有一個(gè)全局的控制的,建議前者
接著就是調(diào)用一個(gè)最重要的plot進(jìn)行繪圖
接著就是對(duì)整體的圖形一些修飾和美化
聯(lián)系客服