简体   繁体   中英

Flip vertical bar plot over X axis Altair

If I have this bar plot:

在此处输入图片说明

How can I flip it like this

在此处输入图片说明

Of course preserving right labels.

You can do this by swapping the x and y encodings and adjusting the axis properties accordingly. For example, if you have this chart:

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)

在此处输入图片说明

You can create a rotated version like this:

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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