久久精品精选,精品九九视频,www久久只有这里有精品,亚洲熟女乱色综合一区
    分享

    Python 隨筆 | 繪圖白化

     沐室 2025-08-28 發(fā)布于江蘇

    在繪制圖形,尤其是涉及地理信息的分布圖時,為了使圖形更加美觀或者更有效地突出重點信息,我們常常需要對一些非關(guān)鍵要素進行掩蓋或隱藏處理。這種做法不僅能夠更快地抓住圖表的核心內(nèi)容,還能增強視覺效果。例如,在制作某個省份的氣溫分布圖時,我們的主要目的是展示該省份內(nèi)部不同地點的氣溫變化情況。此時,如果將地圖上屬于其他省份的數(shù)據(jù)也一并顯示出來,則可能會分散讀者注意力,并且使得圖表顯得雜亂無章。為了解決這個問題,接可能需要“掩膜”來實現(xiàn)只保留特定區(qū)域(如目標省份)的數(shù)據(jù)而屏蔽掉其余部分。

    在剛開始的時候,我一直使用的是下面的語句:

    countries=BasicReader('./data.shp')geo_list=list(countries.geometries())poly=geo_listpath=Path.make_compound_path(*geos_to_path(poly))...省略畫圖部分...contour = ax.contourf(...)...省略畫圖部分...for col in contour.collections:    col.set_clip_path(path,ccrs.PlateCarree()._as_mpl_transform(ax))

    簡單來說,就是以下4步:

    1. 定義地理邊界 → 從 Shapefile 讀取國家邊界
    2. 轉(zhuǎn)換路徑格式 → Matplotlib 可識別的 Path 對象
    3. 繪制數(shù)據(jù) → 使用 contourf 繪制原始等值線圖(可能覆蓋整個矩形區(qū)域)
    4. 應用裁剪 → 只保留國家邊界內(nèi)的可見部分

    但最近在用的時候,卻發(fā)現(xiàn)以往很好用的代碼報錯了,報錯信息如下:

    AttributeError:'GeoContourSet' object has no attribute 'collections'

    經(jīng)檢測,應該是matplotlib版本問題,因此,又把裁剪代碼改成下列形式使用:

    for artist in ax_main.get_children():if hasattr(artist,'get_paths')and len(artist.get_paths())>0:        artist.set_clip_path(path, ax_main.transData)
    1. 裁剪路徑(Clip Path):類似蒙版,只有路徑內(nèi)的圖形會顯示,路徑外的部分被隱藏。
    2. 坐標系對齊 ( **transData***): *路徑的坐標必須與圖形元素的坐標系一致。使用 ax_main.transData 表示路徑的坐標值基于數(shù)據(jù)范圍(例如經(jīng)緯度、物理單位)。
    3. 篩選條件 ( get_paths):支持的圖形類型: ContourSet(等值線填充)、 PolyCollection(多邊形集合)、 LineCollection(線條集合)等。

    此時,又可以畫出這種圖了,又能用100年了。

    但又想畫出南海小地圖感覺,剛開始我以為,只需要簡單加個范圍限制就可以:

    south_china_sea_bbox =[105,125,0,30]ax_south_china.set_extent(south_china_sea_bbox, crs=ccrs.PlateCarree())

    然后畫成了這樣,裁剪區(qū)域后,設置范圍并沒有按預想的演示。

    苦思冥想一會,加上數(shù)據(jù)篩選解決問題。

    combined_mask = (lon >= min_lon) & (lon <= max_lon) & (lat >= min_lat) & (lat <= max_lat)south_data = np.full_like(data, np.nan)  south_data[combined_mask] = data[combined_mask]  

    最后完整畫圖代碼:

    from cartopy.io.shapereader import BasicReaderfrom matplotlib.path import Pathfrom cartopy.mpl.patch import geos_to_pathimport numpy as npimport matplotlib.pyplot as pltimport cartopy.crs as ccrsfrom cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTERimport netCDF4 as ncimport matplotlib as mplimport warnings
    warnings.filterwarnings("ignore"mpl.rcParams['font.sans-serif'] = [u'SimHei']  mpl.rcParams['axes.unicode_minus'] = Falsebinary_colors = ['#CE4A4B''#D68F3B']  binary_cmap = mpl.colors.ListedColormap(binary_colors)
    def picture(lon, lat, data, name):
        countries = BasicReader("./中華人民共和國.shp")    geo_list = list(countries.geometries())    poly = geo_list    path = Path.make_compound_path(*geos_to_path(poly))
        south_china_sea_bbox = [105125030]    china_bbox = [70140055    min_lon, max_lon, min_lat, max_lat = south_china_sea_bbox
        fig = plt.figure(figsize=(128))    ax_main = fig.add_subplot(111, projection=ccrs.PlateCarree())
        contour = ax_main.contourf(lon, lat, data,                               transform=ccrs.PlateCarree(),                               cmap=binary_cmap,                              levels=[-0.50.51.5],                               vmin=-0.5, vmax=1.5,                              extend='neither')    ax_main.add_geometries(countries.geometries(), linewidth=1., edgecolor='black'                          crs=ccrs.PlateCarree(), facecolor='none')    ax_main.set_extent(china_bbox, crs=ccrs.PlateCarree())    g1 = ax_main.gridlines(draw_labels=True, linewidth=1, color='gray'                          alpha=0.5, linestyle='--')    g1.top_labels = False    g1.right_labels = False    g1.xformatter = LONGITUDE_FORMATTER    g1.yformatter = LATITUDE_FORMATTER    g1.rotate_labels = False    for artist in ax_main.get_children():        if hasattr(artist, 'get_paths'and len(artist.get_paths()) > 0:            artist.set_clip_path(path, ax_main.transData)
        ax_main.set_title(name, fontsize=16, pad=20)    ax_south_china = fig.add_axes([0.600.240.150.15], projection=ccrs.PlateCarree())    combined_mask = (lon >= min_lon) & (lon <= max_lon) & (lat >= min_lat) & (lat <= max_lat)    south_data = np.full_like(data, np.nan)      south_data[combined_mask] = data[combined_mask]      contour_south = ax_south_china.contourf(lon, lat, south_data,                              transform=ccrs.PlateCarree(),                              cmap=binary_cmap,                              levels=[-0.50.51.5],                              vmin=-0.5, vmax=1.5,                              extend='neither')
        ax_south_china.add_geometries(countries.geometries(), linewidth=1., edgecolor='black'                          crs=ccrs.PlateCarree(), facecolor='none')    ax_south_china.set_extent(south_china_sea_bbox, crs=ccrs.PlateCarree())    for artist in ax_south_china.get_children():        if hasattr(artist, 'get_paths'and len(artist.get_paths()) > 0:            artist.set_clip_path(path, ax_south_china.transData)

    如果可以安裝新庫的話,推薦一個更簡單的方法:cnmaps庫,讓地圖處理更簡單。

      轉(zhuǎn)藏 分享 獻花(0

      0條評論

      發(fā)表

      請遵守用戶 評論公約

      類似文章 更多

      主站蜘蛛池模板: 亚洲综合色AAA成人无码| 国偷自产一区二区三区在线视频 | 极品尤物被啪到呻吟喷水| 日韩加勒比一本无码精品| 国色天香成人一区二区| 中文字幕亚洲制服在线看| 在线观看国产成人AV片| 国内精品国产成人国产三级| 久久亚洲精品情侣| 婷婷色香五月综合缴缴情香蕉| 久9视频这里只有精品试看| 国产一区二区波多野结衣 | 好男人官网资源在线观看| 狠狠五月深爱婷婷网| 国产精品毛片在线完整版SAB| 国内少妇偷人精品免费| 岛国岛国免费v片在线观看| 任我爽精品视频在线播放| 亚洲另类激情专区小说图片| 亚洲色在线V中文字幕| 中文字幕日韩有码一区| 亚洲AV无码AV在线影院| 不卡一区二区国产在线| 日韩精品久久久久久久电影蜜臀| 亚洲精品一区二区天堂| 色香欲天天影视综合网| 无码人妻久久一区二区三区APP| 人妻日韩人妻中文字幕| 国产又爽又粗又猛的视频 | 国产精成人品日日拍夜夜| 日韩人妻无码一区二区三区| 久久伊人色AV天堂九九小黄鸭 | 亚洲av免费成人在线| 成人AV无码一区二区三区| 两个人看的WWW在线观看| 強壮公弄得我次次高潮A片| 欧洲一区二区中文字幕| 中文字幕日韩有码一区| 在线观看国产成人无码| 精品成人乱色一区二区| 亚洲人成网线在线播放VA|