參考來源:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.axis.html
心血來潮,想用matplotlib畫一下faster-rcnn中生成的基礎anchors,如下,
base_anchors = [[ -84.,-40., 99., 55.],
[-176.,-88.,191.,103.],
[-360. -184.,375.,199.],
[ -56.,-56., 71., 71.],
[-120. -120.,135.,135.],
[-248. -248.,263.,263.],
[ -36.,-80., 51., 95.],
[ -80. -168., 95.,183.],
[-168. -344.,183.,359.]]
本來覺得應該很簡單,但是始終畫不出來...座標軸範圍始終是0-1...
死磕之下終於攻破...主要是axis參數設置問題,這裏簡單講解一下其使用方法,避免再入坑...
1. 調用方式
xmin, xmax, ymin, ymax = axis()
xmin, xmax, ymin, ymax = axis(xmin, xmax, ymin, ymax)
xmin, xmax, ymin, ymax = axis(option)
xmin, xmax, ymin, ymax = axis(**kwargs)
xmin, xmax, ymin, ymax = axis(str)
2. 參數
xmin, ymin, xmax, ymax | 座標軸的最大最小值 | |
str | ‘on’ |
顯示座標軸 |
‘off’ | 隱藏座標軸 | |
'equal' | 調整座標軸進行縮放 | |
‘scaled’ | 調整畫布長寬進行縮放 | |
'tight' | 設置座標軸取值恰好滿足數據取值範圍 | |
'auto' | 自動調整刻度 | |
‘image’ | 'scaled' with axis limits equal to data limits.(感覺和auto,tight差不多,不知道怎麼翻譯...) | |
'square' | 與scale類似,但要求xmax-xmin = ymax-ymin |
3 代碼及結果展示
# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
#a的每一行爲一個anchor
#四個值分別爲矩形對角線兩頂點座標
base_anchors = [[ -84.,-40., 99., 55.],
[-176.,-88.,191.,103.],
[-360. -184.,375.,199.],
[ -56.,-56., 71., 71.],
[-120. -120.,135.,135.],
[-248. -248.,263.,263.],
[ -36.,-80., 51., 95.],
[ -80. -168., 95.,183.],
[-168. -344.,183.,359.]]
fig = plt.figure()
axes = fig.add_subplot(111)
for i in range(9):
p = a[i]
rec = patches.Rectangle((p[0], p[1]),p[2]-p[0], p[3]-p[1], fill=False, linewidth=1, edgecolor='r', facecolor='none')
axes.add_patch(rec)
#axes.axis('equal')
#axes.axis('scaled')
#axes.axis('tight')
#axes.axis('auto')
#plt.title('Base anchors')
'''
plt.xlim、plt.ylim 設置橫縱座標軸範圍
plt.xlabel、plt.ylabel 設置座標軸名稱
plt.xticks、plt.yticks設置座標軸刻度
'''
#plt.xlim(-600, 600)
#plt.ylim(-600, 600)
plt.show()
str參數效果(不設置時,座標軸始終爲0-1.0),從左到右邊分別爲equal,scaled, tight, auto
上圖scaled參數的效果明顯與其它三種不同,它會自動將畫布縮放爲正方形。
equal和scaled區別在改變窗口大小時候區別更加明顯,如下。