I have a plot in a Jupyter Notebook that maps annual population data against percentages. It looks like this:
The plot has the dimensions 800*400:
plt.figure(figsize=(8,4));
plt.ylim([0,100])
...
IN[]: fig.get_size_inches()*fig.dpi
OUT[]: array([800., 400.])
Now that it's done I want to set an image in the background. If I now load in the image I want to use completely separately from the plot:
fig = plt.figure(figsize=(8,4));
plt.style.use('seaborn-muted')
img = mpimg.imread('london_skyline.png')
plt.imshow(img, cmap=cm.gray, alpha=0.2)
The code produces this:
I note that the y axis of the image is mapped in reverse, from 100-0 rather than 0-100, and the x axis is sized at 250. The image shape is (100, 250):
IN[]: img.shape
OUT[]: (100, 250)
If I try to combine the background with the original plot everything is horribly resized and reshaped. Presumably what I need to do is reshape and resize the image so that it fits the shape, dimensions and orientation of the original plot (800, 400). I see, eg., that skimage.transform
has the functions rescale
and resize
that may be of use.
Basically I want to keep the image as it is, but reshape it so that it slots into the original plot frame. So I would want to manipulate the image so that I could just add it to the original plot without it distorting anything, or itself being distorted.
Can anyone help?
Looking at Paul Brodersen's response below I used the extent
keyword to resize my image, which solves my original problem. So my code now looks like this:
plt.figure(figsize=(8,4));
plt.ylim([0,100])
plt.plot(df1, color=col, linewidth=1.0, zorder=2);
plt.plot(df2, color=col, ls='-.',alpha=0.5, linewidth=1.0, zorder=2);
plt.gca().fill_between(df1.keys(), df1, df2,facecolor=col, alpha=0.05, zorder=2)
plt.imshow(img, cmap=cm.gray, alpha=0.1, zorder=1, extent=[0, 400, 0, 100])
which produces this:
However, as you'll notice, the plots don't appear on top of the image even though I have set the zorder
arguments appropriately, and the whole image / plot is shrunk in the y dimension, so I feel I still have a few things to understand here.
Any more clues? tia.
Perhaps of interest - if I set the alpha of the image to 0.0 the plot lines still don't appear. I've also tried suggestions made elsewhere such as using larger zorder values (10, 15...), or using values such as 2.5. I have also tried setting the image to a negative order value, -1. None of them have helped so far.
Setting the extent to [2004, 2018, 0 , 100], as suggested in the comments below, produces this:
plt.style.use('seaborn-muted')
plt.ylim([0,100])
plt.ylabel('% of population', fontsize=8)
plt.rcParams['xtick.labelsize']=8
plt.rcParams['ytick.labelsize']=8
plt.plot(hackney_df['Christian'], color='k', linewidth=1.0, zorder=2.5);
plt.plot(london_df['Christian'], color='k', ls='-.',alpha=0.5, linewidth=1.0, zorder=2.5);
plt.gca().fill_between(hackney_df['Christian'].keys(), hackney_df['Christian'], london_df['Christian'],facecolor='k', alpha=0.1, zorder=2.5)
plt.imshow(img, cmap=cm.gray, alpha=0.1, extent=[2004, 2018, 0, 100])
plt.title ('Hackney Christians compared to London', fontsize=9)
plt.tight_layout()
So now I just need somehow to stretch the x axis out to a reasonable size.