简体   繁体   中英

Adding a 2d histogram to a python cartopy plot causes the extent to be reset

I am writing a program that displays a heat map of lightning strikes over a cartopy plot. Using a scatter plot work; however when I try to implement the 2d histogram, the map becomes zoomed out all the way. Plot before adding histogram Plot after adding histogram

Here is the code lon_small and lat_small are the coordinate arrays.

proj = ccrs.LambertConformal()
fig, ax = plt.subplots(figsize=(10,10),subplot_kw=dict(projection=proj))
#FEATURES
#state_borders = cfeat.NaturalEarthFeature(category='cultural', name="admin_1_states_provinces_lakes",             scale = '10m', facecolor = 'none')
extent = ([-99,-92, 27, 31.3])
xynps = ax2.projection.transform_points(ccrs.PlateCarree(), lon_array, lat_array)

ax.set_extent(extent)
ax.set_extent(extent)

ax.add_feature(USCOUNTIES.with_scale('5m'), edgecolor = "gray")
ax.add_feature(state_borders, linestyle='solid', edgecolor='black')
ax.gridlines(draw_labels=True, dms=True, x_inline=False, y_inline=False)

#Adding scatter plots
ax.scatter(-95.36, 29.76, transform = ccrs.PlateCarree(), marker='*', s = 400, color = "orange")   #Houston 
ax.scatter(lon_small, lat_small, transform = ccrs.PlateCarree(), marker='.', s=0.001, c = "red")
#Adding Histogram
h = ax.hist2d(xynps[:,0], xynps[:,1], bins=100, zorder=10, alpha=0.75, cmin=120)
plt.show()

I have checked online for people with a similar problem, but I can't find anything like this problem.

There's a note in the docstring of hist2d stating:

Currently hist2d calculates its own axis limits, and any limits previously set are ignored.

... so i guess the easiest way to maintain initial limits is to do something like this:

...
extent = ax.get_extent(crs=ax.projection)
ax.hist2d(...)
ax.set_extent(extent, crs=ax.projection)
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM