簡體   English   中英

plot斑馬紋軸如何在matplotlib

[英]How to plot zebra style axis in matplotlib

我想要 plot斑馬風格的軸,類似於:

斑馬紋邊框軸

下面是我的代碼:

import matplotlib.pyplot as plt
import cartopy.io.shapereader as shpreader
import cartopy.crs as ccrs
from cartopy.feature  import ShapelyFeature

fig, ax = plt.figure(figsize=(12,9),  dpi=150 )
sFilename_shapefile = './some_shape.shp'
pShapeReader = shpreader.Reader(sFilename_shapefile)
pProjection_map = ccrs.PlateCarree()
aShapeFeature = ShapelyFeature(pShapeReader.geometries(), 
  pProjection_map, facecolor='grey', edgecolor='grey', 
  linewidth=0.5)
ax.add_feature(aShapeFeature, zorder = 4)
plt.show()

我得到的是這樣的:

法線邊界軸

我有一個適合我的目的的 hacky 解決方案:

在此處輸入圖像描述

示例用法:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt

crs = ccrs.PlateCarree()

fig = plt.figure(figsize=(5, 2))
ax = fig.add_subplot(projection=crs)

ax.coastlines()
ax.set_extent((-125, -85, 22, 42))
ax.set_xticks((-120, -110, -100, -90))
ax.set_yticks((25, 30, 35, 40))

add_zebra_frame(ax, crs=crs)

我現在把框架放在 function 中。 它可能不適用於混合緯度/經度刻度的許多極地類型投影,現在如果你不指定你想要的刻度線它就不能很好地工作(我仍然不清楚 Cartopy 如何選擇默認值刻度)。

https://gist.github.com/scottstanie/dff0d597e636440fb60b3c5443f70cae

基本上我所做的就是關閉脊椎並在每個 xticks/yticks 之間繪制一條交替的黑/白線。

import itertools
import matplotlib.patheffects as pe
import numpy as np

def add_zebra_frame(ax, lw=2, crs="pcarree", zorder=None):

    ax.spines["geo"].set_visible(False)
    left, right, bot, top = ax.get_extent()
    
    # Alternate black and white line segments
    bws = itertools.cycle(["k", "white"])

    xticks = sorted([left, *ax.get_xticks(), right])
    xticks = np.unique(np.array(xticks))
    yticks = sorted([bot, *ax.get_yticks(), top])
    yticks = np.unique(np.array(yticks))
    for ticks, which in zip([xticks, yticks], ["lon", "lat"]):
        for idx, (start, end) in enumerate(zip(ticks, ticks[1:])):
            bw = next(bws)
            if which == "lon":
                xs = [[start, end], [start, end]]
                ys = [[bot, bot], [top, top]]
            else:
                xs = [[left, left], [right, right]]
                ys = [[start, end], [start, end]]

            # For first and lastlines, used the "projecting" effect
            capstyle = "butt" if idx not in (0, len(ticks) - 2) else "projecting"
            for (xx, yy) in zip(xs, ys):
                ax.plot(
                    xx,
                    yy,
                    color=bw,
                    linewidth=lw,
                    clip_on=False,
                    transform=crs,
                    zorder=zorder,
                    solid_capstyle=capstyle,
                    # Add a black border to accentuate white segments
                    path_effects=[
                        pe.Stroke(linewidth=lw + 1, foreground="black"),
                        pe.Normal(),
                    ],
                )

暫無
暫無

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

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