Skip to content
Tutorials Center
My learning

Topics

Lesson 46 of 57 ยท Projects

Project: Weather Station Report

Build a climate report in Matplotlib from a year of synthetic weather data: a dual-axis climograph, monthly boxplots, a day-by-month heatmap and seasonal shading.

Weather data is a perfect playground for charts: it has a clear yearly rhythm, two very different variables (temperature and rainfall), plenty of noise, and a long tradition of standard chart types. In this project you will turn one year of daily readings from an imaginary weather station into a four-part climate report: a climograph that combines monthly rainfall bars with a temperature line, monthly boxplots that show how variable each month is, a calendar heatmap with one cell per day, and seasonal shading to tie them together.

The data is synthetic and uses a fixed seed, so every number below matches what you will see. You will practice dual axes, box plots, heatmaps, date handling and subplot layouts.

The four views

Climograph Rain bars with a temperature line.
Boxplots How much each month varies.
Heatmap One cell for every day.
Seasons Shaded bands as context.

Each view answers a different question about the same year.

1 / 2

Step 1: generate a year of weather

Real station data would arrive as a CSV with one row per day. Here we build the same table from simple rules, so the project runs anywhere. Temperature follows a yearly cosine wave (coldest in January, warmest in July) plus a day-to-day random wobble. Rainfall is more interesting: most days are dry, wet days are more likely in winter and autumn, and the amount on a wet day is skewed, with many small showers and a few heavy ones. The gamma distribution gives that shape.

Use a pandas DataFrame with a daily DatetimeIndex. Everything after this step is a view of that one table.

Python Step 1: daily data and monthly summary

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Before drawing anything, read the summary. It confirms the seasonal shape: low temperatures in the winter months, high in the summer, and rainfall totals that change from month to month. If a table looks wrong, a chart will only hide the problem.

Step 2: the climograph

A climograph is the classic way to summarize a climate. Months run along the x axis; bars show total precipitation and a line shows mean temperature. The two variables have different units, so the chart uses two y axes with twinx. This is the one place where dual axes are standard practice, but follow the safety rules from Shared, Twin and Secondary Axes:

  • Color each axis label and tick set like its data (blue rain, red temperature) so nobody has to guess which scale belongs to which mark.
  • Start the rain axis at zero (bars must), and choose temperature limits that leave the line clear of the bars.
  • Draw the bars on the first axes and the line on the twin, so the line stays on top.

Seasonal shading adds context: ax.axvspan paints a pale band across a range of x values. With a month axis, the four meteorological seasons in the northern hemisphere are winter (December to February), spring (March to May), summer (June to August) and autumn (September to November). Winter spans the end of the axis and the beginning, so it needs two bands.

Python Step 2: climograph with seasonal bands

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The temperature line forms a smooth hill through the summer band, while the bars wander more irregularly, with the most rain in March (149 mm) and low totals in May and June. The printed lines say the same: July is the warmest month at 20.6 ยฐC and March the wettest. Notice ax.get_xaxis_transform(): it lets the season labels use data coordinates in x and axes fractions in y, so they stay at the top whatever the y limits are. That is the same idea as in Transforms and the Artist Layer.

Step 3: monthly boxplots

The climograph only shows averages. A boxplot per month shows the spread of daily temperatures: the median, the middle half of the days (the box), the typical range (the whiskers) and unusual days (the dots). Build a list with one array per month and give it to ax.boxplot. Turn on patch_artist=True to color the boxes, and use showfliers=True (the default) so unusual days are not hidden.

A boxplot is a good chart for this question, but remember what it hides: a box says nothing about the order of days, and two different shapes can have the same box. For a daily series, the heatmap in step 4 fills in what the boxes cannot.

Python Step 3: temperature spread by month

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Coloring each box by its median with a diverging colormap (coolwarm) repeats the message of the vertical position in color, which helps at a glance. The dashed line at 0 ยฐC turns an abstract axis into a meaningful threshold: the boxes in the coldest months straddle it, and the reader sees right away how many winter days are freezing.

