gpt4 book ai didi

python - 在 X 轴上翻转垂直条形图 Altair

转载 作者:行者123 更新时间:2023-12-05 03:47:32 24 4
gpt4 key购买 nike

如果我有这个条形图:

enter image description here

这样怎么翻转

enter image description here

当然要保留正确的标签。

最佳答案

您可以通过交换 xy 编码并相应地调整轴属性来做到这一点。例如,如果您有此图表:

import altair as alt
import pandas as pd
import numpy as np

np.random.seed(1701)
df = pd.DataFrame({
'data': 6 + np.random.randn(500)
})

alt.Chart(df).mark_bar().encode(
x=alt.X('data', bin=alt.Bin(maxbins=40)),
y='count()'
).properties(width=800, height=150)

enter image description here

您可以像这样创建旋转版本:

alt.Chart(df).mark_bar().encode(
y=alt.Y('data', bin=alt.Bin(maxbins=40), axis=alt.Axis(orient='right')),
x=alt.X('count()', scale=alt.Scale(reverse=True))
).properties(width=150, height=800)

enter image description here

关于python - 在 X 轴上翻转垂直条形图 Altair,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64859420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com