我嘗試使用contourf包括陰影區域(指示重要性)來創建地理地圖。
這是一個 MWE:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
lats = np.arange(-90, 91, 10)
lons = np.arange(-180, 181, 20)
data = np.sin(np.arange(len(lats)*len(lons))).reshape(len(lats), len(lons))
proj = ccrs.Robinson()
fig, ax = plt.subplots(figsize=(6, 7), subplot_kw={'projection': proj})
im = ax.contourf(
lons, lats, data,
transform=ccrs.PlateCarree(),
)
ax.contourf(
lons, lats, data > data.mean(),
transform=ccrs.PlateCarree(),
colors='none',
levels=[.5, 1.5],
hatches='///////',
)
ax.coastlines()
ax.set_global()
cbar = fig.colorbar(im, ax=ax, location='bottom')
我正在努力調整孵化的屬性。這是粗糙的方式,我想調整它以便能夠決議更精細的結構。可以通過縮放圖形大小來做到這一點:
scale = 10
fig, ax = plt.subplots(figsize=(6*scale, 7*scale), subplot_kw={'projection': proj})
ax.contourf(
lons, lats, data,
transform=ccrs.PlateCarree(),
)
ax.contourf(
lons, lats, data > data.mean(),
transform=ccrs.PlateCarree(),
colors='none',
levels=[.5, 1.5],
hatches='///////',
)
ax.coastlines()
ax.set_global()
cbar = fig.colorbar(im, ax=ax, location='bottom')
但這實際上會弄亂其他所有內容(文本、線寬等),并且在任何情況下都可能不是最好的方法。在這種情況下,有沒有更好的方法來調整影線的屬性?

uj5u.com熱心網友回復:
引數hatchesincontourf應該是大小為 2 的串列,因為您有兩個級別。然后,您可以通過重復圖案來增加陰影圖案的密度,例如,density*'/'。所以總體來說應該是hatches=[density*'/',density*'/']。下面是我將密度設定為 7 時的示例:
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
lats = np.arange(-90, 91, 10)
lons = np.arange(-180, 181, 20)
data = np.sin(np.arange(len(lats)*len(lons))).reshape(len(lats), len(lons))
proj = ccrs.Robinson()
fig, ax = plt.subplots(figsize=(6, 7), subplot_kw={'projection': proj})
im = ax.contourf(
lons, lats, data,
transform=ccrs.PlateCarree())
density=7
ax.contourf(
lons, lats, data > data.mean(),
transform=ccrs.PlateCarree(),
colors='none',
levels=[.5,1.5],
hatches=[density*'/',density*'/'],
)
ax.coastlines()
ax.set_global()
cbar = fig.colorbar(im, ax=ax)
輸出給出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/371882.html
標籤:蟒蛇-3.x matplotlib 卡托皮
