Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserving Axis annotation while interactively zooming in on a matplotlib basemap plot?

I'm creating a figure displaying the worldmap with some data on it (which is irrelevant here). Now I want to be able to zoom in on it using the Pan/Zoom-button or the Zoom-to-rectangle-button and then save the figure to a picture file once I'm done zooming in. The problem is that the axis annotations (and the lng-/lat-lines) are "hard-embedded" in the picture, which make them vanish when you zoom in.

Does anybody know how to get axis annotations that adapt to the zooming?

Here is a minimal working example (without any data):

import matplotlib as mpl
import matplotlib.pyplot as plt

from mpl_toolkits.basemap import Basemap
import numpy as np

fig = plt.figure(1, figsize=(12, 7))
m = Basemap(projection='merc',llcrnrlat=-80,urcrnrlat=80,\
            llcrnrlon=-180,urcrnrlon=180,resolution='l') #,lat_ts=20
m.drawcoastlines(); m.fillcontinents(); m.drawcountries()
# draw parallels and meridians.
m.drawparallels(np.arange(-90.,91.,30.),labels=[True, False, False, False], color='White')
m.drawmeridians(np.arange(-180.,181.,60.), labels=[False, False, False, True], color='White')
plt.show()
like image 736
Jonathan Kowalik Avatar asked Oct 21 '22 11:10

Jonathan Kowalik


1 Answers

Just in case someone ever stumbles upon my question, here is what I came up with as a rather quick work-around. :-)

My intended application was to zoom in and then save the figure. Now I do not do that interactively but by entering the "zoomed bounding box" in the code and dynamically creating lng/lat-ticks with the following function (which needs an import numpy as np beforehand):

def calculateBasemapTicks(minMaxList, nrOfParalles = 3, nrOfMeridians = 3):
    """
    Attempts to calculate meaningful ranges for .basemap.drawparallels
    and .drawmeridians. Enter a <minMaxList> in the form
    [lng_min, lng_max, lat_min, lat_max]. 
    Note that you might get rather ugly floats. I suggest using formatting
    as in ".drawparallels(..., fmt='%.4f')" or similar.
    """

    pAdjust = (minMaxList[3]-minMaxList[2])*0.1
    mAdjust = (minMaxList[1]-minMaxList[0])*0.1
    parallels = np.linspace(minMaxList[2]+pAdjust,minMaxList[3]-pAdjust,
                            nrOfParalles)
    meridians = np.linspace(minMaxList[0]+mAdjust,minMaxList[1]-mAdjust,
                            nrOfMeridians)
    return parallels, meridians
like image 86
Jonathan Kowalik Avatar answered Oct 24 '22 01:10

Jonathan Kowalik