有没有办法在 Altair 中自定义串联图表的位置?

Is there a way to customize the placement of concatenated charts in Altair?

我在 Altair 中有以下图表,我想知道是否有办法改变地图图表的方向。我试过使用“中心”和“间距”参数,但无济于事。我想让地图出现在图例下面,让整体图表更简洁。

这是用于生成图表的代码:

background = alt.Chart(states).mark_geoshape(
        fill='white',
        stroke='lightgray'
    ).properties(
        width=200,
        height=200
    ).project('albersUsa')

# Generate the points themselves
climate_points = alt.Chart(climate_with_temps).mark_circle(size=300).encode(
    alt.Longitude('lon:Q'),
    alt.Latitude('lat:Q'),
    alt.OpacityValue(0.25),
    alt.Color('city:N', scale=alt.Scale(scheme='dark2'), legend=None)
).properties(
    title=alt.TitleParams(
        ['Location of Coastal Cities'],
        baseline='bottom',
        orient='bottom',
        fontSize=15   
    )
)

map_chart = background + climate_points

temp_line_plot = alt.Chart(climate_with_temps).mark_line().encode(
    alt.X('month:O', axis=alt.Axis(title="Month"), sort=list(set(climate_with_temps.city))),
    alt.Y('avg_temp:Q', axis=alt.Axis(title="Average Temperature in Fahrenheit"), scale=alt.Scale(domain=[30, 95])),
    alt.Color('city:N'),
    alt.OpacityValue(1)
).properties(
    height=500,
    width=700
)

sun_square_plot = alt.Chart(climate_with_temps).mark_square().encode(
    alt.X('month:O', axis=alt.Axis(title="Month"), sort=list(set(climate_with_temps.city))),
    alt.Y('avg_temp:Q', axis=alt.Axis(title="Average Temperature in Fahrenheit"), scale=alt.Scale(domain=[30, 95])),
    alt.Color('city:N', scale=alt.Scale(scheme='dark2'), legend=alt.Legend(title='City')),
    alt.Size('sun_bin:O', sort=['0 - 50', '50 - 99', '100 - 149', '150 - 199', '200 - 249', '250 - 299', '300 - 350'], legend=alt.Legend(title='Average Hours of Sunshine')),
    alt.OpacityValue(1)
).properties(
    height=500,
    width=700
)

left_chart = sun_square_plot + temp_line_plot
final = alt.hconcat(left_chart, map_chart, center=False, spacing=0)

我认为没有直接的方法可以实现这一点,尽管您可以这样做:

import altair as alt
from vega_datasets import data

source = data.cars()

chart1 = alt.Chart(source, height=400).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
    size='Cylinders'
)
chart2 = alt.Chart(source, height=200, width=200).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
)

# Empty chart for spacing
chart3 = alt.Chart(height=180, width=180).mark_circle(opacity=0)

(chart1 | (chart3 & chart2)).configure_legend(
    orient='right',
    direction='vertical',
    offset=-220,
    symbolDirection='vertical'
)

但最简单的方法可能是在两个图表上方创建一个水平图例:

import altair as alt
from vega_datasets import data

source = data.cars()

chart1 = alt.Chart(source).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin',
    size='Cylinders'
)

chart2 = alt.Chart(source).mark_circle(size=60).encode(
    x='Horsepower',
    y='Miles_per_Gallon',
    color='Origin'
)

(chart1 | chart2).configure_legend(
    orient='top'
)