InquilineKea
InquilineKea

Reputation: 891

Matplotlib: plot multiple graphs as subplots on the same figure when different functions in the same class call the plot routine?

So, say I have a function plot(self) within the class Pendulum that calls plot. Then I have another function plot2(self) within the same class Pendulum that also calls plot. How would I set up pyplot such that both functions would set up multiple graphs as separate subplots on the same figure?

Upvotes: 1

Views: 455

Answers (1)

btel
btel

Reputation: 5693

Try this:

import numpy as np
import matplotlib.pyplot as plt

class Pendulum:

    def __init__(self):
        self.fig = plt.figure()

    def plot1(self):
        ax = self.fig.add_subplot(211)
        ax.plot([1,2],[3,4])

    def plot2(self):
        ax = self.fig.add_subplot(212)
        ax.plot([1,2],[4,3])

p = Pendulum()
p.plot1()
p.plot2()
plt.show()

Upvotes: 1

Related Questions