独立处理图中的数据(?)图例 - Python

时间:2017-06-22 15:44:24

标签: python matplotlib plot matplotlib-basemap

假设我们从一个经常更改值的源导入shapefile。其中一个shapefile包含1到5之间的值,但大小和值不同(例如,第一次从源中获取shapefile,它包含1个值。下次从源中抓取它时,它包含4个值)另一个shapefile包含动物名称,但只有3种可能性,就像第一个shapefile一样,它可以有多少个值。

让我们说shapefile1包含' 2'和' 3',而shapefile2包含' Horse'当我们第一次抓取zip文件时。在传说中,我只想要' 2',' 3'和'马'显示正确的图标(参见下面的脚本)。下次我们运行程序并获取zip文件夹时,shapefile1包含' 1',' 2',' 4',' 5'和shapefile2包含“Bird'”,“Cat&#39 ;.图例应该有' 1',' 2',' 4',' 5',' Bird',& #39;目录'

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import requests, zipfile, io

def plot_stuff_sf1(x, y, color):
    m.plot(x, y, c = color, mec = 'k', m = 'o', ms = 5., ls = None, mew = 1.)

def plot_stuff_sf2(x, y, marker):
    m.plot(x, y, color = 'c', mec = 'k', m = marker, ms = 5., ls = None, mew = 1.)

#gets zipped file and stores on computer
zip_file_url = 'zippedfile/location'
r = requests.get(zip_file_url)
z = zipfile.ZipFile(io.BytesIO(r.content))
z.extractall('/path/to/save/files')

m = Basemap(projection = 'merc', llcrnrlat= 90, urcrnrlat= -90,
             llcrnrlon= -180, urcrnrlon= 180, lat_ts=40,resolution='i') 
m.readshapefile('/path/to/shapefile', 'name')
points_info = m.readshapefile('/path/to/shapefile', 'name')
for info, shape in zip(m.name_info, m.name):
    x, y = zip(shape)
    if info['NUMBER'] == '1':
        plot_stuff_sf1(x, y, 'c')
    elif info['NUMBER'] == '2':
        plot_stuff_sf1(x, y, 'm')
    elif info['NUMBER'] == '3':
        plot_stuff_sf1(x, y, 'g')
    elif info['NUMBER'] == '4':
        plot_stuff_sf1(x, y, 'r')
    elif info['NUMBER'] == '5':
        plot_stuff_sf1(x, y, 'k')

m.readshapefile('/path/to/shapefile2', 'name2')
name_info2 = m.readshapefile('/path/to/shapefile2', 'name2')
for info, shape in zip(m.name_info2, m.name2):
    x, y = zip(shape)
    if info['VALUE'] == 'Bird':
        plot_stuff_sf2(x, y, 'o')
    elif info['VALUE'] == 'Horse':
        plot_stuff_sf2(x, y, 'D')
    elif info['VALUE'] == 'Cat':
        plot_stuff_sf2(x, y, '>')

我没有包含传奇代码,因为那是我被绊倒的地方。我查看了文档并尝试了plt.legend(handles = [...]),但由于某种原因它无法正常工作(即使我从文档中复制/粘贴代码并将其与我的脚本集成)。

0 个答案:

没有答案