Skip to content

Instantly share code, notes, and snippets.

@emmanuelle
Created December 11, 2019 04:01
Show Gist options
  • Save emmanuelle/2c96b5976caa5055caca03dca1c9d089 to your computer and use it in GitHub Desktop.
Save emmanuelle/2c96b5976caa5055caca03dca1c9d089 to your computer and use it in GitHub Desktop.
Choropleth chart of scikit-image website visits
import plotly.express as px
import pandas as pd
# Analytics file downloaded from matomo, each line is a visit
df = pd.read_csv('december10-skimage.csv', encoding='utf-16le')
df['unit'] = 1
df['hour'] = pd.to_datetime(df['serverTimePretty']).dt.hour
# For each country and hour, count number of visitors
dfg = df[['unit', 'country', 'hour']].groupby(['country', 'hour']).sum()
dfg.reset_index(inplace=True)
# Data cleaning, remove unknown
dfg = dfg.query("country != 'Unknown'")
dfg.sort_values(by='hour', inplace=True)
# Plotly figure
fig = px.choropleth(dfg, locations='country', color='unit', locationmode='country names',
animation_frame='hour', range_color=(0, dfg['unit'].max()),
color_continuous_scale='RdPu')
# Add a different title for each frame
for i, frame in enumerate(fig.frames):
frame.layout.title = 'GMT time %d:00, visitors of https://scikit-image.org' %i
fig.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment