我试图提高绘图的易读性,因为两个y轴标签(左侧)已经重叠,并调整了它们之间的刻度范围和间隔.请帮助我这样做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()

推荐答案

从技术上讲,可以使用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()

enter image description here

然而,我会考虑绘制单独的图,因为读取具有多个y轴的图表可能会变得困难.如果垂直叠加图,仍然很容易比较三种不同测量的趋势:

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

Python相关问答推荐

如何删除索引过go 的lexsort深度可能会影响性能?' &>

删除任何仅包含字符(或不包含其他数字值的邮政编码)的观察

Pandas - groupby字符串字段并按时间范围 Select

用合并列替换现有列并重命名

如何在solve()之后获得症状上的等式的值

numpy卷积与有效

在pandas中使用group_by,但有条件

将JSON对象转换为Dataframe

转换为浮点,pandas字符串列,混合千和十进制分隔符

考虑到同一天和前2天的前2个数值,如何估算电力时间序列数据中的缺失值?

使用Python和文件进行模糊输出

ruamel.yaml dump:如何阻止map标量值被移动到一个新的缩进行?

如何在Great Table中处理inf和nans

为什么Python内存中的列表大小与文档不匹配?

使用嵌套对象字段的Qdrant过滤

每次查询的流通股数量

Python OPCUA,modbus通信代码运行3小时后出现RuntimeError

高效生成累积式三角矩阵

对数据帧进行分组,并按组间等概率抽样n行

用0填充没有覆盖范围的垃圾箱