Hey scientist! How is it going?

Yes, we weren’t here on the previous week, but… we’re back! ðŸ™‚

Let’s continue the series about 2D plots on Python. How about to put several plots on the same window? Maybe you want to compare some information on different plots. We’ll use the command `subplots()`

for that.

In this example we’ll put a sine and a cosine on the same plot, ranging from `0` to `2`. If you have any doubts on the first commands, please check the previous posts ([1] and [2]), ok? The code follows:

1 2 3 4 5 6 7 8 9 10 11 12 |
from pylab import * t = arange(0.0,2.0,0.01) y1 = sin(2*pi*t) y2 = cos(2*pi*t) fig, ax = subplots(2, sharex=True) ax[0].plot(t, y1, color='green', linestyle='-.', linewidth=3) ax[1].plot(t, y2, color='red', linestyle=':', linewidth=3) show() |

Very well. We just said to `subplots()`

that we want two plots on the same figure, and we want them sharing the same `X` axis (`sharex=True`

). The figure is contained on the variable `fig`

, and the axes were attributed to the variable `ax`

; then, the coefficient `0`

on `ax`

represents the first plot. The coefficient `1`

, by its turn, represents the second one. Python starts to count on zero, not one!

The result follows:

How nice it is, right? If you want your plots in columns instead of rows, just change the previous `subplots()`

by this:

1 |
fig, ax = subplots(1, 2, sharex=True) |

And this is the result:

Awesome! If you need more plots, you can use any number you want. Let’s say `5`:

1 |
fig, ax = subplots(5, sharex=True) |

In this example we would have `5` plots in different rows: `ax[0]`

, `ax[1]`

, `ax[2]`

, `ax[3]`

, `ax[4]`

. Note that when we want only plots in rows, the second number is not necessary.

We also can divide the plots into rows and columns. If you’d like to have `3` rows and `2` colums of plots, the command is:

1 |
fig, ax = subplots(3, 2, sharex=True) |

In this case, we would use the ax variable as a matrix. Then, the plots would be:

**first row, first column:**`ax[0, 0]`

(or`a[0][0]`

)**second row, first column:**`ax[1, 0]`

(or`a[1][0]`

)**third row, first column:**`ax[2, 0]`

(or`a[2][0]`

)**first row, second column:**`ax[0, 1]`

(or`a[0][1]`

)**second row, second column:**`ax[1, 1]`

(or`a[1][0]`

)**third row, second column:**`ax[2, 1]`

(or`a[2][0]`

)

Let’s exemplify. The following code presents sine, cosine,Â tangent, secant and cosecant of `Ï€/8` in different colors and line styles:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from pylab import * t = arange(0.0,2.0,0.01) fig, ax = subplots(3, 2, sharex=True) ax[0, 0].plot(t, sin(pi/8*t), color='green', linestyle='-', linewidth=3) ax[0, 1].plot(t, cos(pi/8*t), color='red', linestyle='--', linewidth=3) ax[1, 0].plot(t, tan(pi/8*t), color='cyan', linestyle='-.', linewidth=3) ax[1, 1].plot(t, 1/cos(pi/8*t), color='magenta', linestyle=':', linewidth=3) ax[2, 0].plot(t, 1/sin(pi/8*t), color='yellow', linestyle='--', linewidth=3) ax[2, 1].plot(t, 1/tan(pi/8*t), color='black', linestyle=':', linewidth=3) show() |

The resulting plots follow:

This is it fellows! Now you can put several plots on the same window. I’ll stop this 2D plot series for now, let’s work with different things a little ðŸ™‚ Try the commands, make your plots and show us how they look!

Gigaregards, see you next time!

Did you like this post? Please comment and share with your friends!

Want to download *Programming Science* codes? Go to our GitHub!

I’m on Twitter: @alexdesiqueira! Follow me if you can!

Make a donation for *Programming Science*!