【发布时间】:2018-10-25 13:26:44
【问题描述】:
我正在尝试重新创建 plotlys 示例页面中给出的世界 Choropleth 地图:https://plot.ly/python/choropleth-maps/,目的是重用一些代码,但更改通知阴影和标签的列。
但是,当我运行示例中给出的确切代码时,我收到以下错误。
plotly.exceptions.PlotlyError:因为您没有在调用中提供“file_id”,我们假设您正在尝试从 url 中获取图形。您提供了网址“”,我们希望它以“https://plot.ly”开头。 运行此函数的帮助以获取更多信息。
我不知道这个错误是从哪里引起的,我的问题是如何调整代码以便离线生成上述数字?其次,有没有一种简单的方法可以将图形直接保存到 png 中?抱歉,如果这是微不足道的,我对这个包完全陌生。
代码如下:
import plotly.plotly as py
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')
data = [dict(
type='choropleth',
locations=df['CODE'],
z=df['GDP (BILLIONS)'],
text=df['COUNTRY'],
colorscale=[[0, "rgb(5, 10, 172)"], [0.35, "rgb(40, 60, 190)"], [0.5, "rgb(70, 100, 245)"],\
[0.6, "rgb(90, 120, 245)"], [0.7, "rgb(106, 137, 247)"], [1, "rgb(220, 220, 220)"]],
autocolorscale=False,
reversescale=True,
marker=dict(
line=dict(
color='rgb(180,180,180)',
width=0.5
)),
colorbar=dict(
autotick=False,
tickprefix='$',
title='GDP<br>Billions US$'),
)]
layout = dict(
title='2014 Global GDP<br>Source:\
<a href="https://www.cia.gov/library/publications/the-world-factbook/fields/2195.html">\
CIA World Factbook</a>',
geo=dict(
showframe=False,
showcoastlines=False,
projection=dict(
type='Mercator'
)
)
)
fig = dict(data=data, layout=layout)
py.iplot(fig,validate=False, filename='d3-world-map')
【问题讨论】:
标签: python pandas plotly visualization choropleth