1

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

enter image description here

2
  • If it's all helpful, setting domain=(300, 365) works, but I don't want to clamp the first 300 days.
    – Don
    Commented Jun 6 at 21:45
  • The code you pasted above works for me as expected and the chart I see does have the maximum x-axis values as 365. Installing Vegafusion shouldn't change this, but maybe your version of Altair was also updated which changed some default (depending on how old it was previously)? Also note that clamp has no effect here, but nice could be needed to set an exact axis values in some cases, although in your case it works out of the box for me on altair 5.3 Commented Jun 7 at 9:12

1 Answer 1

0

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")

Not the answer you're looking for? Browse other questions tagged or ask your own question.