# [Matplotlib-users] Problem doing multiple independent plots

William Ray Wing wrw at mac.com
Tue Sep 26 17:04:36 EDT 2017

```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")

```