简体   繁体   中英

Python: Can I make the colorbar static in a plotly time series choropleth?

I am exploring the COVID cases over time in a world map. Right now, the colorscale is dynamic and the min and max change every day. I want to fix them (maybe to 0 and to the alltime high) so that I can more easily compare different dates with each other. How can I do this?

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly
from datetime import datetime
import plotly.graph_objs as go
from datetime import timedelta
import plotly.offline as offline
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
df = pd.read_csv('https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/owid-covid-data.csv')

start = datetime.strptime('Jan 1 2020', '%b %d %Y')
df['datum'] = pd.to_datetime(df.date)


df_cleaned = df[df['iso_code'].notna()]
data_slider = []

for datum in df['datum'].unique():
    df_segmented = df_cleaned[df_cleaned['datum']==datum]
    data_each_date = dict(type='choropleth',
                locations = df_segmented['iso_code'],
                z = df_segmented['new_deaths_per_million'],
                
                text = df_segmented['location'],
                colorbar = {'title':'New Deaths per Million'})
    data_slider.append(data_each_date)
 
steps = []
for i in range(len(data_slider)):
    step = dict(method='restyle',
                args=['visible',[False]*len(data_slider)],
                label='Date {}'.format(timedelta(days=1) + start)
                )
    step['args'][1][i]=True
    start = timedelta(days=1) + start
    steps.append(step)


sliders = [dict(active=0, pad={"t": 1}, steps=steps)]

layout = dict(title = 'COVID',
              geo=dict(
                       scope ='world',
                       projection = {'type':'mercator'}
                       ),
              sliders = sliders)

fig =dict(data=data_slider,layout=layout)
plotly.offline.plot(fig)

It seems to be fixed with max=80 , zmin=0 . I used this information as a reference. How to keep the color bar in a choropleth map consistent with changing data

for datum in df['datum'].unique():
    df_segmented = df_cleaned[df_cleaned['datum']==datum]
    data_each_date = dict(type='choropleth',
                locations = df_segmented['iso_code'],
                z = df_segmented['new_deaths_per_million'],

                zmax = 50, # update
                zmin = 0, # update
                text = df_segmented['location'],
                colorbar = {'title':'New Deaths per Million'})
    data_slider.append(data_each_date)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM