【问题标题】:slider for choropleth map plotly shows error - <ipython-input-17-c5022c9e0aab>:13: SettingWithCopyWarning:等值线图的滑块显示错误 - <ipython-input-17-c5022c9e0aab>:13: SettingWithCopyWarning:
【发布时间】: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


    【解决方案1】:

    您可以绕过value is trying to be set on a copy of a slice from a DataFrame 警告,您可以一步将列设置为字符串 - 您无需分别循环遍历每个列名。

    # Populate data object
    for year in df.year.unique():
        #select year
        df2=df[(df['year']==year)]
        
        df2.columns = df2.columns.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)
    

    在此更改之后,我在Google Colab 中运行了您的代码,并且能够显示地图。

    【讨论】:

    • 感谢您的回复,德里克!是的,我也设法到达了那个阶段,但是滑块似乎仍然没有出现在底部。看起来它只是从我的数据集中获取一年的数据,但是我想包含几年的数据,这样我就可以使用滑块来表示更改。我只是无法弄清楚为什么我的滑块代码不起作用! :( 谢谢!
    • @jamesj477 如果您还没有弄清楚这一点,我很乐意再看看是什么阻止了滑块工作
    猜你喜欢
    • 2021-07-02
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2022-11-08
    • 1970-01-01
    • 2020-09-14
    相关资源
    最近更新 更多