簡體   English   中英

將 2d 直方圖添加到 python cartopy plot 會導致范圍被重置

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

我正在編寫一個程序,顯示 plot 上的雷擊熱量 map。使用散射 plot 工作; 然而,當我嘗試實現二維直方圖時,map 會一直縮小。 添加直方圖前Plot 添加直方圖后 Plot

這里的代碼lon_small和lat_small是坐標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()

我在網上查過有類似問題的人,但我找不到類似這個問題的東西。

hist2d的文檔字符串中有一條說明:

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

...所以我想維持初始限制的最簡單方法是做這樣的事情:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM