Skip to content

Commit

Permalink
docs: Improve dock example for alt.Step (#3644)
Browse files Browse the repository at this point in the history
* docs: adds info with step size/independent scale

* Reword slightly

* Remove commented out lines

* Use the same data as in the rest of this section

* Remove redundant example

* Fix formatting

---------

Co-authored-by: Joel Ostblom <[email protected]>
  • Loading branch information
daylinmorgan and joelostblom authored Oct 19, 2024
1 parent 0245e0f commit 61f1db7
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions doc/user_guide/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -624,32 +624,7 @@ But since ``mark_bar(size=10)`` only controls the width of the bars, it might be
y='value:Q'
)

The width of the chart containing the bar plot can be controlled through setting the ``width``
property of the chart, either to a pixel value for any chart, or to a step value
in the case of discrete scales.

Here is an example of setting the width to a single value for the whole chart:

.. altair-plot::

alt.Chart(data).mark_bar(size=30).encode(
x='name:O',
y='value:Q'
).properties(width=200)

The width of the bars are set using ``mark_bar(size=30)`` and the width of the chart is set using ``properties(width=100)``

Here is an example of setting the step width for a discrete scale:

.. altair-plot::

alt.Chart(data).mark_bar(size=30).encode(
x='name:N',
y='value:Q'
).properties(width=alt.Step(100))

The width of the bars are set using ``mark_bar(size=30)`` and the width that is allocated for each bar bar in the chart is set using ``width=alt.Step(100)``

Therefore, it is often preferred to set the width of the entire chart relative to the number of distinct categories using :class:`Step`, which you can can see an example of a few charts down.

.. _customization-chart-size:

Expand All @@ -662,9 +637,9 @@ For example:

import altair as alt
from vega_datasets import data

cars = data.cars()

alt.Chart(cars).mark_bar().encode(
x='Origin',
y='count()'
Expand All @@ -685,8 +660,26 @@ the subchart rather than to the overall chart:
).properties(
width=100,
height=100
).resolve_scale(
x='independent'
)

To change the chart size relative to the number of distinct categories, you can use the :class:`Step` class to specify the width/height for each category rather than for the entire chart:

.. altair-plot::

alt.Chart(cars).mark_bar().encode(
x='Origin',
y='count()',
column='Cylinders:Q'
).properties(
width=alt.Step(35),
height=100
).resolve_scale(
x='independent'
)


If you want your chart size to respond to the width of the HTML page or container in which
it is rendered, you can set ``width`` or ``height`` to the string ``"container"``:

Expand Down Expand Up @@ -902,4 +895,4 @@ The configured localization settings persist upon saving.
alt.renderers.set_embed_options(format_locale="en-US", time_format_locale="en-US")

.. _Vega Themes: https://github.com/vega/vega-themes/
.. _`D3's localization support`: https://d3-wiki.readthedocs.io/zh-cn/master/Localization/
.. _`D3's localization support`: https://d3-wiki.readthedocs.io/zh-cn/master/Localization/

0 comments on commit 61f1db7

Please sign in to comment.