[英]Repeating an axis in a faceted Altair chart
Given a simple, faceted chart like:给定一个简单的多面图表,例如:
import altair as alt
data = alt.Data(values = [
{ "category" : "a", "x" : 1, "y" : 2 },
{ "category" : "a", "x" : 2, "y" : 4 },
{ "category" : "b", "x" : 1, "y" : 3 },
{ "category" : "b", "x" : 2, "y" : 5 }
])
alt.Chart(data).mark_point().encode(x = "x:Q", y = "y:Q").facet(
row = "category:O"
)
How do you have the x axis appear for each subchart, rather than just once at the bottom?你如何让每个子图表的 x 轴出现,而不是只在底部出现一次? This is to improve readability when there are a lot of subcharts...
这是为了在有很多子图表时提高可读性......
Unfortunately, there's no way to make the x-axis appear in multiple charts when using the row encoding.不幸的是,在使用行编码时,无法让 x 轴出现在多个图表中。 As a workaround, you can manually vconcat charts based on filtered data:
作为一种解决方法,您可以根据过滤后的数据手动创建 vconcat 图表:
chart = alt.Chart(data).mark_point().encode(x="x:Q", y="y:Q")
alt.vconcat(
chart.transform_filter(alt.datum.category == 'a'),
chart.transform_filter(alt.datum.category == 'b')
)
To avoid writing out the column values manually, you can generate the different subcharts using Python tools;为避免手动写出列值,您可以使用 Python 工具生成不同的子图表; for example, this is equivalent to the above:
例如,这相当于上面的:
df = pd.DataFrame.from_records([
{ "category" : "a", "x" : 1, "y" : 2 },
{ "category" : "a", "x" : 2, "y" : 4 },
{ "category" : "b", "x" : 1, "y" : 3 },
{ "category" : "b", "x" : 2, "y" : 5 }
])
chart = alt.Chart(df).mark_point().encode(x="x:Q", y="y:Q")
alt.vconcat(
*(chart.transform_filter(alt.datum.category == val)
for val in df['category'].unique())
)
Here's a simple way to repeat the axis, with a drawback:这是重复轴的简单方法,但有一个缺点:
import altair as alt
data = alt.Data(values = [
{ "category" : "a", "x" : 1, "y" : 2 },
{ "category" : "a", "x" : 2, "y" : 4 },
{ "category" : "b", "x" : 1, "y" : 3 },
{ "category" : "b", "x" : 2, "y" : 5 }
])
alt.Chart(data).mark_point().encode(x = "x:Q", y = "y:Q").facet(
row = "category:O"
).resolve_scale(x='independent')
But: If the facets do not have identical x-axis ranges, this will also de-couple them!但是:如果刻面没有相同的 x 轴范围,这也会使它们解耦! There is, to my knowledge, no simple way of just repeating the axis without making the facets independent.
据我所知,没有简单的方法可以在不使小平面独立的情况下重复轴。 But you can always fix the range to achieve that.
但是您始终可以修复范围以实现这一目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.