Change legend number range in Altair plot

Viewed 458

I am trying to plot time series data in a kind of "climate stripes plot" using the package Altair. The problem is that I do not know how to change the range in the legend to standardise all my plots with the same colour range and numbers in the legend. At the moment, each time I plot something the legend adapts to the range of the data.

I think the problem is with the "domain" property, maybe is not in the correct place ?

Thank you for your help :)

This is the code for the plot :

chart=alt.Chart(source).mark_rect().encode(
    x=('day:O'),
    y='subasins:N',
    color=alt.Color('90%:Q',legend=alt.Legend(title='CH4'), bin=alt.Bin(maxbins=20),
                    scale=alt.Scale(scheme='blueorange'),domain=[1830,2000]) 
    
).properties(width=100).facet(column=alt.Column('month'))

chart.show()

Plots that I get now with different scales in the legend

2 Answers

You're using the right approach with domain, it just needs to be put inside alt.Scale:

scale=alt.Scale(scheme='blueorange', domain=[1830, 2000])

When you're using a bin transform, one way to ensure the scale is consistent is to specify the bin extent:

bin=alt.Bin(maxbins=20, extent=[1830, 2000])
Related