Skip to content
Tutorials Center
My learning

Topics

Lesson 50 of 57 ยท Projects

Project: Infographic Poster

Design an infographic poster with Matplotlib: draw a layout with patches and text, add gradient backgrounds, icon shapes, a donut, a timeline, then export it.

Matplotlib is known for scientific charts, but underneath it is a general-purpose drawing library. Everything you have used, from lines to legends, is an object placed at coordinates on a canvas. If you can place objects freely, you can design a poster: big typography, cards with rounded corners, icons made of simple shapes, a donut chart and a timeline, all in one image.

In this project you will build an infographic for an imaginary community garden. Instead of letting Matplotlib arrange axes for you, you will create one full-page canvas and place every element by hand, the way a designer works in a layout tool. Along the way you will use shapes and patches, text and typography, annotations, colormaps and saving figures. The garden and its numbers are invented for this lesson.

Build a poster in layers

Background A gradient made with imshow.
Typography Big title, quiet subtitle.
Cards and icons Patches combined into shapes.
Timeline Annotations on a line.

Back to front, each layer has its own zorder.

1 / 2

Step 1: a canvas with your own coordinates

A normal figure reserves margins for tick labels and titles. A poster has none of that. So we add a single axes that covers the entire figure (fig.add_axes([0, 0, 1, 1]), meaning left 0, bottom 0, width 1, height 1 as fractions of the figure), turn off its frame with ax.axis("off") and give it simple coordinates. Our final poster is 8 by 12 inches, so we choose a 100 by 150 unit canvas: both axes then have the same number of inches per unit, and set_aspect("equal") keeps circles round.

Now we have a blank page measured in friendly numbers. The next ingredients are a background and a title. For a gradient, make a tall one-column array of numbers from 0 to 1 and display it with imshow. With aspect="auto" and extent equal to the canvas, the image stretches to fill the page, and a custom colormap built with LinearSegmentedColormap.from_list maps the numbers to your two colors. The background gets zorder=0 so everything else sits above it.

Typography does a lot of work here: a very large heavy title, a smaller italic subtitle in a muted color, and plenty of empty space. Text positions use ha and va (horizontal and vertical alignment) so the anchor point is the center of the text.

Below is a small version of the idea, a banner with the title and four icons. The icons are not images: each is a short function that adds a few patches (Rectangle, Circle, Wedge) and lines to the canvas. A bar chart icon is three rectangles; a person is a circle on top of a half disk; a clock is a circle with two lines; a bowl is a half disk with a rim.

Python Gradient banner, title and icons

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 icon functions take a center and a size s and build everything from that, so you can put the same icon anywhere at any size. This is the same idea as a function that returns a reusable chart: keep shapes parameterized and layout becomes arithmetic.

Keep a small palette and use it everywhere: one dark ink color for text, one muted color for secondary text, and three or four accent colors. Define them once as constants at the top. A consistent palette is the cheapest way to make a layout look designed. See Designing Clear Charts for more.

Step 2: a donut and a timeline

Two classic infographic pieces still need to be built from scratch: a donut and a timeline.

A donut is a ring of Wedge patches. Wedge(center, radius, theta1, theta2, width=...) draws the arc from angle theta1 to theta2 in degrees, counter-clockwise from the right. Passing width turns the pie slice into a ring segment. To make shares run clockwise from the top, we start at 90 degrees and subtract share * 3.6 for each slice (one percent is 3.6 degrees). A white edge line separates the slices, and text placed at the center turns the ring into a label. The legend is hand-made too: a colored circle, a name, and a right-aligned percentage on each line, which reads better than a default legend and lets you control the spacing. Pie and donut charts are discussed in Pie, Donut and Stacked Area Charts.

A timeline is a thick horizontal line with a marker at each event and a label connected by a thin line. We alternate labels above and below the line (side = 1 or -1) so neighbors never collide. ax.annotate does both jobs at once: xytext positions the text and arrowprops=dict(arrowstyle="-") draws the connector with no arrowhead.

Python A donut with a custom legend, and a timeline

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 shares printed at the end add up to exactly 100, which is a good habit: when you hand-draw proportions, assert that they sum to the whole. If they did not, the ring would have a gap or an overlap and the numbers would quietly lie.

