
:orphan:
:html_theme.sidebar_secondary.remove:

.. This document is auto-generated by the altair-gallery extension. Do not modify directly.

.. _gallery_violin_plot:


Violin Plot
-----------
This example shows how to make a Violin Plot using Altair's density transform.


.. altair-plot::
    :remove-code:
    


    import altair as alt
    from altair.datasets import data

    alt.Chart(data.cars()).transform_density(
        'Miles_per_Gallon',
        as_=['Miles_per_Gallon', 'density'],
        extent=[5, 50],
        groupby=['Origin']
    ).mark_area(orient='horizontal').encode(
        y='Miles_per_Gallon:Q',
        color='Origin:N',
        x=alt.X(
            'density:Q',
            stack='center',
            impute=None,
            title=None,
            axis=alt.Axis(labels=False, values=[0],grid=False, ticks=True),
        ),
        column=alt.Column(
            'Origin:N',
            header=alt.Header(
                titleOrient='bottom',
                labelOrient='bottom',
                labelPadding=0,
            ),
        )
    ).properties(
        width=100
    ).configure_facet(
        spacing=0
    ).configure_view(
        stroke=None
    )


.. tab-set::

    .. tab-item:: Method syntax
        :sync: method

        .. code:: python


            import altair as alt
            from altair.datasets import data

            alt.Chart(data.cars(), width=100).transform_density(
                'Miles_per_Gallon',
                as_=['Miles_per_Gallon', 'density'],
                extent=[5, 50],
                groupby=['Origin']
            ).mark_area(orient='horizontal').encode(
                alt.X('density:Q')
                    .stack('center')
                    .impute(None)
                    .title(None)
                    .axis(labels=False, values=[0], grid=False, ticks=True),
                alt.Y('Miles_per_Gallon:Q'),
                alt.Color('Origin:N'),
                alt.Column('Origin:N')
                    .spacing(0)
                    .header(titleOrient='bottom', labelOrient='bottom', labelPadding=0)
            ).configure_view(
                stroke=None
            )


    .. tab-item:: Attribute syntax
        :sync: attribute

        .. code:: python


            import altair as alt
            from altair.datasets import data

            alt.Chart(data.cars()).transform_density(
                'Miles_per_Gallon',
                as_=['Miles_per_Gallon', 'density'],
                extent=[5, 50],
                groupby=['Origin']
            ).mark_area(orient='horizontal').encode(
                y='Miles_per_Gallon:Q',
                color='Origin:N',
                x=alt.X(
                    'density:Q',
                    stack='center',
                    impute=None,
                    title=None,
                    axis=alt.Axis(labels=False, values=[0],grid=False, ticks=True),
                ),
                column=alt.Column(
                    'Origin:N',
                    header=alt.Header(
                        titleOrient='bottom',
                        labelOrient='bottom',
                        labelPadding=0,
                    ),
                )
            ).properties(
                width=100
            ).configure_facet(
                spacing=0
            ).configure_view(
                stroke=None
            )
