umar_a
umar_a

Reputation: 53

Image is upside down in ax.imshow() but changing the origin keyword doesn't fix the problem

Image inside a matplotlib subplot is inverted. Changing the 'origin' keyword from 'upper' to 'lower' doesn't make any difference. When I plot the image separately it plots fine.

I am trying to visualize an image behind a scatter and contour plot in matplotlib. This visualization is one subplot amongst six different subplots. The problem is that the image appears upside down and I tried changing the 'origin' keyword from 'upper' to 'lower' but strangely that doesn't make any difference in my case. Could anyone give me a clue to why this is happening and how to possibly fix this? Following is the relevant part of the code I use. The image is a 560 X 550 X 3 numpy array.

Problem Code

fig, axes = plt.subplots(nrows=3, ncols=2) 

"plot 1"
Dataframe[scorer][bodyparts2plot[0]].plot.scatter('x', 'y', 
               c = '#a98d19', ax=axes[0,0], xlim = (0,560), ylim = (0,550),
               figsize= (20,20), title = bodyparts2plot[3], alpha = 0.1) 
axes[0,0].imshow(image) # plot image
df = Dataframe[scorer][bodyparts2plot[0]][['x','y']]
sns.kdeplot(df,cmap='jet', n_levels=50,ax=axes[0,0]) # plot contour

"plot 2"
Dataframe[scorer][bodyparts2plot[1]].plot.scatter('x', 'y',
                c = '#006666', ax=axes[0,1], xlim = (0,560), ylim = (0,550),
                figsize= (20,20), title = bodyparts2plot[4], alpha = 0.1) 

axes[0,1].imshow(image) # plot image
df = Dataframe[scorer][bodyparts2plot[1]][['x','y']]
sns.kdeplot(df,cmap='jet', n_levels=50, ax=axes[0,1]) # plot contour

Changing the 'origin' keyword in axes.imshow() to either "upper" or "lower" makes no difference

This works but not what I want

when I do

plt.imshow(image)

the image shows fine.

This is the current situation: image with problem

What i want is the image to be upside down. Like this: example but with all the other stuff overlaid.

Upvotes: 0

Views: 1884

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40697

Your problem is with your initial pandas df.plot.scatter() call, or rather the fact that you specify de ylim= in there. You are forcing the axis to start at 0 at the bottom, whereas imshow() plots with 0 at the top of the image.

Changing to df.plot.scatter(..., ylim=(550,0), ...) should fix the problem

Upvotes: 2

Related Questions