我已經為此苦苦掙扎了一個小時,但仍然感到困惑......我想繪制一系列 15 張影像(熱圖的每一行一張),并有代碼這樣做:
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
def getImage(path):
return OffsetImage(plt.imread(path),zoom=0.085)
for x0, y0, path in zip([0]*15,np.arange(0.5,15.5,1),paths):
ab = AnnotationBbox(getImage(path), (x0, y0), frameon=False, fontsize=4)
ax.add_artist(ab)
由于我的 x 坐標是 15 個 0 的串列,因此我的繪圖生成如下(未顯示所有 15 個徽標):

我想將影像向左移動一點,這樣它們就不會與實際的熱圖重疊,并且像文本標簽一樣很好地對齊。但是,如果我將 x 坐標更改為小于 0 的值(或它出現的軸邊界之外的任何值),則繪圖會生成,但所有影像都不存在。您是否不允許將軸藝術家添加到負坐標?我發現有一個add_artist()用于圖形類,但是當我嘗試使用它而不是ax我得到一個錯誤時:
AttributeError: 'NoneType' object has no attribute 'transData'
So, does anyone know how I could plot these logos just a bit to the left using ax.add_artist()?
EDIT: After looking through the code of the accepted answer, I realized the xybox param of my annotation box will solve my problem. Thus, the following loop works correclty:
for x0, y0, path in zip([0]*15,np.arange(0.5,15.5,1),paths):
ab = AnnotationBbox(getImage(path), (x0, y0), xybox=(-.4,y0), frameon=False, fontsize=4)
ax.add_artist(ab)
But the accepted answer's way of going about it also works correctly.
uj5u.com熱心網友回復:
我改編了@JohanC 對您的作業的出色回應,您對此發表了評論。他應該是回應的人,但出于尊重,我會這樣做。將圖形的軸與加載影像的物件對齊。根據圖形資料將注釋框的位置設定為負值。將注釋框添加到藝術家。熱圖資料來自
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/440753.html
標籤:python matplotlib
下一篇:如何繪制df行的切片?
