我试图在JupyterLab上运行以下代码,但出现值错误。我从https://plot.ly/python/choropleth-maps/处获得了代码,但我改用离线方式。有人可以帮忙吗?谢谢!
import plotly.offline as py
py.init_notebook_mode(connected=True)
import plotly.graph_objs as go
import plotly.tools as tls
import warnings
from collections import Counter
# Load data frame and tidy it.
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')
fig = go.Figure(data=go.Choropleth(
locations=df['code'], # Spatial coordinates
z = df['total exports'].astype(float), # Data to be color-coded
locationmode = 'USA-states', # set of locations match entries in `locations`
colorscale = 'Reds',
colorbar_title = "Millions USD",
))
fig.update_layout(
title_text = '2011 US Agriculture Exports by State',
geo_scope='usa', # limite map scope to USA
)
fig.show()
答案 0 :(得分:1)
我将代码更改为此,并且有效:
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv')
data = [dict(type = 'choropleth',
colorscale = 'Reds',
locations=df['code'], # Spatial coordinates
z = df['total exports'].astype(float), # Data to be color-coded
locationmode = 'USA-states', # set of locations match entries in `locations
colorbar = {'title':"Millions USD"},
)]
layout = dict(title = '2011 US Agriculture Exports by State',
geo = dict(scope='usa', showlakes = True)) # limite map scope to USA)
fig = dict( data=data, layout=layout )
url = iplot( fig, filename='d2-cloropleth-map' )