Step 4: the day-by-month heatmap

The strongest chart for a daily series is a calendar-style heatmap: 31 rows (the day of the month) by 12 columns (the months), one cell per day, colored by temperature. A glance shows the seasons as a color gradient from left to right, individual hot or cold days as stray cells, and the length of every month as the ragged bottom edge.

Build a 2D array of shape (31, 12) filled with NaN, then place each day's value at [day - 1, month - 1]. Draw it with imshow (with aspect="auto"), or pcolormesh. Matplotlib leaves NaN cells blank (you can choose their color with cmap.set_bad), which handles the months with fewer than 31 days. Add a colorbar with a label, and set the ticks to month names (see Heatmaps and Images).

Python Step 4: heatmap of every day

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The color gradient from blue on the left, through gray-white, to red in the middle columns and back is the whole year in one glance. The printed count of empty cells is 7: four months with 30 days leave one empty cell each, and February, with 28 days, leaves three. Setting vmin and vmax yourself keeps the color scale fixed, which matters if you compare two years.

Step 5: assemble the report

Now combine the views into one figure with a mosaic, in the style of the previous project, Project: Sales Dashboard. The layout puts the climograph on top, wide, with the boxplots and the heatmap below it side by side. To keep the code short, collect your panel code into functions that take an Axes, as in Reproducible and Automated Charts, and call them from a build_report(weather) function that returns the figure.

A few touches make the report look finished:

  • A shared style through rc_context: no top spines, left-aligned bold titles.
  • The same month labels on every panel, and the same blue-to-red meaning of color for temperature everywhere.
  • A title that states the finding (for example an f-string such as f"{wet_days} wet days, warmest in {hottest_month}") instead of just "Report", with the numbers calculated from the data so they never go stale.
  • A small footer with the data source and the seed.

Ideas to extend it

  • Replace the synthetic data with a CSV of real daily readings through pd.read_csv (on your own computer) and check that every panel still works.
  • Add a second, line-based panel with a 7-day rolling mean of temperature, drawn over the raw daily values in light gray.
  • Shade the heatmap's wettest week with a rectangle patch from Shapes, Patches and Custom Drawing.
  • Compare two years: draw the climographs on two panels with shared limits so the bars are comparable.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does the climograph use twinx?

    Millimeters of rain and degrees Celsius cannot share one scale. twinx adds a second y axis on the same x axis; color-coding each axis keeps it readable.
  2. 2. What does ax.get_xaxis_transform() allow for the season labels?

    It is a blended transform: x is in data units, y is a fraction of the axes height, so the labels stay at the top whatever the data range.
  3. 3. How are the days that do not exist (such as 30 February) shown in the heatmap?

    The 31 x 12 grid starts filled with NaN. NaN cells are not colored by the data scale; cmap.set_bad chooses their color.
  4. 4. What is the main reason to set vmin and vmax on the heatmap yourself?

    Without fixed limits the scale follows each dataset's range, so colors from different charts are not comparable.
  5. 5. Which statement about a boxplot of daily temperatures is true?

    A box summarizes the distribution: the line is the median, the box the middle half of the values, the whiskers the usual range and dots the outliers. It hides time order, which is why we add a heatmap.

Summary

  • Generate or load a daily table first, then derive monthly summaries; every chart is a view of the same data.
  • A climograph combines rainfall bars and a temperature line with twinx; color the axes to match their data and start bars at zero.
  • axvspan plus a blended transform gives labelled seasonal bands that ignore the y limits.
  • Monthly boxplots show spread and unusual days; color by median and add a freezing line for meaning.
  • A 31 x 12 grid with NaN gaps drawn by imshow shows every day; fix vmin and vmax for comparable colors.
  • Wrap panels in functions and assemble them in a mosaic returned by one build_report function.

Next, continue with a project built around exploring a dataset: Project: Exploratory Data Report.