简体   繁体   中英

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

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

在此处输入图像描述

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM