java 画四周阴影,如何在底图中为单个多边形添加点画或阴影?

如果 shape 是 .shp 文件的形状,则可以将其提供给 matplotlib.patches.Polygon 并使用 hatch 参数添加一些剖面线 .

p= Polygon(np.array(shape), fill=False, hatch="X")

plt.gca().add_artist(p)

一个完整的例子:

from mpl_toolkits.basemap import Basemap

import matplotlib.pyplot as plt

from matplotlib.patches import Polygon

import numpy as np

m = Basemap(llcrnrlon=-10,llcrnrlat=35,urcrnrlon=35,urcrnrlat=60.,

resolution='i', projection='tmerc', lat_0 = 48.9, lon_0 = 15.3)

m.drawcoastlines()

# shape file from

# http://www.naturalearthdata.com/downloads/10m-cultural-vectors/10m-admin-0-countries/

fn = r"ne_10m_admin_0_countries\ne_10m_admin_0_countries"

m.readshapefile(fn, 'shf', drawbounds = False)

# here, 'shf' is the name we later use to access the shapes.

#Madrid

x,y = m([-3.703889],[40.4125])

m.plot(x,y, marker="o", color="k", label="Madrid", ls="")

hatches = ["\\\\","++", "XX"]

countries = ['Spain', 'Ireland', "Belgium"]

hatchdic = dict(zip(countries, hatches))

shapes = {}

for info, shape in zip(m.shf_info, m.shf):

if info['NAME'] in countries:

p= Polygon(np.array(shape), fill=False, hatch=hatchdic[info['NAME']])

shapes.update({info['NAME'] : p})

for country in countries:

plt.gca().add_artist(shapes[country])

handles, labels = plt.gca().get_legend_handles_labels()

handles.extend([shapes[c] for c in countries])

labels.extend(countries)

plt.legend(handles=handles, labels=labels, handleheight=3, handlelength=3, framealpha=1. )

plt.show()

dc0e5d2d-8658-4843-a10c-58feb5de0be1.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值