Changing Number of y-axis Ticks in Altair
I've generated this image comparing Bitcoin to REIT yields over the ten-year period from 2010-2020 using a symmetric log y-axis:
The plot was generated from this DataFrame:
btc_yields Year reit_yields
0 3619143193 2010 38791
1 45790476 2011 31261
2 37433124 2012 30274
3 2739779 2013 4051
4 808407 2014 282
5 1711472 2015 650
6 685726 2016 863
7 75958 2017 1324
8 49428 2018 976
9 34330 2019 -285
10 27005 2020 631
Using this code:
alt.Chart(merged_btc_reit_low_inf).transform_fold(
['btc_yields', 'reit_yields'],
).mark_line(strokeWidth = 5).encode(
x=alt.X('Year:N', title = 'Year Investment Made'),
y=alt.Y('value:Q', title = 'Returns in USD',
scale=alt.Scale(type='symlog'),
axis=alt.Axis(tickCount=merged_btc_reit_low_inf.shape[0])),
color='key:N',
tooltip = 'value:Q'
).properties(
width = 600,
height = 400,
title = 'Return on $10,000 Seed Investment in Bitcoin'
).configure_axis(
labelFontSize=14,
titleFontSize=16,
labelAngle = 0
).configure_title(
fontSize = 21
).configure_legend(
labelFontSize = 14
)
I am not sure why, even with axis=alt.Axis(tickCount=merged_btc_reit_low_inf.shape[0])
there are only 3 y-axis ticks. I would like a hand increasing the number of y-axis ticks to enough that the plot is more informative, especially considering the log-transformed axis.
You could set the axis values
explicitly instead of trying to adjust the number of ticks:
from itertools import product
...
axis=alt.Axis(values=[0] + [10**x * y for (x, y) in product(range(2, 9, 2), (1, -1))]))
# There might be a nicer way of composing this list...
I think the reason tickCount
is not working for you is this section from the docs:
The resulting number may be different so that values are “nice” (multiples of 2, 5, 10) and lie within the underlying scale’s range.
I am not sure why they are not symmetrical, this parameter might not work well with symlog
so you might want to open a feature request for that at the VegaLite repo.