I've been struggling with this for an hour and am still stumped... I would like to plot a series of 15 images (one for each row of a heatmap) and have code to do so:
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)
Since my x coordinates is list of 15 0s, my plot generates as such (not all 15 logos shown):
I would like to move the images to the left a bit more so they don't overlap the actual heatmap and are nicely aligned like a text label would be. However, if I change the x coordinate to something less than 0 (or anything outside of the axes bound it appears), the plot generates but all the images are absent. Are you not allowed to add axes artists to negative coordinates? I found out there is an add_artist()
for the figure class, but when I try to use that instead of ax
I get an error:
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.
CodePudding user response:
I adapted @JohanC's excellent response to your assignment, which you commented on. He should be the one to respond, but I will do so out of respect. Align the axis of the graph with the object from which the image was loaded. Set the position of the annotation box to a negative value based on the graph data. Add the annotation box to the artist. The heatmap data is obtained from the