# Subsequently add data to subplots

Hello,
I am new here I want to create a figure with two subplots where I plot my measurement results. I would like to create a function where I can recall the figure and put additional x and y data in the already existing subplots

It should somehow look like this:

def plot_results(v, c, name):

• fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, sharex=True)*

• ax1.plot(v, c, label=name)*

• ax1.legend()*

• ax1.set_title(“C-V”)*

• ax1.set_ylabel(“C in [F]”)*

• ax2.plot(v, 1 / c, label=name)*

• ax2.legend()*

• ax2.set_title(“1/C”)*

• ax2.set_xlabel(“V_{applied} in [V]”)*

• ax2.set_ylabel(“1/C in [1/F]”)*

Then I would like to use the function plot_results like this

plot_results(v_1, c_1, “1st run”)
plot_results(v_2, c_2, “2nd run”)

But when I run the code, I get two windows with the window titles fig1 and fig2 where the data of the first measurement is in fig1 and the 2nd measurement in fig2, but i would love to have them in the same fig. Is there a way to recall the same fig again and again?

I would be very thankful if someone could help me with this

Pass ax1 and ax2 to the function rather than have it make them.

could you please explain this in a little more detail

Your function, as its first act, creates a figure with 2 subplots. Pull
that out of the function, so that your calling code looks like this:

``````figure, (ax1, ax2) = plt.subplots(nrows=2, ncols=1, sharex=True)*
plot_results(ax1, ax2, v_1, c_1, "1st run")
plot_results(ax1, ax2, v_2, c_2, "2nd run")
``````

and modify `plot_results()` to have an additional 2 leading parameters,
being `ax1` and `ax2`. That way you crerat one figure, and draw on it
twice, instead of “create and draw” twice.

Cheers,
Cameron Simpson cs@cskk.id.au

···

On 19Aug2022 07:00, Anton Hofer via Matplotlib nobody@discourse.matplotlib.org wrote:

could you please explain this in a little more detail

1 Like