5

I'm working on a chart using Altair, and I'm trying to figure out how to have less lines in the background grid. Is there a term for that background grid?

Here's a chart that looks like mine, that I took from the tutorial:

Let's say that I want to have half as many grid lines on the X axis. How could I do that?

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374

1 Answers1

3

Grid lines are drawn at the location of ticks, so to adjust the grid lines you can adjust the ticks. For example:

import altair as alt
import numpy as np
import pandas as pd

x = np.arange(100)
source = pd.DataFrame({
  'x': x,
  'f(x)': np.sin(x / 5)
})

alt.Chart(source).mark_line().encode(
    x=alt.X('x', axis=alt.Axis(tickCount=4)),
    y='f(x)'
)

enter image description here

You can see other tick-related properties in the documentation for alt.Axis.

jakevdp
  • 77,104
  • 11
  • 125
  • 160
  • Thanks for the fast answer. I want to specify the number of ticks in terms of units of x, not the total number in the range of the X axis. Is that possible? – Ram Rachum Sep 21 '21 at 18:53
  • 1
    You should be able to do this via `tickMinStep`, but unfortunately there is a [bug](https://github.com/vega/vega-lite/issues/7034) in the version of Vega-Lite targeted by Altair that makes the grid lines not respect this. – jakevdp Sep 21 '21 at 19:01
  • I understand, thank you – Ram Rachum Sep 21 '21 at 19:23