【发布时间】:2022-01-03 17:03:44
【问题描述】:
我正在尝试使用 plotly 为我的 choropleth 地图添加一个滑块。我一直无法在 google colab 中看到地图,但 jupyter notebook 运行良好。
我有一个数据集,其中包含多年来世界每个国家/地区的值。我想在下面做一个滑块,这样你就可以滚动浏览这些年并查看颜色的变化。我已经能够从一年中获取数据,但我无法让滑块出现在我的地图下方,并且我收到一条粉红色的大错误消息,指出该值正试图在 DataFrame 的切片副本上设置。
我能做些什么来解决这个问题吗?请在附件中找到我的代码
pip install chart-studio
import pandas as pd
import chart_studio.plotly as py
import plotly.offline as po
import matplotlib.pyplot as plt
%matplotlib inline
import plotly
import plotly.graph_objs as go
import plotly.offline as offline
from plotly.graph_objs import *
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, pilot
df=pd.read_csv("https://raw.githubusercontent.com/jamesjeffery77/jamesjeffery77.github.io/main/share-electricity-low-carbon_fullDataset.csv")
year=1985
### create empty list for data object:
data_slider = []
# Populate data object
for year in df.year.unique():
#select year
df2=df[(df['year']==year)]
for col in df2.columns:
df2[col] = df2[col].astype(str)
#dictionary with date for current year
data_one_year = dict(
type='choropleth',
locations=df2['code'],
z=df2['percentage'].astype(float),
text=df2['country'],)
#add data to next year
data_slider.append(data_one_year)
#steps for the slider
steps = []
for i in range(len(data_slider)):
step=dict(method='restyle',
args=['visible',[False] * len(data_slider)],
label='year {}'.format(i+1985))
step['args'][1][i] = True
steps.append(step)
## create the sliders object from the steps
sliders = [dict(active=0, pad={"t": 1}, steps=steps)]
#layout such that there is a global view
layout = dict(title = 'Global GDP - natural earth',
geo = dict( projection = {'type':'natural earth'},
showlakes = True,
lakecolor = 'rgb(0,191,255)'))
fig = dict(data=data_slider, layout=layout)
#plot graph
plotly.offline.iplot(fig)
【问题讨论】:
标签: python plotly slider google-colaboratory choropleth