0

I would like to plot a shape on a basemap. For this, I created a basemap plot and a path plot and included both in one python code snippet. I first draw the baemap and then the path. Afterwards, I draw the plot with plt.show(). However, both plots are being drawn in seperate images. How can I plot the square on the basemap? Thanks!

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.path import Path
import matplotlib.patches as patches

# =============================================================================
# Basemap
# =============================================================================

plt.figure(figsize=(15,15/2))
m = Basemap(projection='robin', lon_0=0, resolution='c')
m.drawcountries(color='#ffffff', linewidth=0.75)
m.fillcontinents(color='#c0c0c0', lake_color='#e6f5ff')                
m.drawmapboundary(fill_color='#e6f5ff', linewidth=1, color='#000000') # Ocean          

# =============================================================================
# Path
# =============================================================================

verts = [
   (8493400,4307750),   # P0
   (8493400,12923250),  # P1
   (25480200,12923250),  # P2
   (25480200,4307750),  # P3
   (0,0)
]

codes = [
    Path.MOVETO,
    Path.LINETO,
    Path.LINETO,
    Path.LINETO,
    Path.CLOSEPOLY,
]

path = Path(verts, codes)

fig, ax = plt.subplots()
patch = patches.PathPatch(path, facecolor='orange', lw=2)
ax.add_patch(patch)

ax.set_xlim(0,33973600)
ax.set_ylim(0,17231000)
plt.show()

enter image description here enter image description here

Stücke
  • 868
  • 3
  • 14
  • 41
  • 1
    `fig, ax = plt.subplots()` creates a new figure. Remove it. `plt.gca()` gets you the current axes, which you can use to do whatever you need on it. – ImportanceOfBeingErnest Jul 03 '19 at 13:53
  • Thank you for cour comment and help! Replacing `fig, ax = plt.subplots()` with `fig, ax = plt.gca()` doesn't do the job. _TypeError: cannot unpack non-iterable AxesSubplot object_ – Stücke Jul 03 '19 at 14:09
  • 1
    I did not say "replace". Please read closely. – ImportanceOfBeingErnest Jul 03 '19 at 14:11
  • Thnaks for the hint. I still don't get it. When I remove `fig, ax = plt.subplots()` I get warnings for all lines in the code with `ax.` and I do not know to handle this. Also I am confused where exactly `plt.gca()` has to be placed. – Stücke Jul 03 '19 at 14:20
  • 1
    Use `plt.gca()` at each case where you would have used `ax`. – ImportanceOfBeingErnest Jul 03 '19 at 14:23
  • Fantastic, thank you! It's working. Do you want to post this as an answer? I still adjusted the `zorder=100` ... thanks! :) – Stücke Jul 03 '19 at 14:31

0 Answers0