我想用情节来构建一个等值区域图。我有一个包含州代码的数据框和一个如下所示的计数:
count state
0 1 AK
1 9 AL
2 2 AR
3 11 AZ
4 31 CA
...
我尝试过以下方法:
from plotly.offline import init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
test_data = [dict(type='choropleth',
autocolorscale=False,
locations=df['state'],
z=df['count'],
locationmode='USA-states',
marker=dict(
line=dict(
color='rgb(255,255,255)',
width=2
)),
colorbar=dict(
title='Choropleth Map Test')
)]
layout = dict(
title='Test title',
geo = dict(
scope='usa',
projection=dict(type='albers usa'),
snowflakes=True,
lakecolor='rgb(255,255,255)'),
)
fig = dict(data=data, layout=layout)
py.iplot(fig,filename='d3-cloropleth-map')
然后我收到错误:
PlotlyDictValueError: 'data' has invalid value inside 'figure'
Path To Error: ['data']
Current path: []
Current parent object_names: []
Additionally:
role: object
During handling of the above exception, another exception occurred:
有人可以指出我正确的方向吗?
答案 0 :(得分:0)
from plotly.offline import init_notebook_mode,plot, iplot
您已经从plotly.offline导入了iplot,应该直接调用该函数。使用iplot()代替py.iplot()。
py似乎被定义为您在上面列出的代码中未包含的其他内容。
答案 1 :(得分:0)
下面的代码应该可以工作:
导入库
import pandas as pd
from plotly import __version__
%matplotlib inline
import plotly.offline as pyo
import plotly.graph_objs as go
from plotly.offline import iplot
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)
init_notebook_mode(connected=True)
cf.go_offline()
创建示例数据
state = ['AK', 'AL', 'AR', 'AZ', 'CA']
count = [1,9,2,11,31]
df = pd.DataFrame({'state':state, 'count':count})
df.head(2)
下面的代码来自上面的问题
注意:snowflakes=True
是commented out
test_data = [dict(type='choropleth',
autocolorscale=False,
locations=df['state'],
z=df['count'],
locationmode='USA-states',
marker=dict(
line=dict(
color='rgb(255,255,255)',
width=2
)),
colorbar=dict(
title='Choropleth Map Test')
)]
layout = dict(
title='Test title',
geo = dict(
scope='usa',
projection=dict(type='albers usa'),
#snowflakes=True,
lakecolor='rgb(255,255,255)'),
)
fig = dict(data=test_data, layout=layout)
pyo.iplot(fig,filename='d3-cloropleth-map')
答案 2 :(得分:0)
可能是问题所在...尝试以这种方式安装:
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org plotly
答案 3 :(得分:-1)
# Code works, but no MAP
Must Restart and run all
!pip install cufflinks
!pip install chart_studio
import chart_studio
import pandas as pd
from plotly import __version__
%matplotlib inline
import plotly.offline as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs
from plotly.offline import init_notebook_mode, plot, iplot
import cufflinks as cf
init_notebook_mode(connected=True)
cf.go_offline()
# scl = [[0.0, 'rgb(242,240,247)'],[0.2, 'rgb(218,218,235)'],[0.4, 'rgb(188,189,220)'],\
# [0.6, 'rgb(158,154,200)'],[0.8, 'rgb(117,107,177)'],[1.0, 'rgb(84,39,143)']]
#Create sample data
state = ['AK', 'AL', 'AR', 'AZ', 'CA']
count = [1,9,2,11,31]
df = pd.DataFrame({'state':state, 'count':count})
#df.head(2)
#Code below is from the question above
#Note: snowflakes=True is commented out
test_data = [dict(type='choropleth',
autocolorscale=False,
locations=df['state'],
z=df['count'],
locationmode='USA-states',
marker=dict(
line=dict(
color='rgb(255,255,255)',
width=2
)),
colorbar=dict(
title='Choropleth Map Test')
)]
layout = dict(
title='Test title',
geo = dict(
scope='usa',
projection=dict(type='albers usa'),
#snowflakes=True,
lakecolor='rgb(255,255,255)'),
)
fig = dict(data=test_data, layout=layout)
py.iplot(fig,filename='d3-cloropleth-map')