# [Matplotlib-users] Problem doing multiple independent plots

Ryan May rmay31 at gmail.com
Tue Sep 26 18:20:08 EDT 2017

```William,

Don't use plt to call all of the methods, but directly use them off of the
Figure (e.g. fig1) and Axes (e.g. ax1) instances you create:

import numpy as np, matplotlib.pyplot as plt

def problem(xdata, ydata, i):
color_dic = {1: "red", 2: "green", 3: "blue", 4: "cyan"}
fig1, ax1 = plt.subplots()
ax1.plot(xdata, ydata, linestyle = '-', color = color_dic[i])
fig1.savefig('Plot for run_num ' + str(i))
return

def problem_alt(xdata, ydata, i):
return

t = np.arange(0.0, 2.0, 0.01)
fig2, ax2 = plt.subplots()

for i in range(0,4):
i = i+1
problem(t, np.sin(i*np.pi*3*t), i)
problem_alt(t, np.sin(i*np.pi*3*t), i)
ax2.set_xlim(xmin = 0.0, xmax = 20.0)
ax2.plot((t+i*3), np.sin(i*np.pi*3*(t+i*3)))

fig2.savefig("Global Plot")

At least, I think that's what you're going for. Note I removed some extra
calls to figure() and subplot() that I don't think were helping you.

Ryan

On Tue, Sep 26, 2017 at 3:04 PM, William Ray Wing <wrw at mac.com> wrote:

> Below is a simplified version of a much more elaborate analysis code, but
> it will illustrate the problem I’m having.  What I want to do is
> repetitively call an analysis function from my main code and plot the
> results of that analysis (a curve fit, although thats immaterial here)
> while in that function.  Back in the main code, I want to plot the results
> of all the curve fits on a single plot.  They share a common x axis, but
> appear at different points along it.  What seems to be happening is that
> the gets set in the function, and doesn’t get set back in the main code.
>
> Note that there are two versions of the “problem” function, problem and
> problem_alt.  If you change the main code (move the #), you get the plot I
> want at the end of the main.
>
> There must be something I can call or set to recover the settings
> associated with figure(2), but I can’t seem to figure it out.  Any help
> would be appreciated.
>
> Thanks,
> Bill Wing
>
> #! /usr/bin/env python
> # -*- coding: utf-8 -*-
> #
> #   A simple skeleton of the program to work out the plotting problem
> #
> import numpy as np, matplotlib.pyplot as plt
> #
> # skeleton subroutines
> #
>
> def problem(xdata, ydata, i):
>     color_dic = {1: "red", 2: "green", 3: "blue", 4: "cyan"}
>     plt.figure(1)
>     fig1, ax1 = plt.subplots()
>     plt.subplot()
>     plt.plot(xdata, ydata, linestyle = '-', color = color_dic[i])
>     plt.savefig('Plot for run_num ' + str(i))
>     return
>
> def problem_alt(xdata, ydata, i):
>     return
>
>
> t = np.arange(0.0, 2.0, 0.01)
> plt.figure(2)
> fig2, ax2 = plt.subplots()
>
> for i in range(0,4):
>     i = i+1
>     problem(t, np.sin(i*np.pi*3*t), i)
>     problem_alt(t, np.sin(i*np.pi*3*t), i)
>     ax2.set_xlim(xmin = 0.0, xmax = 20.0)
>     plt.subplot()
>     plt.plot((t+i*3), np.sin(i*np.pi*3*(t+i*3)))
>
> plt.savefig("Global Plot")
>
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
>

--
Ryan May
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170926/bfe18b3f/attachment.html>
```