# [Matplotlib-users] Problem doing multiple independent plots

Benjamin Root ben.v.root at gmail.com
Wed Sep 27 09:44:58 EDT 2017

```You might want to check out my Anatomy of Matplotlib tutorial:
https://github.com/matplotlib/AnatomyOfMatplotlib

You can use the Jupyter notebooks and follow along with the recordings
2017) and https://www.youtube.com/watch?v=MKucn8NtVeI  (Scipy 2015 - which
covered more, but spent less time on each aspect).

There is also a book, "Mastering Matplotlib", which goes into deep depths
with respect to the API and logic behind the library:
https://www.amazon.com/Mastering-matplotlib-Duncan-M-McGreggor/dp/1783987545

I hope that helps!
Ben Root

On Wed, Sep 27, 2017 at 8:37 AM, William Ray Wing <wrw at mac.com> wrote:

>
> On Sep 26, 2017, at 6:20 PM, Ryan May <rmay31 at gmail.com> wrote:
>
> 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:
>
>
> Thanks, that did it.  Very grateful.
>
> Parenthetically, I wish there were a matplotlib book that explained the
> underlying logic of mil plotting.  Every one I’ve looked into so far is
> full of specific cookbook examples, but they don’t explain what the various
> calls really do, what order they need to be called in (and why), and how
> they affect each other.  Cookbooks are fine for doing things by rote, but
> they don’t provide understanding. If anyone on this list knows of such a
> book, I’d really appreciate hearing.
>
> Thanks,
> Bill
>
> 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
>
>
>
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users at python.org
> https://mail.python.org/mailman/listinfo/matplotlib-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170927/05b1ab4d/attachment.html>
```