Home > Software engineering >  Plotting multiple subplots with different shapefiles in background
Plotting multiple subplots with different shapefiles in background

Time:03-27

I am trying to plot side by side GeoPandas shapefiles using matplotlib but the titles, xlabel and ylabel are not plotting correctly.

fig, axes = plt.subplots(1,2, figsize=(10,3), sharex=True, sharey=True)

base = subs.boundary.plot(color='black', linewidth=0.1, ax=axes[0])
cluster.plot(ax=base, column='pixel', markersize=20, legend=True, zorder=2)
plt.title('THHZ')
plt.xlabel('Longitude')
plt.ylabel('Latitude')


base = forest.boundary.plot(color='black', linewidth=0.2, ax=axes[1])
cluster.plot(ax=base, column='forest', markersize=20, legend=True, zorder=2)
plt.title('Forest')

This is what I get

enter image description here

This is what I want

enter image description here

CodePudding user response:

You have a mixture of object-oriented and pyplot-style matplotlib interactions. The plt.* calls are following a logic of the current axis to act upon. More detail from the matplotlib docs here: Pyplot vs Object Oriented Interface. I don't know how that behaves with your plotting function calls (code not included in your post).

To be certain of what axes you are interacting with, use the object-oriented calls using the axes object you already have:

axes[0].set_title('THHZ')
axes[0].set_xlabel('Longitude')
axes[0].set_ylabel('Latitude')
axes[1].set_title('Forest')

You can also add fig.tight_layout() at the very end for a compacted figure layout.

  • Related