Kindt
Kindt

Reputation: 41

Adding several data sets in matplotlib.figure subplot?

I have a question with matplotlib.figure.Figure. Is there anyway to plot several sets of data in the same subplot?

I want to show both dataA and dataB against time in the same subplot. Then i wish to return the figure fig at the end of the script. So I don't want the plots to show right away but rather that my function is able to return a Figure-object.

At the moment I'm doing the following (simplified):

import matplotlib.pyplot as plt
from matplotlib.figure import Figure
def getfig():
    fig = plt.Figure(figsize=(5, 4), dpi=100)
    fig.add_subplot(111).plot(dataA,time)
    fig.add_subplot(111).plot(dataB,time)
    return fig

I suppose that I'm overwriting the previous subplot at the moment but I don't really know how to add the same datasets in the same subplots...

Upvotes: 0

Views: 875

Answers (1)

Mateen Ulhaq
Mateen Ulhaq

Reputation: 27201

Plot to the same axis. Usually, I do:

import matplotlib.pyplot as plt

def create_fig(t, a, b, **fig_kwargs):
    fig, ax = plt.subplots(**fig_kwargs)
    ax.plot(t, a)
    ax.plot(t, b)
    return fig

fig = create_fig(t, a, b, figsize=(5, 4), dpi=100)

Upvotes: 2

Related Questions