Python使用matplotlib 畫矩形的三種方式分析
本文實例講述了Python使用matplotlib 畫矩形的三種方式。分享給大家供大家參考,具體如下:
假設(shè)矩形兩點坐標(biāo)如下,分別為:x1, y1, x2, y2
cat_dict['bbox'][i] = (min_row, min_col, max_row, max_col)
1. plt.plot(x,y)
這種方式畫的矩形 因為邊距的問題 會放縮
plt.plot([cat_dict['bbox'][i][1], cat_dict['bbox'][i][3], cat_dict['bbox'][i][3], cat_dict['bbox'][i][1], cat_dict['bbox'][i][1]], # col [cat_dict['bbox'][i][0], cat_dict['bbox'][i][0], cat_dict['bbox'][i][2], cat_dict['bbox'][i][2], cat_dict['bbox'][i][0]], # row color=[c / 255 for c in label_colors[cat_idx]], marker='.', ms=0)
2. ax.add_line(Line2D)
添加 4 條直線的方式,比較繁瑣
from matplotlib.lines import Line2D color = [c / 255 for c in label_colors[cat_idx]] ax.add_line(Line2D([cat_dict['bbox'][i][1], cat_dict['bbox'][i][3]], [cat_dict['bbox'][i][0], cat_dict['bbox'][i][0]], linewidth=2, color=color)) ax.add_line(Line2D([cat_dict['bbox'][i][3], cat_dict['bbox'][i][3]], [cat_dict['bbox'][i][0], cat_dict['bbox'][i][2]], linewidth=2, color=color)) ax.add_line(Line2D([cat_dict['bbox'][i][3], cat_dict['bbox'][i][1]], [cat_dict['bbox'][i][2], cat_dict['bbox'][i][2]], linewidth=2, color=color)) ax.add_line(Line2D([cat_dict['bbox'][i][1], cat_dict['bbox'][i][1]], [cat_dict['bbox'][i][2], cat_dict['bbox'][i][0]], linewidth=2, color=color))
3. plt.gca().add_patch(plt.Rectangle())
最好的一種實現(xiàn)方式,fast rcnn 也是這么用的,傳送門
plt.gca().add_patch(plt.Rectangle(xy=(cat_dict['bbox'][i][1], cat_dict['bbox'][i][0]), width=cat_dict['bbox'][i][3] - cat_dict['bbox'][i][1], height=cat_dict['bbox'][i][2] - cat_dict['bbox'][i][0], edgecolor=[c / 255 for c in label_colors[cat_idx]], fill=False, linewidth=2))
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python服務(wù)器與android客戶端socket通信實例
這篇文章主要介紹了python服務(wù)器與android客戶端socket通信的實現(xiàn)方法,實例形式詳細講述了Python的服務(wù)器端實現(xiàn)原理與方法,以及對應(yīng)的Android客戶端實現(xiàn)方法,需要的朋友可以參考下2014-11-11基礎(chǔ)語音識別-食物語音識別baseline(CNN)
這篇文章主要介紹了一個基礎(chǔ)語音識別題目-食物語音識別baseline(CNN),代碼詳細嗎,對于想要學(xué)習(xí)語音識別的朋友可以參考下2021-04-04Python PyMuPDF實現(xiàn)PDF與圖片和PPT相互轉(zhuǎn)換
能夠用來對PDF文檔進行操作的Python包有好幾個,如提取內(nèi)容的PdfPlumber、PDFMiner,可以用來對PDF文件進行修改操作的PyPDF2等等,如果只是需要簡單地對PDF文件實現(xiàn)合并、拆分、書簽操作,使用PyPDF2就足以滿足。但如果想對PDF文件進行一些底層操作,基本上只有PyMuPDF了2022-12-12python讀取并定位excel數(shù)據(jù)坐標(biāo)系詳解
這篇文章主要介紹了python讀取并定位excel數(shù)據(jù)坐標(biāo)系詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-06-06Pandas數(shù)據(jù)分析-pandas數(shù)據(jù)框的多層索引
這篇文章主要介紹了Pandas數(shù)據(jù)分析-pandas數(shù)據(jù)框的多層索引,pandas數(shù)據(jù)框針對高維數(shù)據(jù),也有多層索引的辦法去應(yīng)對具體詳細的內(nèi)容介紹需要的小伙伴可以參考一下2022-08-08Pandas?DataFrame.drop()刪除數(shù)據(jù)的方法實例
pandas作為數(shù)據(jù)分析強大的庫,是基于numpy數(shù)組構(gòu)建的,專門用來處理表格和混雜的數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于Pandas?DataFrame.drop()刪除數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2022-07-07