简体   繁体   English

Altair 多面直方图 - 独立尺度

[英]altair faceted histogram - independent scales

Let's assume I have a dataset where the variables can be scaled quite differently from each other.假设我有一个数据集,其中变量的缩放比例可以完全不同。 I want to create histograms for each of the numeric variables.我想为每个数字变量创建直方图。 I am trying to make the x and y scales independent from each other so that the scales will not affect the visual quality.我试图使 x 和 y 尺度彼此独立,以便尺度不会影响视觉质量。 But even when I use resolve_scale() it only makes the y-scale independent, while x is still common among all the variables.但即使我使用resolve_scale()它也只会使 y 尺度独立,而 x 在所有变量中仍然很常见。 This can be seen from the figure below which can be obtained by applying the code.这可以从下图看出,可以通过应用代码获得。 Is this the desired behavior or am I missing something?这是所需的行为还是我错过了什么?

My question is:我的问题是:
1. How can I make the x scale independent? 1. 如何使 x 比例独立?
2. How can I make the title get closer to the plot? 2. 如何让标题更贴近剧情?

Thanks for your help.谢谢你的帮助。

version: python altair 4.0版本:python altair 4.0

alt.__version__
'4.0.1'
import altair as alt

data = alt.datasets.load_dataset('flights-2k')
chosen_origin_airports = data.groupby('origin').size().sort_values(ascending=False).head(12).index.tolist()
data = data[data.origin.isin(chosen_origin_airports)]
data.loc[data.origin=='BWI', 'delay']  = data.loc[data.origin=='BWI', 'delay'] * (10000)

alt.Chart(data=data).mark_bar().encode(
    x = alt.X('delay:Q', 
              axis=alt.Axis(title=''), 
              scale=alt.Scale(zero=False),
              bin=alt.Bin(maxbins=20)),
    y = alt.Y('count():Q', 
              axis=alt.Axis(title='')),
    color = alt.Color('origin:N')
).properties(
    width=130,
    height=130
).facet(
    alt.Column('origin:N', sort = alt.EncodingSortField(order=None)),
    align= 'all',
    padding=0,
    columns=4,
    spacing=0
).properties(
    title=''
).configure_title(
    fontSize=20,
    font='Courier',
    anchor='middle',
    color='gray',
    align='left'
).configure_header(
    title=None,
    titleColor='green',
    titleFontSize=14,
    labelColor='forestgreen',
    labelFontSize=14
).resolve_axis(
    x='independent',
    y='independent'
).resolve_scale(
    x='independent', 
    y='independent'
)

分面直方图

Your scales are independent, but your binnings are not.你的尺度是独立的,但你的分档不是。 Unfortunately, the Vega-Lite grammar provides no easy way to define a bin transform that applies different bin parameters to different subsets of data, so you'll have to have to manually use a distinct bin transform for each panel of the chart.不幸的是,Vega-Lite 语法没有提供简单的方法来定义将不同的 bin 参数应用于不同数据子集的 bin 变换,因此您必须手动为图表的每个面板使用不同的 bin 变换。

I would probably do something like this:我可能会做这样的事情:

chart = alt.Chart(data).mark_bar().encode(
    x = alt.X('delay:Q', 
              axis=alt.Axis(title=''), 
              scale=alt.Scale(zero=False),
              bin=alt.Bin(maxbins=20)),
    y = alt.Y('count():Q', 
              axis=alt.Axis(title='')),
    color = alt.Color('origin:N')
).properties(
    width=130,
    height=130
)

alt.ConcatChart(
    concat=[
      chart.transform_filter(alt.datum.origin == value).properties(title=value)
      for value in sorted(data.origin.unique())
    ],
    columns=4
).configure_title(
    fontSize=20,
    font='Courier',
    anchor='middle',
    color='gray',
    align='left'
).resolve_axis(
    x='independent',
    y='independent'
).resolve_scale(
    x='independent', 
    y='independent'
)

在此处输入图片说明

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

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