有没有办法手动偏移连接 and/or 在 Altair 中将连接线添加到连接图表?
Is there a way to offset concatenations manually and/or add connecting lines to concatenated charts in Altair?
我有以下串联图表:
生成的代码如下:
def quarterly_avg_bar_graph(city):
data = climate_with_seasons[climate_with_seasons['city'] == city]
grouped = data.groupby('season')[['sunshine']].mean().reset_index()
chart = alt.Chart(grouped).mark_bar().encode(
alt.X('season:N', sort=['spring', 'summer', 'autumn', 'winter']),
alt.Y('sunshine:Q'),
alt.Color('season:N', scale=alt.Scale(scheme='pastel1'), sort=['spring', 'summer', 'autumn', 'winter'], legend=None)
).properties(
height=75,
width=75
)
return chart
seattle = quarterly_avg_bar_graph('Seattle')
chicago = quarterly_avg_bar_graph('Chicago')
sf = quarterly_avg_bar_graph('San Francisco')
houston = quarterly_avg_bar_graph('Houston')
miami = quarterly_avg_bar_graph('Miami')
ny = quarterly_avg_bar_graph('New York')
background = alt.Chart(states).mark_geoshape(
fill='white',
stroke='lightgray'
).properties(
width=300,
height=300
).project('albersUsa')
# Generate the points themselves
climate_points = alt.Chart(climate).mark_square(color='red').encode(
alt.Longitude('lon:Q'),
alt.Latitude('lat:Q'),
alt.OpacityValue(0.05),
)
text = climate_points.mark_text(
dx=15,
dy=10
).encode(
text='city:N'
)
map_chart = background + climate_points + text
left = seattle & sf
right = ny & miami
middle = alt.vconcat(alt.vconcat(chicago, map_chart, center=True), houston, center=True)
final_chart = alt.hconcat(left, middle, right, center=True)
final_chart.configure_square(size=150)
我想让地理位置和条形图之间的联系更清楚一点(将顶部条形图向右移动一点,右下角移动一点,等等),以及添加一条线将每个城市广场标记连接到其各自的条形图。这可以在 Altair 中本地完成吗?
您可以使用 spacing
参数控制串联对象之间的间距:
import altair as alt
from vega_datasets import data
source = data.cars()
points = alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color='Origin:N'
)
bars = alt.Chart(source).mark_bar().encode(
y='Origin:N',
color='Origin:N',
x='count(Origin):Q'
)
alt.vconcat(points, bars, spacing=200)
不幸的是,我认为您不能添加跨越图表之间 space 的注释。您可以添加一条线到网格 by using clip
and limiting the domain as in the documentation 之外,但它不会跨越图表之间的间距(您可以使用这条线而不是间距将图表分开,但这需要很多恐怕摆弄)。
我有以下串联图表:
生成的代码如下:
def quarterly_avg_bar_graph(city):
data = climate_with_seasons[climate_with_seasons['city'] == city]
grouped = data.groupby('season')[['sunshine']].mean().reset_index()
chart = alt.Chart(grouped).mark_bar().encode(
alt.X('season:N', sort=['spring', 'summer', 'autumn', 'winter']),
alt.Y('sunshine:Q'),
alt.Color('season:N', scale=alt.Scale(scheme='pastel1'), sort=['spring', 'summer', 'autumn', 'winter'], legend=None)
).properties(
height=75,
width=75
)
return chart
seattle = quarterly_avg_bar_graph('Seattle')
chicago = quarterly_avg_bar_graph('Chicago')
sf = quarterly_avg_bar_graph('San Francisco')
houston = quarterly_avg_bar_graph('Houston')
miami = quarterly_avg_bar_graph('Miami')
ny = quarterly_avg_bar_graph('New York')
background = alt.Chart(states).mark_geoshape(
fill='white',
stroke='lightgray'
).properties(
width=300,
height=300
).project('albersUsa')
# Generate the points themselves
climate_points = alt.Chart(climate).mark_square(color='red').encode(
alt.Longitude('lon:Q'),
alt.Latitude('lat:Q'),
alt.OpacityValue(0.05),
)
text = climate_points.mark_text(
dx=15,
dy=10
).encode(
text='city:N'
)
map_chart = background + climate_points + text
left = seattle & sf
right = ny & miami
middle = alt.vconcat(alt.vconcat(chicago, map_chart, center=True), houston, center=True)
final_chart = alt.hconcat(left, middle, right, center=True)
final_chart.configure_square(size=150)
我想让地理位置和条形图之间的联系更清楚一点(将顶部条形图向右移动一点,右下角移动一点,等等),以及添加一条线将每个城市广场标记连接到其各自的条形图。这可以在 Altair 中本地完成吗?
您可以使用 spacing
参数控制串联对象之间的间距:
import altair as alt
from vega_datasets import data
source = data.cars()
points = alt.Chart(source).mark_point().encode(
x='Horsepower:Q',
y='Miles_per_Gallon:Q',
color='Origin:N'
)
bars = alt.Chart(source).mark_bar().encode(
y='Origin:N',
color='Origin:N',
x='count(Origin):Q'
)
alt.vconcat(points, bars, spacing=200)
不幸的是,我认为您不能添加跨越图表之间 space 的注释。您可以添加一条线到网格 by using clip
and limiting the domain as in the documentation 之外,但它不会跨越图表之间的间距(您可以使用这条线而不是间距将图表分开,但这需要很多恐怕摆弄)。