I'm creating a line chart using Altair. I have a DataFrame where my y-values move up and down around 0, and I'd like to add a phat line to mark y=0. Sounds easy enough, so I tried this:
# Add a horizontal line at y=0 to clearly distinguish between positive and negative values.
y_zero = alt.Chart().mark_rule().encode(
y=alt.value(0),
color=alt.value('black'),
size=alt.value(10),
)
This indeed draws a horizontal line, but it gets drawn at the top very top of my chart. It seems Altair uses a coordinate system where (0,0) is at the top-left corner. How do I move my line to my data's y=0 position?
Thanks!
Using alt.datum
instead of alt.value
will draw the line at the data value 0, instead at a pixel value of 0. You can read more and see examples in the docs here https://altair-viz.github.io/user_guide/encodings/index.html#datum-and-value