geopandas / contextily

Context geo-tiles in Python
https://contextily.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
493 stars 81 forks source link

attributions severely wrapped upon savefig #211

Open Matthew-J-Payne opened 1 year ago

Matthew-J-Payne commented 1 year ago

Hey contextily, thanks for all your work for this library - really great.

Apologies for the gigantic .pngs!

So when I visualise a plot using contextily.add_basemap(), when viewing the plot using jupyter inline, I get a nice unwrapped attribution (showing you via windows snipping tool, hence low dpi), which I would like to be reflected when the .png is saved:

image

But, when saving I get this:

for_pr

My script, below:

import geopandas as gpd
import contextily as ctx

rivers_shapefile = gpd.read_file('datasets/rivers.shp')
rivers_shapefile['class'] = 'rivers'

fig, ax = plot.subplots(figsize = (8, 8))
rivers_shapefile.plot(column = 'class',
                         ax = ax,
                         cmap = 'bwr',
                         linewidth = 0.6,
                         zorder = 4,
                         alpha = 0)
ctx.add_basemap(ax = ax,
                source = ctx.providers.CartoDB.Voyager,
                zorder = 1,
                zoom = 8,
                attribution = "© OpenStreetMap contributors 2023. Distributed under the Open Data Commons Open Database License (ODbL) v1.0",
                attribution_size = 6,
                crs = 32649)

ax.xaxis.set_major_locator(plot.MaxNLocator(4))
ax.yaxis.set_major_locator(plot.MaxNLocator(4))

custom_xticklabels_4 = ["0", "114.1°E", "114.6°E", "115.1°E", "115.6°E"]
custom_yticklabels_4 = ["0", "2.7°N", "3.3°N" , "3.8°N", "4.3°N"]
ax.set_xticklabels(custom_xticklabels_4)
ax.set_yticklabels(custom_yticklabels_4)

fig.savefig(fname = "outputs/for_pr.png", dpi = 1200, facecolor = "white", bbox_inches = "tight")
plot.show() # plots for debugging, defaults to low res plot

The plot is for a paper in review, so I can't publicly share the data, but happy to send across. Contextily version is 1.3.0 and geopandas 0.12.2.

Any pointers in the right direction is appreciated, thank you!

All the best, Matt

darribas commented 1 year ago

Perhaps related to https://github.com/geopandas/contextily/issues/95 ?

The actual method is at:

https://github.com/geopandas/contextily/blob/d821ac782764a0bad436b61eef9f83473fc3df6c/contextily/plotting.py#L259-L298