0

I'm attempting to inset a line graph within the lower right-hand corner of a Basemap plot, but the code I'm using (based off the Matplotlib documentation) is not working for me, probably because it includes no Basemap arguments. Instead, I keep getting a figure that looks like this:

enter image description here

Below is the code I've used to try and create my figure. Any help on getting the line graph inside the graph would be extremely helpful. Thanks in advance!

fig = plt.figure(figsize=(10,8))
ax = fig.add_subplot(121)
m = Basemap(llcrnrlon=50.,llcrnrlat=35.,urcrnrlon=160.,urcrnrlat=63.,projection='lcc',resolution='c',lat_1=20.,lat_2=40.,lon_0=90.,lat_0=50.)
m.drawcountries(color='black')
m.drawmapboundary(fill_color='lightblue')
m.fillcontinents(color='beige')
m.drawparallels(np.arange(0.,90.,5.),color='gray',dashes=[1,3],labels=[1,0,0,0])
m.drawmeridians(np.arange(0.,360.,15.),color='gray',dashes=[1,3],labels=[0,0,0,1])

clat = np.arange(65,45,-2)
clon = np.arange(80,100,2)
dlat = np.arange(70,50,-2)
dlon = np.arange(85,105,2)
mincon = np.random.randint(900,1000,73)
mindis = np.random.randint(910,1010,73)


cX,cY = m(clon,clat)
dX,dY = m(dlon,dlat)
m.plot(cX,cY,'bo-',label='Continuous')
m.plot(dX,dY,'ro-',label='Discontinuous') 
ax.legend()

a = plt.axes([0.7,0.3,.2,.2])
plt.plot(np.arange(0,73,1),mincon,color='blue',label='Continuous')
plt.plot(np.arange(0,73,1),mindis,color='red',label='Discontinuous')
plt.xlabel('Model Time')
plt.ylabel('Minimum Pressure (hPa)')

plt.show() 
4
  • Is there a chance you can make this reproducible (see minimal reproducible example)? (I would suspect the actual data used here is not relevant, so please provide a piece of code that is runnable by itself.) Commented Mar 8, 2019 at 19:50
  • @ImportanceOfBeingErnest Thanks for that link! I've edited the code with some dummy data that should still do the trick. Commented Mar 8, 2019 at 20:05
  • You've done: ax = fig.add_subplot(121) so the map only fills the left subplot slot, whereas your inset is at 0.7 which is on the right-hand-side... Put at 0.35, and you should get what you want. Commented Mar 8, 2019 at 21:23
  • Instead of ax = fig.add_subplot(121) try ax = plt.axes([0.1,0.1,0.9,0.9]). Commented Mar 8, 2019 at 21:25

1 Answer 1

3

I'd use an mpl_toolkits.axes_grid1.inset_locator.inset_axes.

import numpy as np
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt


fig = plt.figure(figsize=(10,6))
ax = fig.add_subplot(111)

m = Basemap(llcrnrlon=50.,llcrnrlat=35.,urcrnrlon=160.,urcrnrlat=63.,
            projection='lcc',resolution='c',lat_1=20.,lat_2=40.,lon_0=90.,lat_0=50., ax=ax)
m.drawcountries(color='black')
m.drawmapboundary(fill_color='lightblue')
m.fillcontinents(color='beige')
m.drawparallels(np.arange(0.,90.,5.),color='gray',dashes=[1,3],labels=[1,0,0,0])
m.drawmeridians(np.arange(0.,360.,15.),color='gray',dashes=[1,3],labels=[0,0,0,1])

clat = np.arange(65,45,-2)
clon = np.arange(80,100,2)
dlat = np.arange(70,50,-2)
dlon = np.arange(85,105,2)
mincon = np.random.randint(900,1000,73)
mindis = np.random.randint(910,1010,73)


cX,cY = m(clon,clat)
dX,dY = m(dlon,dlat)
m.plot(cX,cY,'bo-',label='Continuous')
m.plot(dX,dY,'ro-',label='Discontinuous') 
ax.legend(loc="lower left")

ax2 = inset_axes(ax, "30%", "40%", loc="lower right")
ax2.plot(np.arange(0,73,1),mincon,color='blue',label='Continuous')
ax2.plot(np.arange(0,73,1),mindis,color='red',label='Discontinuous')
ax2.set_xlabel('Model Time')
ax2.set_ylabel('Minimum Pressure (hPa)')
ax2.xaxis.set_ticks_position('top')
ax2.xaxis.set_label_position('top') 

plt.show() 

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.