Step 3: put the whole poster together

Now combine everything on the tall canvas. The structure of the final script follows the visual order of the poster, from top to bottom:

  1. the gradient background and the header (title, subtitle and a short green rule),
  2. a two-by-two grid of stat cards, each made of a soft shadow, a white rounded box, a tinted circle with an icon, a big number and a small label,
  3. the donut with its legend,
  4. the timeline and a footer with the data source.
flowchart TB
    A["Canvas<br/>one axes, 100 x 150"] --> B["Background<br/>imshow gradient, zorder 0"]
    B --> C["Header<br/>text and rule"]
    C --> D["Cards<br/>shadow, box, icon, text"]
    D --> E["Donut and legend"]
    E --> F["Timeline and footer"]
    F --> G["savefig to PNG"]

The poster is built back to front. Later layers draw over earlier ones, and zorder fixes the order inside a layer.

Two details deserve a closer look. The card shadow is a second rounded box shifted by a little, filled with a translucent black ("#00000018", where the last two hex digits are the alpha value). It is drawn with a lower zorder than the white card so it peeks out at the bottom right. The rounded corners come from FancyBboxPatch with boxstyle="round,pad=0,rounding_size=3"; pad=0 makes the box exactly the size you give it.

The last lines export the figure to an in-memory PNG with fig.savefig(buf, format="png", dpi=100) and print the pixel size and file size. At figsize=(8, 12) and dpi=100 the poster is 800 by 1,200 pixels; to print a poster at higher quality, raise the dpi, not the figure size, so the text keeps its proportions. See Figure Size, DPI and Layout for how the two interact.

Python The complete infographic poster

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.

You now have a poster whose every element is data: change a number in cards, a share in crops or an event in events, run it again, and the layout follows. That is the real advantage of drawing with code instead of a design tool: the data and the design stay connected.

On the site, Matplotlib figures appear as images. In your own project you would call fig.savefig("poster.png", dpi=200) for a file, or fig.savefig("poster.pdf") for a vector version that stays sharp at any print size, as covered in Saving Figures.

Making it your own

A few extensions to try once the poster runs:

  • Replace the gradient colors with another two-color pair and watch how the whole mood shifts; make the gradient horizontal by reshaping to (1, 256).
  • Add a fifth card by changing the grid to three rows and moving the lower sections down.
  • Draw a sprout icon from two rotated Ellipse patches and a line.
  • Wrap the poster in a function make_poster(title, cards, crops, events) that returns the figure, so one script can generate a poster for each garden.
  • Set a font family from rcParams to give the type a different personality (only fonts bundled with Matplotlib are guaranteed to exist everywhere; DejaVu Sans is the safe choice).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does the poster use fig.add_axes([0, 0, 1, 1]) and ax.axis("off")?

    The rectangle [0, 0, 1, 1] is the full figure in fractions of its width and height. Hiding the axes decorations leaves a blank canvas measured in your own coordinates.
  2. 2. How is the donut ring drawn?

    Wedge draws an arc between two angles, and the width argument cuts out the inner part so only a ring segment remains. One wedge per share builds the full ring.
  3. 3. What does a smaller zorder do to the card shadow compared with the white card?

    Artists are drawn in increasing zorder. The shadow with the lower value is painted first and the white card covers most of it, leaving only the offset edge visible.
  4. 4. To get a sharper print of the finished poster, what should you raise?

    Figure size in inches fixes the relative size of text and shapes. dpi only sets how many pixels each inch gets, so a higher dpi gives more detail with the same layout.

Summary

  • A full-figure axes with its frame off, plus your own coordinate system, turns Matplotlib into a layout canvas.
  • A gradient is a tall array shown with imshow, a custom colormap and aspect="auto".
  • Icons are small functions that combine Rectangle, Circle and Wedge patches; parameterize them by center and size.
  • Rounded cards use FancyBboxPatch; a translucent offset copy makes a shadow; zorder controls the stacking.
  • A donut is a ring of Wedge patches with width; a timeline is a line, markers and annotate labels that alternate sides.
  • Define a palette once, keep the layout data-driven, and export with savefig, raising dpi for print quality.

Next, test yourself with the Practice Exercises.