I would like to add padding between the far left-most bar (A) from x-axis and y-axis of Bar plot using Altair Python. I am using Python V.3.8 and altair V.4.2.0.
I am new to Altair so I am experiment with the examples provided in the altair documentation so here is a an example snippet code from their docs (https://altair-viz.github.io/gallery/simple_bar_chart.html):
import altair as alt
import pandas as pd
source = pd.DataFrame({
'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})
alt.Chart(source).mark_bar().encode(
x='a',
y='b'
)
I want to add padding or space between element A from x-axis and the y-axis. Also I would like introduce padding from the bottom of x-axis which will lift all the bars a bit to the top.
I tried the following but nothing change:
import altair as alt
import pandas as pd
source = pd.DataFrame({
'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})
alt.Chart(source).mark_bar().encode(
x=alt.X("a", axis=alt.Axis(tickSize=7, titlePadding=10, tickColor="black", labelPadding=6, domainWidth=1.7,domainCap="round", domainColor='black')
y=alt.Y("b", axis=alt.Axis(tickSize=7, titlePadding=10, tickColor="black", labelPadding=6, domainWidth=1.7,domainCap="round", domainColor='black')
)
How do I do introduce padding in both axises as described above ?
Thank you so much in advance.
Simply shifting the axis can be done by adding an offset value to the intended one.
import altair as alt
import pandas as pd
import numpy as np
source = pd.DataFrame({
'a': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'],
'b': [28, 55, 43, 91, 81, 53, 19, 87, 52]
})
alt.Chart(source).mark_bar().encode(
x=alt.X('a', axis=alt.Axis(offset=10)),
y=alt.Y('b', axis=alt.Axis(offset=10))
)
To lift the graph
The width of the bar and the range of the y-axis are adjusted.
alt.Chart(source).mark_bar(size=10).encode(
x=alt.X('a', scale=alt.Scale(domainMin=-5, padding=3)),
y=alt.Y('b', scale=alt.Scale(domainMin=-5, padding=3))
)