Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set Axis FontSize in Altair?

Tags:

python

altair

I would like to increase the X-Axis (or Y-Axis for that matter) fontSize to 16 (or any value) in the following Altair graph. I could not find any example in the Altair documentation here: https://altair-viz.github.io/index.html. I am using Jupyter Lab for visualization. Intuitively alt.Axis should take FontSize argument

import altair as alt
from vega_datasets import data
cars = data.cars()

alt.Chart(cars).mark_point().encode(
    alt.X('Horsepower', axis=alt.Axis(title="HORSEPOWER")),
    alt.Y('Miles_per_Gallon', axis=alt.Axis(title="Miles Per Gallon")),
    color='Origin',
    shape='Origin'
)

like image 220
oekici Avatar asked Nov 20 '18 21:11

oekici


People also ask

How do I change font size in Axis?

To change the text font for any chart element, such as a title or axis, right–click the element, and then click Font. When the Font box appears make the changes you want.

How do I make axis labels larger?

Just click to select the axis you will change all labels' font color and size in the chart, and then type a font size into the Font Size box, click the Font color button and specify a font color from the drop down list in the Font group on the Home tab.

How do I get rid of gridlines on Altair?

We can remove the grid lines on x or y-axis by specifying the argument grid=False inside alt. X() or alt. Y() method in the encoding channels.

Which argument can be used to increase the font size of the axis labels?

Increasing the font size using theme's base_size We can increase the axis label size by specifying the argument base_size=24 inside theme_bw().


1 Answers

One way you can do this is using the top-level chart configuration (think of it as a set of default chart properties). For example:

import altair as alt
from vega_datasets import data
cars = data.cars()

alt.Chart(cars).mark_point().encode(
    alt.X('Horsepower', axis=alt.Axis(title="HORSEPOWER")),
    alt.Y('Miles_per_Gallon', axis=alt.Axis(title="Miles Per Gallon")),
    color='Origin',
    shape='Origin'
).configure_axis(
    labelFontSize=20,
    titleFontSize=20
)

enter image description here

You can read more in Altair's Chart Configuration documentation.

like image 152
jakevdp Avatar answered Oct 01 '22 13:10

jakevdp