# Problem doing multiple independent plots Classic List Threaded 8 messages Open this post in threaded view
|

## Problem doing multiple independent plots

 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 [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users
Open this post in threaded view
|

## Re: Problem doing multiple independent plots

 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 pltdef 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))    returndef problem_alt(xdata, ydata, i):    returnt = 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.RyanOn Tue, Sep 26, 2017 at 3:04 PM, William Ray Wing 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 [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users -- Ryan May _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users
Open this post in threaded view
|

## Re: Problem doing multiple independent plots

 On Sep 26, 2017, at 6:20 PM, Ryan May <[hidden email]> 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,Billimport numpy as np, matplotlib.pyplot as pltdef 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))    returndef problem_alt(xdata, ydata, i):    returnt = 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.RyanOn Tue, Sep 26, 2017 at 3:04 PM, William Ray Wing 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 [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users -- Ryan May _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users
Open this post in threaded view
|

## Re: Problem doing multiple independent plots

 You might want to check out my Anatomy of Matplotlib tutorial: https://github.com/matplotlib/AnatomyOfMatplotlibYou can use the Jupyter notebooks and follow along with the recordings available on YouTube: https://www.youtube.com/watch?v=rARMKS8jE9g  (Scipy 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/1783987545I hope that helps!Ben RootOn Wed, Sep 27, 2017 at 8:37 AM, William Ray Wing wrote:On Sep 26, 2017, at 6:20 PM, Ryan May <[hidden email]> 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,Billimport numpy as np, matplotlib.pyplot as pltdef 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))    returndef problem_alt(xdata, ydata, i):    returnt = 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.RyanOn Tue, Sep 26, 2017 at 3:04 PM, William Ray Wing 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 [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users -- Ryan May _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users
Open this post in threaded view
|

## Re: Problem doing multiple independent plots

 On Sep 27, 2017, at 9:44 AM, Benjamin Root <[hidden email]> wrote:You might want to check out my Anatomy of Matplotlib tutorial: https://github.com/matplotlib/AnatomyOfMatplotlibYou can use the Jupyter notebooks and follow along with the recordings available on YouTube: https://www.youtube.com/watch?v=rARMKS8jE9g  (Scipy 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/1783987545Thanks, that book may be just what I’ve been searching for.  I’ll check it out.BillI hope that helps!Ben RootOn Wed, Sep 27, 2017 at 8:37 AM, William Ray Wing wrote:On Sep 26, 2017, at 6:20 PM, Ryan May <[hidden email]> 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,Billimport numpy as np, matplotlib.pyplot as pltdef 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))    returndef problem_alt(xdata, ydata, i):    returnt = 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.RyanOn Tue, Sep 26, 2017 at 3:04 PM, William Ray Wing 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 [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users -- Ryan May _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users
Open this post in threaded view
|

## Re: Problem doing multiple independent plots

 In reply to this post by William Ray Wing Hi William, Did you see the tutorials and the external ressources pages on the Matplotlib website? http://matplotlib.org/devdocs/tutorials/index.htmlhttp://matplotlib.org/devdocs/resources/index.htmlI am giving you the "testing" (devdocs) version because the documentation went through a lot of improvements (IMO ;)) for our incoming 2.1 version. But you should be able to find similar pages even on the non-devdocs web site if you prefer to use this one. Besides, with an interactive shell like IPython, you can easily get the documentation associated with a peculiar command by postpending a question mark to it (`command_i_want_to_better_know?`) and if you are using an IDE like Spyder, this kind of software usually provides a way to display the same information (click on the command and then Ctrl-I if I remember correctly in the case of Spyder). Hopefully this will help you. Regards, Adrien On September 27, 2017 5:37:06 AM PDT, William Ray Wing <[hidden email]> wrote: > >> On Sep 26, 2017, at 6:20 PM, Ryan May <[hidden email]> 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 <[hidden email] >> 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 >> [hidden email] >> https://mail.python.org/mailman/listinfo/matplotlib-users> >> >> >> >> -- >> Ryan May >> -- Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma brièveté. _______________________________________________ Matplotlib-users mailing list [hidden email] https://mail.python.org/mailman/listinfo/matplotlib-users