Search code examples
pythonaltair

How to increse label spacing and tick spacing in case of multiple y-axis in altair?


I am trying to increase the legibility of the plot since both the y-axis labels(on left) have got overlapped and adjust the tick range and interval between them.Kindly help me in doing so multi axis combo chart

import altair as alt
from vega_datasets import data
source = data.seattle_weather()
base = alt.Chart(source).encode( alt.X('month(date):T', axis=alt.Axis(title=None)))
area = base.mark_area(opacity=0.3, color='#57A44C').encode(
    alt.Y('average(temp_max)',
          axis=alt.Axis(title='Avg. Temperature (°C)', titleColor='#57A44C')),
    alt.Y2('average(temp_min)'))

line = base.mark_line(stroke='#5276A7', interpolate='monotone').encode(
    alt.Y('average(precipitation)',
          axis=alt.Axis(title='Precipitation (inches)', titleColor='#5276A7')))

line2 = base.mark_line(stroke='#5276A7', interpolate='monotone').encode(
    alt.Y('average(wind)',
          axis=alt.Axis(title='wind', titleColor='#5276A7')))

alt.layer(area, line,line2).resolve_scale(y = 'independent').configure_axisY()

Solution

  • It is technically possible to shift the third y-axis with the offset parameter:

    import altair as alt
    from vega_datasets import data
    
    
    source = data.seattle_weather()
    base = alt.Chart(source).encode( alt.X('month(date):T', axis=alt.Axis(title=None)))
    area = base.mark_area(opacity=0.3, color='#57A44C').encode(
        alt.Y('average(temp_max)',
              axis=alt.Axis(title='Avg. Temperature (°C)', titleColor='#57A44C')),
        alt.Y2('average(temp_min)'))
    
    line = base.mark_line(stroke='#5276A7', interpolate='monotone').encode(
        alt.Y('average(precipitation)',
              axis=alt.Axis(title='Precipitation (inches)', titleColor='#5276A7')))
    
    line2 = base.mark_line(stroke='coral', interpolate='monotone').encode(
        alt.Y('average(wind)',
              axis=alt.Axis(title='wind', titleColor='coral', offset=60)))
    
    alt.layer(area, line,line2).resolve_scale(y = 'independent').configure_axisY()
    

    enter image description here

    However, I would consider making separate plots instead since it can become difficult to read a chart with multiple y-axis. If you stack the plots vertically, it is still easy to compare the trend for the three different measurements:

    base = alt.Chart(source, height=150).encode(alt.X('month(date):T', axis=None))
    area = base.mark_area(opacity=0.3, color='#57A44C').encode(
        alt.Y('average(temp_max)',
              axis=alt.Axis(title='Avg. Temperature (°C)', titleColor='#57A44C')),
        alt.Y2('average(temp_min)'))
    
    line = base.mark_line(stroke='#5276A7', interpolate='monotone').encode(
        alt.Y('average(precipitation)',
              axis=alt.Axis(title='Precipitation (inches)', titleColor='#5276A7')))
    
    line2 = base.mark_line(stroke='coral', interpolate='monotone').encode(
        alt.Y('average(wind)',
              axis=alt.Axis(title='wind', titleColor='coral')),
        alt.X('month(date):T')
    )
    
    alt.vconcat(area, line, line2, spacing=0).resolve_scale(x='shared')
    

    enter image description here