0

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...

1
  • Do you mean on the same axis? fig refers to a big image on which one can have multiple axes. Each axis might contain multiple series that e.g. share the same x axis. Commented Jul 27, 2021 at 7:43

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.