简体   繁体   English

如果 altair 中有多个 y 轴,如何增加标签间距和刻度间距?

[英]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我正在尝试增加绘图的易读性,因为 y 轴标签(左侧)已经重叠并调整它们之间的刻度范围和间隔。请帮助我这样做多轴组合图

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:在技​​术上可以使用offset参数移动第三个 y 轴:

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.但是,我会考虑制作单独的图,因为阅读具有多个 y 轴的图表可能会变得困难。 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')

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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