【问题标题】:Reverse log axis in AltairAltair 中的反向对数轴
【发布时间】:2022-11-05 01:21:11
【问题描述】:

我正在尝试在 Altair 中绘制 this 类型的图表。是否有一种惯用的方式来配置水平轴,即它是对数的并且“从 100 指向 0”。我想在 100 左右而不是 0 左右获得高分辨率的数字。

Here 人们正在尝试使用其他绘图库来做同样的事情。

【问题讨论】:

  • 我认为没有办法直接在 Altair 中实现这一点,但您链接到的帖子似乎显示了如何手动进行转换,然后您可以根据需要使用 axis=alt.Axis(values=...) 在 Altair 中重新标记轴

标签: altair vega-lite


【解决方案1】:

您可以使用type 规模definition 参数来实现此目的。 如果将type = "pow"corresponding exponent 值一起使用,则可以灵活调整X 轴和Y 轴:

scale=alt.Scale(type="pow", exponent=np.e)

# Create dummy data
x = np.linspace(0, 1, 100)
y = np.random.exponential(1, 100) # Distribution to verify the inverse-log (exp) plot axis
y.sort()

alt.Chart(pd.DataFrame({"x": x, "y": y}), title="Exp Axes").transform_window(
    cumulative_sum="sum(y)",
    sort=[{"field": "x"}],
).mark_line().encode(
    x=alt.X("x:Q", scale=alt.Scale(type="pow", exponent=np.e)),
    y=alt.Y("cumulative_sum:Q", scale=alt.Scale(type="pow", exponent=1)),
)

作为参考,以下是原始信号在绘制时的样子:

alt.Chart(pd.DataFrame({"x": x, "y": y}), title="Raw Plot").transform_window(
    cumulative_sum="sum(y)",
    sort=[{"field": "x"}],
).mark_line().encode(
    x=alt.X("x:Q"),
    y=alt.Y("cumulative_sum:Q"),
)

【讨论】:

    猜你喜欢
    • 2018-09-23
    • 1970-01-01
    • 2019-10-23
    • 2011-07-20
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多