Python Tutorial: Plotting multiple graphs

Publicado em: 01 Março 2020
no canal de: DataCamp
32,338
175

Want to learn more? Take the full course at https://learn.datacamp.com/courses/in... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.

---

We've seen already that the pyplot module from Matplotlib...

...is our workhorse to construct plots quickly.

We'll see how to plot graphs on the same axes, how to create additional tailored axes, and how to create a grid of axes (called subplots) within a single figure.

To start, let's assume we have three NumPy arrays:

t for time
temperature, and
dewpoint.
These come from weather measurements over the course of two weeks in January 2010.

Ths first plot() command plots temperature vs. t in red.

The arguments to plot() can be NumPy arrays, lists, or Pandas Series.

The curve is not actually drawn, just created in memory.

The second plot() command issued in sequence actually overlays a curve of dewpoint vs. t in blue on the same axes.

The next two lines create a label for the horizontal axis and a title.

The last line with the show() command actually displays the figure on screen once the sequence of plot objects from prior instructions have been positioned.

This is the resulting figure with two curves overlaid on the same axes.

Notice the axis tick labels are messy here; you'll see how to clean those up later.

Common axes are convenient for comparing curves directly, but they can get messy.

We'll try to draw the same two curves, this time on multiple axes in the same figure; overlaying curves is not useful when the scales differ widely.

The tool to construct axes explicitly is the axes() command.

We'll explain the numbers shortly; just realize that the axes() command creates coordinate axes within a figure within which subsequent plots are drawn.

Thus, the first line constructs axes on the left side of the figure window and the subsequent three lines create a curve, an axis label, and a title within those axes.

The second axes() command make new axes on the right of the figure and also makes those axes active.

This means the next three commands place objects in the second axes, not the first.

And again, nothing is drawn until the show() command.

The preceding code snippet produces this figure.

The figures are laid out in separate axes (with different vertical scales) side-by-side.

The axes() command requires the lower left corner, the width, and the height of the axes, all expressed in figure units.

By figure units, we mean numbers between 0 & 1 scaled to the dimensions of the figure.

Getting these right requires some trial & error.

Later, you'll learn how to customize axes to draw an inset plot.

The subplot() command creates a grid of axes, freeing us from figuring out axes coordinates manually.

This figure has two rows and one column of axes created by subplot().

This vertical stacking of axes is slightly more pleasing than before.

This is the code that made the preceding figure.

The top subplot is active for the first four lines, while the bottom subplot is active for the rest.

Before showing the figure, issuing tight_layout() pads spaces between the subplots so the ticklabels & titles display without overlap.

The three subplot() arguments are: number of rows, number of columns, and which subplot number to activate.

Subplot numbers increase across rows, then down columns, starting from number one in the top left corner.

Notice subplot grids are indexed from one (unlike other Python arrays).

Again, calling subplot(nrows, ncols, nsubplot) explicitly instructs where subsequently created plot objects go.

Now it's your turn to practice making multiple plots.


Nesta página do site você pode assistir ao vídeo on-line Python Tutorial: Plotting multiple graphs duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário DataCamp 01 Março 2020, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 32,338 vezes e gostou 175 espectadores. Boa visualização!