底图

编程入门 行业动态 更新时间:2024-10-25 16:17:51
底图 - 从shapefile添加文本(Basemap - adding text from shapefile)

在阅读了关于注释和添加文本的底图教程之后,我仍然遇到了一些问题。

shapefile_info = m.readshapefile('/path/to/shapefile', 'shapefile_name') for info, shape in zip(m.points_info, m.points): print info, shape

读取shapefile并打印出信息(使用上面的代码),我们得到这个输出:

{'LABELTYPE':'ONE','LABELNAME':'起点'}(2274311.7551607937,759422.9640236866)

{'LABELTYPE':'TWO','LABELNAME':'终点'}(1839892.6558604166,947255.0800333266)

使用下面的代码......

from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt l_one, l_two = 0, 0 m = Basemap(projection = 'merc', llcrnrlat= -2, urcrnrlat= 52, llcrnrlon= -137,\ urcrnrlon= -58, lat_ts=40,resolution='i') m.shadedrelief() m.drawcoastlines(linewidth=0.5) m.drawcountries(linewidth=0.5) m.drawstates(linewidth=0.5) m.drawparallels(np.arange(-90, 90, 10), linewidth = 0.2, labels = [True, False, True, False], fontsize = 'x-small') m.drawmeridians(np.arange(-180, 180, 10), linewidth = 0.2, labels = [False, False, False, True], fontsize = 'x-small') m.readshapefile('/path/to/shapefile', 'shapefile_name') shapefile_info = m.readshapefile('/path/to/shapefile', 'shapefile_name') for info, shape in zip(m.points_info, m.points): x, y = zip(shape) if info['LABELTYPE'] == 'ONE': m.plot(x, y, c = 'k', ms = 9., ls = "", mew = 1., label = 'Start Point' if l_one == 0 else "_no-legend_") x, y = m(y[0], x[0]) plt.plot(x, y, info['LABELNAME']) l_one += 1 if info['LABELTYPE'] == 'TWO': m.plot(x, y, c = 'c', ms = 9., ls = "", mew = 1., label = 'End Point' if l_two == 0 else "_no-legend_") x, y = m(y[0], x[0]) plt.plot(x, y, info['LABELNAME']) l_two += 1

我收到以下错误: Illegal format string "Start Point"; two linestyle symbols Illegal format string "Start Point"; two linestyle symbols

为什么我会收到这个错误,如何修复它以便我能够将字典中的文本放到图上?

After reading the the basemap tutorial on annotating and adding text, I'm still running into a few issues with this.

shapefile_info = m.readshapefile('/path/to/shapefile', 'shapefile_name') for info, shape in zip(m.points_info, m.points): print info, shape

Reading in the shapefile and printing out info (with the above code), we get this output:

{'LABELTYPE': 'ONE', 'LABELNAME': 'Start Point'} (2274311.7551607937, 759422.9640236866)

{'LABELTYPE': 'TWO', 'LABELNAME': 'End Point'} (1839892.6558604166, 947255.0800333266)

Using the code below...

from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt l_one, l_two = 0, 0 m = Basemap(projection = 'merc', llcrnrlat= -2, urcrnrlat= 52, llcrnrlon= -137,\ urcrnrlon= -58, lat_ts=40,resolution='i') m.shadedrelief() m.drawcoastlines(linewidth=0.5) m.drawcountries(linewidth=0.5) m.drawstates(linewidth=0.5) m.drawparallels(np.arange(-90, 90, 10), linewidth = 0.2, labels = [True, False, True, False], fontsize = 'x-small') m.drawmeridians(np.arange(-180, 180, 10), linewidth = 0.2, labels = [False, False, False, True], fontsize = 'x-small') m.readshapefile('/path/to/shapefile', 'shapefile_name') shapefile_info = m.readshapefile('/path/to/shapefile', 'shapefile_name') for info, shape in zip(m.points_info, m.points): x, y = zip(shape) if info['LABELTYPE'] == 'ONE': m.plot(x, y, c = 'k', ms = 9., ls = "", mew = 1., label = 'Start Point' if l_one == 0 else "_no-legend_") x, y = m(y[0], x[0]) plt.plot(x, y, info['LABELNAME']) l_one += 1 if info['LABELTYPE'] == 'TWO': m.plot(x, y, c = 'c', ms = 9., ls = "", mew = 1., label = 'End Point' if l_two == 0 else "_no-legend_") x, y = m(y[0], x[0]) plt.plot(x, y, info['LABELNAME']) l_two += 1

I get the following error: Illegal format string "Start Point"; two linestyle symbols

Why is it I get this error, and how do I go about fixing it so I'm able to put the text from the dictionary onto the plot?

最满意答案

要将一些文本放在位置(x,y)您将使用plt.text()而不是plt.plot() (因为plot绘制了线图,而不是文本)。

plt.text(x, y, text)

或者在这种情况下

plt.text(x, y, info['LABELNAME'])

To put some text at position (x,y) you would use plt.text() instead of plt.plot() (as plot plots a lineplot, not a text).

plt.text(x, y, text)

or in this case

plt.text(x, y, info['LABELNAME'])

更多推荐

本文发布于:2023-07-24 10:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1244940.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:底图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!