<commit_before>.. _exploring-weather:

Exploring Seattle Weather
=========================

(This tutorial is adapted from `Vega-Lite's documentation
<http://vega.github.io/vega-lite/tutorials/explore.html>`_)

In this tutorial, you’ll learn a few more techniques for creating
visualizations in Altair. If you are not familiar with Altair,
please read :ref:`starting` first.

For this tutorial, we will create visualizations to explore
weather data for Seattle, taken from NOAA.
The dataset is a CSV file with columns for the temperature
(in Celsius), precipitation (in millimeters),
wind speed (in meter/second), and weather type.
We have one row for each day from January 1st, 2012 to December 31st, 2015.

Altair is designed to work with data in the form of Pandas_
dataframes, and contains a loader for this and other built-in datasets:

.. altair-plot::
    :output: repr

    from vega_datasets import data

    df = data.seattle_weather()
    df.head()

The data is loaded from the web and stored in a Pandas DataFrame, and from
here we can explore it with Altair.

Let’s start by looking at the precipitation, using tick marks to see the
distribution of precipitation values:

.. altair-plot::

    import altair as alt

    alt.Chart(df).mark_tick().encode(
        x='precipitation',
    )

It looks as though precipitation is skewed towards lower values;
that is, when it rains in Seattle, it usually doesn’t rain very much.
It is difficult to see patterns across continuous variables, and so to
better see this, we can create a histogram of the precipitation data.
For this we first discretize the precipitation values by adding a binning
to ``x``.
Additionally, we set our encoding channel ``y`` with the special field ``*``
that is aggregated with ``count``.
The result is a histogram of precipitation values:

.. altair-plot::

    alt.Chart(df).mark_bar().encode(
        alt.X('precipitation', bin=True),
        y='count()'
    )

Next, let’s look at how precipitation in Seattle changes throughout the year.
Altair natively supports dates and discretization of dates when we set the
type to ``temporal`` (shorthand ``T``).
For example, in the following plot, we compute the total precipitation for each month.
To discretize the data into months, we can use a ``month`` binning (see
:ref:`user-guide-timeunit-transform` for more information about this and
other ``timeUnit`` binnings):

.. altair-plot::

    alt.Chart(df).mark_line().encode(
        x='month(date:T)',
        y='average(precipitation)'
    )

This chart shows that in Seattle the precipitation in the winter is, on average,
much higher than summer (an unsurprising observation to those who live there!).
By changing the mapping of encoding channels to data features, you can begin
to explore the relationships within the data.

When looking at precipitation and temperature, we might want to aggregate by
year *and* month (``yearmonth``) rather than just month.
This allows us to see seasonal trends, with daily variation smoothed out.
We might also wish to see the maximum and minimum temperature in each month:
<commit_msg>fixing chart not displaying and deleting field *<commit_after>.. _exploring-weather:

Exploring Seattle Weather
=========================

(This tutorial is adapted from `Vega-Lite's documentation
<http://vega.github.io/vega-lite/tutorials/explore.html>`_)

In this tutorial, you’ll learn a few more techniques for creating
visualizations in Altair. If you are not familiar with Altair,
please read :ref:`starting` first.

For this tutorial, we will create visualizations to explore
weather data for Seattle, taken from NOAA.
The dataset is a CSV file with columns for the temperature
(in Celsius), precipitation (in millimeters),
wind speed (in meter/second), and weather type.
We have one row for each day from January 1st, 2012 to December 31st, 2015.

Altair is designed to work with data in the form of Pandas_
dataframes, and contains a loader for this and other built-in datasets:

.. altair-plot::
    :output: repr

    from vega_datasets import data

    df = data.seattle_weather()
    df.head()

The data is loaded from the web and stored in a Pandas DataFrame, and from
here we can explore it with Altair.

Let’s start by looking at the precipitation, using tick marks to see the
distribution of precipitation values:

.. altair-plot::

    import altair as alt

    alt.Chart(df).mark_tick().encode(
        x='precipitation',
    )

It looks as though precipitation is skewed towards lower values;
that is, when it rains in Seattle, it usually doesn’t rain very much.
It is difficult to see patterns across continuous variables, and so to
better see this, we can create a histogram of the precipitation data.
For this we first discretize the precipitation values by adding a binning
to ``x``.
Additionally, we set our encoding channel ``y`` with ``count``.
The result is a histogram of precipitation values:

.. altair-plot::

    alt.Chart(df).mark_bar().encode(
        alt.X('precipitation', bin=True),
        y='count()'
    )

Next, let’s look at how precipitation in Seattle changes throughout the year.
Altair natively supports dates and discretization of dates when we set the
type to ``temporal`` (shorthand ``T``).
For example, in the following plot, we compute the total precipitation for each month.
To discretize the data into months, we can use a ``month`` binning (see
:ref:`user-guide-timeunit-transform` for more information about this and
other ``timeUnit`` binnings):

.. altair-plot::

    alt.Chart(df).mark_line().encode(
        x='month(date):T',
        y='average(precipitation)'
    )

This chart shows that in Seattle the precipitation in the winter is, on average,
much higher than summer (an unsurprising observation to those who live there!).
By changing the mapping of encoding channels to data features, you can begin
to explore the relationships within the data.

When looking at precipitation and temperature, we might want to aggregate by
year *and* month (``yearmonth``) rather than just month.
This allows us to see seasonal trends, with daily variation smoothed out.
We might also wish to see the maximum and minimum temperature in each month:

{'fraction_non_alphanumeric': 0.08146836258251489, 'fraction_numerical': 0.003542102720978908, 'mean_word_length': 4.202680067001675, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5092966', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1629, 'n_words': 846}