python画虚线代码_如何在python中从shapefile绘制虚线?

I am not sure on how to plot a dotted line from a shapefile in Python. It appears that readshapefile() does not have any linestyle for me to set. Below I have a working code where I take a shapefile and plot it, but it only plots a solid line. Any ideas to set me in the right direction? Thanks!

The shapefile can be found here: http://www.natice.noaa.gov/products/daily_products.html, where the Start Date is Feb 15th, end date is Feb 17th, and the Date Types is Ice Edge. It should be the first link.

#!/awips2/python/bin/python

from mpl_toolkits.basemap import Basemap

import matplotlib.pyplot as plt

map = Basemap(llcrnrlon=-84.37,llcrnrlat=42.11,urcrnrlon=-20.93,urcrnrlat=66.48,

resolution='i', projection='tmerc', lat_0 = 55., lon_0 = -50.)

map.drawmapboundary(fill_color='aqua')

map.fillcontinents(color='#ddaa66',lake_color='aqua')

map.drawcoastlines(zorder = 3)

map.readshapefile('nic_autoc2018046n_pl_a', 'IceEdge', zorder = 2, color = 'blue')

plt.show()

解决方案

From the Basemap documentation:

A tuple (num_shapes, type, min, max) containing shape file info is

returned. num_shapes is the number of shapes, type is the type code

(one of the SHPT* constants defined in the shapelib module, see

http://shapelib.maptools.org/shp_api.html) and min and max are

4-element lists with the minimum and maximum values of the vertices.

If drawbounds=True a matplotlib.patches.LineCollection object is

appended to the tuple.

drawbounds is True by default, so all you have to do is collect the return value of readshapefile and alter the linestyle of the returned LineCollection object, which can be done with LineCollection.set_linestyle(). So in principle you can change the linestyle of your plotted shape file with something like this:

result = m.readshapefile('shapefiles/nic_autoc2018046n_pl_a', 'IceEdge', zorder = 10, color = 'blue')#, drawbounds = False)

col = result[-1]

col.set_linestyle('dotted')

plt.show()

However, your shapefile contains 5429 separate line segments of different length and somehow matplotlib does not seem to be able to deal with this large amount of non-continuous lines. At least on my machine the plotting did not finish within one hour, so I interrupted the process. I played a bit with your file and it seems like many of the lines are broken into segments unnecessarily (I'm guessing this is because the ice sheet outlines are somehow determined on tiles and then pieced together afterwards, but only the providers will really know). Maybe it would help to piece together adjacent pieces, but I'm not sure.

I was also wondering whether the result would even look that great with a dotted line, because there are so many sharp bends. Below I show a picture where I only plot the 100 longest line segments (leaving out drawcoastlines and with thicker lines) using this code:

import numpy as np

result = m.readshapefile('shapefiles/nic_autoc2018046n_pl_a', 'IceEdge', zorder = 10, color = 'blue')#, drawbounds = False)

col = result[-1]

segments = col.get_segments()

seglens = [len(seg) for seg in col.get_segments()]

segments = np.array(segments)

seglens = np.array(seglens)

idx = np.argsort(seglens)

seglens = seglens[idx]

segments = segments[idx]

col.remove()

new_col = LineCollection(segments[-100:],linewidths = 2, linestyles='dotted', colors='b')

ax.add_collection(new_col)

plt.show()

And the result looks like this:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值