Solvedaltair Set axis range and absolute chart width

Hi,

can please someone tell me how to limit the range of y-axis to [50, 100] for example?

Also is there option to set chart absolute dimensions in pixels?

Thanks

13 Answers

✔️Accepted Answer

Hi,
These things are not all that well documented in Altair itself at the moment (we're working on that), but you can find info by searching for how things are done in Vega-Lite.

For example, take a look at this chart:

from altair import *

# load data as a pandas DataFrame
cars = load_dataset('cars')

chart = Chart(cars).mark_point().encode(
    y='Miles_per_Gallon',
    x='Horsepower',
    color='Origin',
)

chart

vega 10

You can adjust the plot size in pixels using the configure_cell command: (Note: this is not true for current versions of Altair, version 2.0 or later. This issue revers to Altair version 1)

chart.configure_cell(height=200, width=200)

vega 11

And you can adjust the axis limits using the domain subfield of any Channel (channels are the things passed to encode):

chart.encode(Y('Miles_per_Gallon', scale=Scale(domain=[-100, 100])))

vega 12

Until we mirror the full vega-lite docs in Altair, you can learn more within the Vega-Lite docs themselves. The translation is pretty strightforward: JSON fields in Vega-Lite become Python attributes in Altair.

Other Answers:

Correct, most everything in this issue refers to version 1 and does not work on version 2. The documentation details how to customize charts in version 2.

In v2, you should use chart.properties(width=200, height=100)