1

我想用 X 在这里制作的代码创建一个地图。

所以我尝试使用以下方法绘制我的地图geoviews

import os, numpy as np, pandas as pd, cartopy.crs as ccrs, bokeh
import holoviews as hv, geoviews as gv, datashader as ds

from colorcet import bmy
from holoviews.util import Dynamic
from holoviews.operation.datashader import rasterize, datashade

hv.extension('bokeh', width=100)

from ast import literal_eval
dictlist = literal_eval(df.results[0])
candidates = [result['name'] for result in dictlist]

def isolate_result(candidate,df_results):
    '''get results of a candidate'''
    for index, value in df_results.items():
        result = [result['pct'] for result in literal_eval(value) if result['name'] == candidate][0]
        df_results.at[index] = result

def get_df(candidate):
    df = gpd.read_file('./data/2012-Election.json')
    df['results'] = get_result(candidate, df['results'])
    return df

def get_plot(ticker):
    df = get_df(ticker)
    merged_json = json.loads(df.to_json())#Convert to String like object.
    json_data = json.dumps(merged_json)
    geosource = GeoJSONDataSource(geojson = json_data)

    #Input GeoJSON source that contains features for plotting.
    palette = brewer['YlGnBu'][8]#Reverse color order so that dark blue is highest turnout.
    palette = palette[::-1]#Instantiate LinearColorMapper that linearly maps numbers in a range, into a sequence of colors.
    color_mapper = LinearColorMapper(palette = palette, low = 0, high = 40)#Define custom tick labels for color bar.
    tick_labels = {'0': '0%', '5': '5%', '10':'10%', '15':'15%', '20':'20%', '25':'25%', '30':'30%','35':'35%', '40': '>40%'}#Create color bar. 
    color_bar = ColorBar(color_mapper=color_mapper, label_standoff=8,width = 500, height = 20,
    border_line_color=None,location = (0,0), orientation = 'horizontal', major_label_overrides = tick_labels)#Create figure object.

    p = figure(title = 'Election Results', plot_height = 600 , plot_width = 950, toolbar_location = None)
    p.xgrid.grid_line_color = None
    p.ygrid.grid_line_color = None#Add patch renderer to figure. 
    p.patches('xs','ys', source = geosource,fill_color = {'field' :'resuts', 'transform' : color_mapper},
              line_color = 'black', line_width = 0.25, fill_alpha = 1)#Specify figure layout.
    p.add_layout(color_bar, 'below')#Display figure inline in Jupyter Notebook.
    output_notebook()#Display figure.
    show(p)
    return p

interact = pn.interact(get_plot, ticker=candidates)

pn.Row(
    pn.Column(title, interact[0]),
    interact[1]
)

但是,当我将地图绘制出来时get_plot(),它工作得很好。

4

0 回答 0