首先,畫圖需要生成一個圖(可理解爲畫布或進程),此參數可以無需設置,如未設置 pyplot 會自動幫你創建一個。
接下來,要決定在圖中哪個位置畫圖,畫多大的圖。這就引入座標點和大小的概念,整個圖中按照 X 與 Y軸橫豎來平均切分,以 0 到 1 之間的數值來表示。 如: X 軸上的 0.1,代表了 X 軸總長自左向右的 10%位置。
整個畫圖區域就是一個軸,通過軸參數選項可以對畫圖區域的座標點與大小進行設置,如未設置會自動幫接近覆蓋整個圖中的值。在一個圖中可添加多個軸,就如一個畫布中可以畫一整副圖,又或者可以畫四宮圖等等。軸參數設置如下:
軸([x,y,xs,ys])#其中 x 代表在 X 軸的位置, y 代表在 Y 軸的位置, xs 代表在 X 軸上向右延展的範圍大小, yx 代表在 Y 軸中向上延展的範圍大小。
注意:軸參數調整的範圍僅包括如下圖紅線內範圍,外圍的刻度數值並不在之內。如設置爲軸([0,0,1,1])將無法正確顯示刻度。
#!/usr/bin/env python # -*- coding:utf-8 -*- import numpy as np import matplotlib.pyplot as plt def main(): # scatter# # plt.figure(1) # set up a figure # plt.subplot(331) # a figure with 3 rows and 3 columns # n = 128 X = np.random.normal(0, 1, n) Y = np.random.normal(0, 1, n) T = np.arctan2(Y, X) # plt.axes([0.025, 0.025, 0.95, 0.95])# plt.scatter(X, Y, s=75, c=T, alpha=5) plt.xlim(-1.5, 1.5), plt.xticks([]) # the range about x # plt.ylim(-1.5, 1.5), plt.yticks([]) plt.axis() plt.title("scatter") plt.xlabel("X") plt.ylabel("Y")
plt.show()
if __name__ == "__main__": main()