我們創建兩個視圖窗口,分別在其中繪製圖像
首先我們不對x和y進行limit
也不添加座標軸標籤
代碼如下:
import numpy as np
import matplotlib.pyplot as plt
fig_1 = plt.figure()
fig_2 = plt.figure()
ax_1 = fig_1.gca(projection="3d")
ax_2 = fig_2.gca(projection="3d")
th = np.linspace(1, 100, 1000)
x = np.cos(th)
y = np.log(th)
z = np.sin(th)
#這裏改變x和y的參數位置,來區分兩個圖像
ax_1.plot(x, y, z, label="logx", color="r")
ax_2.plot(y, x, z, label="logy")
ax_1.legend()
ax_2.legend()
plt.show()
我們觀察各個軸的範圍可以知道,python將會根據圖像的情況自動匹配軸的範圍。
那麼接下來我們自己設置:
(1)x軸和y軸的範圍
(2)添加軸標籤
首先我們嘗試:
ax_1.plot(x, y, z, label="logx", color="r")
plt.xlim(-8.0, 8.0)
plt.ylabel("this is y label")
————————————————————————————————————————————————————
ax_2.plot(y, x, z, label="logy")
plt.xlim(-8.0, 8.0)
plt.xlabel("this is x label")
一開始想的是,按照代碼執行順序,應該對第一個圖像設置y軸標籤,對第二個圖像設置x軸的標籤。並分別擴大其x軸的範圍。
但實際上運行後如下
所有的操作全部加到了第二個圖像上!!!我也不知道爲什麼。。
請教大佬後,才知道諸如plt.xlabel()
之類的方法,只會對當前figure進行操作
如果要特殊指定figure,應該這樣寫
ax_1.plot(x, y, z, label="logx", color="r")
ax_2.plot(y, x, z, label="logy")
ax_1.set_xlabel("this is x label")
ax_2.set_ylabel("this is y label")
把plt.xlabel()變成了ax_1.set_xlabel()。
直接對指定對象進行操作,這樣就可以避免之前的情況
第一種寫法叫做函數式
第二種寫法叫做對象式
ylim和xlim也是如此寫法。
這樣設置圖像屬性就方便多了。