如何在图例中添加图例?

时间:2019-02-22 13:20:02

标签: python legend choropleth

我想在我的Choropleth地图中添加一个图例。我已经使用geoplotlib库进行了绘制,但是,在Internet上找不到关于该库的太多信息。因此,我无法找到一种方法来显示根据颜色渐变显示最大值和最小值的图例。你知道我该怎么做吗?

import geoplotlib
from geoplotlib.colors import ColorMap
from geoplotlib.utils import BoundingBox
import json


#Define function get_color, returns the colour in which each county has to bee painted according to the number of TCAS RA (stored in pos variable) and the color map chosen (stored in the cmap variable).
def get_color(properties):
    key = str(int(properties['STATE'])) + properties['COUNTY']
    if key in pos5:
        return cmap.to_color(pos5.get(key), 2, 'lin')
    else:
        return [0, 0, 0, 0]

#Open and load pos2.json file.
with open ('C:\\Users\\W7\\PycharmProjects\\Test\\pos5.json') as fin:
    pos5 = json.load(fin)

#Select a color map
cmap = geoplotlib.colors.ColorMap('Reds', alpha=255, levels=10)

#We draw the polygons that size counties, painting them according to get_color
geoplotlib.geojson('C:\\Users\\W7\\PycharmProjects\\Test\\gz_2010_us_050_00_20m.json',
                   fill=True,
                   color=get_color,
                   f_tooltip=lambda properties: properties['NAME'])

#We paint the borders of each polygon in black
geoplotlib.geojson('C:\\Users\\W7\\PycharmProjects\\Test\\gz_2010_us_050_00_20m.json',
                   fill=False,
                   color=[0, 0, 0, 64])

#Open and read the file containing the airports in which VJ has experienced a TCAS RA.
data=geoplotlib.utils.read_csv("C:\\Users\\W7\\PycharmProjects\\Test\\seven.csv")

#Print points to the map
geoplotlib.dot(data,color='r',point_size=1.8)

#Print name of the airports
geoplotlib.labels(data,'name',color=[255,0,0,255],font_size=12,anchor_x='center')

#Focus on USA Visualization
geoplotlib.set_bbox(geoplotlib.utils.BoundingBox.USA)

#Show the map
geoplotlib.show()

非常感谢您!

0 个答案:

没有答案