DPatrick
DPatrick

Reputation: 431

Python: How to plot different plots separately in a function?

Apology if this sounds like a silly question but I really can not wrap my head around this after searching 20 min online:

I have a function like this:

from numpy import *
import math
import matplotlib.pyplot as plt

def plot(t,a,b,c):
    plt.plot(t, a, 'r') 
    plt.plot(t, b, 'b') 
    plt.plot(t, c, 'g') 
    plt.show()

Then:

t = linspace(0, 2*math.pi, 400)
a = sin(t)
b = cos(t)
c = a + b

plot(t,a,b,c)

plot(t,a,b,c) will make all three lines (a,b,c) appear on the same graph. This is not what I want.

My goal is to plot these 3 lines separately, in 3 different graphs. Those 3 graphs should not be crammed next to each other. They should be one after/below another (similar to when you print multiple dataframes in a function: they would be returned one below another. Those 3 graphs do not share axis, legend, or anything.

I looked up online and it looks like "subplot" is the most popular way to do it - but I thought "subplot" is when all graphs are relevant to each other, and you designate how many rows and columns this big empty "canvas" should be, and then you decide where each individual subplot goes on this canvas. In my case, Is there a way to draw 3 separate graphs individually, without using subplots?

Thanks so much for your help!

Upvotes: 3

Views: 6280

Answers (2)

pakpe
pakpe

Reputation: 5479

In your example, your 3 plots do have a relationship to each other -- they share the same x data. Subplots are perfect for displaying your plots, and you can maintain full control on how you want to label them. Here is how:

from matplotlib import pyplot as plt
from numpy import*
import math

t = linspace(0, 2*math.pi, 400)
a = sin(t)
b = cos(t)
c = a + b

fig, (ax1, ax2, ax3) = plt.subplots(3, 1)

ax1.plot(t, a, 'r')
ax1.set(xlabel ='t', ylabel = 'sin(t)')
ax1.set_title('Sin Graph', y=1.0, pad= -20)

ax2.plot(t, b, 'b', )
ax2.set(xlabel ='t', ylabel = 'cos(t)')
ax2.set_title('Cos Graph', y=1.0, pad= -20)

ax3.plot(t, c, 'g')
ax3.set(xlabel ='t', ylabel = 'sin(t) + cos(t)')
ax3.set_title('Sin + Cos Graph', y=1.0, pad= -20)

plt.show()

enter image description here

If you really want to do it as 3 independent plots that refer to the same formulae, you can do it as shown below. Each plot is saved separately. Each plot shows after you close the preceding one.

t = linspace(0, 2*math.pi, 400)
a = sin(t)
b = cos(t)
c = a + b

plt.plot(t, a, 'r')
plt.xlabel('t')
plt.ylabel('sin(t)')
plt.title('Sin Graph')
plt.savefig('Sin_Graph')
plt.show()

plt.plot(t, b, 'b')
plt.xlabel('t')
plt.ylabel('cos(t)')
plt.title('Cos Graph')
plt.savefig('Cos_Graph')
plt.show()

plt.plot(t, c, 'g')
plt.xlabel('t')
plt.ylabel('sin(t) + cos(t)')
plt.title('Sin + Cos Graph')
plt.savefig('Combined_Graph')
plt.show()

Upvotes: 4

Ariel Werle
Ariel Werle

Reputation: 141

Try this:

fig, ax = plt.subplots(3, 1)

ax[0].plot(t, a, 'r') 
ax[1].plot(t, b, 'b') 
ax[2].plot(t, c, 'g') 

plt.show()

Upvotes: 0

Related Questions