Reputation: 888
I'm trying to use altair to recreate the world temperature plot from Climate Reanalyzer: https://climatereanalyzer.org/clim/t2_daily/?dm_id=world. The data is pretty easy to load:
import pandas as pd
import altair as alt
import json
url = "https://climatereanalyzer.org/clim/t2_daily/json/era5_world_t2_day.json"
data = requests.get(url).json()
years = []
all_temperatures = []
for year_data in data:
year = year_data['name']
temperatures = year_data['data']
temperatures = [temp if temp is not None else float('nan') for temp in temperatures]
days = list(range(1, len(temperatures) + 1))
df = pd.DataFrame({
'Year': [year] * len(temperatures),
'Day': days,
'Temperature': temperatures
})
years.append(year)
all_temperatures.append(df)
df_all = pd.concat(all_temperatures)
There are several obvious problems with the chart, but the the part I'm most curious about is the faulty clamping at 365 days:
alt.data_transformers.enable('default', max_rows=None)
chart = alt.Chart(df_all).mark_line().encode(
x=alt.X(
'Day:Q',
title='Day of the Year',
scale=alt.Scale(domain=(0, 365), clamp=True)),
y=alt.Y(
'Temperature:Q',
title='Temperature (°C)',
scale=alt.Scale(domain=(11, 18), clamp=True)),
).properties(
title='Daily World Temperatures',
width=600,
height=600
).encode(
color=alt.Color(
'Year:N',
legend=alt.Legend(
orient='bottom',
columns=10,
symbolLimit=100
)
),
)
chart
Upvotes: 1
Views: 57
Reputation: 888
After a little more digging, I found vegafusion. The following code fixed this problem:
! pip install vegafusion vegafusion-python-embed vl-convert-python
import altair as alt
alt.data_transformers.enable("vegafusion")
Upvotes: 0