3

If I have this bar plot:

enter image description here

How can I flip it like this

enter image description here

Of course preserving right labels.

jakevdp
  • 77,104
  • 11
  • 125
  • 160
Pam_pam
  • 33
  • 3

1 Answers1

3

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)

enter image description here

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)

enter image description here

jakevdp
  • 77,104
  • 11
  • 125
  • 160