Reputation:
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)
Upvotes: 3
Views: 951
Reputation: 35115
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)
Upvotes: 2