store python plot as a variable and reload it to overlay another plot











up vote
0
down vote

favorite












In Mathematica you can store plots in variables and then overlay them at some later time. For example,



plt1 = Plot[Cos[x],{x,0,Pi}];
plt2 = Plot[Sin[x],{x,0,Pi}];
plt3 = Plot[x,{x,0,Pi}];

Show[plt1,plt2]
Show[plt1,plt3]


gives two plots, one overlays cos(x) and sin(x) plots, and the other overlays cos(x) and x plots. Therefore, I do not need to replot cos(x) for the second overlay since it is already saved in plt1.



I am wondering the same thing can happen in python too. I have a 2D function that is time-consuming to plot, and I need to replot it and overlay it with some other data every time. Can I plot it only once and then overlay it in with plots of other data?










share|improve this question






















  • I am not certain this can be done with matplotlib as it creates an image for its plots. Mathematica (a.k.a Wolfram Language) can do this because of its box language which makes it easy for the front end to combine multiple plots by their underlying elements.
    – Edmund
    5 hours ago















up vote
0
down vote

favorite












In Mathematica you can store plots in variables and then overlay them at some later time. For example,



plt1 = Plot[Cos[x],{x,0,Pi}];
plt2 = Plot[Sin[x],{x,0,Pi}];
plt3 = Plot[x,{x,0,Pi}];

Show[plt1,plt2]
Show[plt1,plt3]


gives two plots, one overlays cos(x) and sin(x) plots, and the other overlays cos(x) and x plots. Therefore, I do not need to replot cos(x) for the second overlay since it is already saved in plt1.



I am wondering the same thing can happen in python too. I have a 2D function that is time-consuming to plot, and I need to replot it and overlay it with some other data every time. Can I plot it only once and then overlay it in with plots of other data?










share|improve this question






















  • I am not certain this can be done with matplotlib as it creates an image for its plots. Mathematica (a.k.a Wolfram Language) can do this because of its box language which makes it easy for the front end to combine multiple plots by their underlying elements.
    – Edmund
    5 hours ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











In Mathematica you can store plots in variables and then overlay them at some later time. For example,



plt1 = Plot[Cos[x],{x,0,Pi}];
plt2 = Plot[Sin[x],{x,0,Pi}];
plt3 = Plot[x,{x,0,Pi}];

Show[plt1,plt2]
Show[plt1,plt3]


gives two plots, one overlays cos(x) and sin(x) plots, and the other overlays cos(x) and x plots. Therefore, I do not need to replot cos(x) for the second overlay since it is already saved in plt1.



I am wondering the same thing can happen in python too. I have a 2D function that is time-consuming to plot, and I need to replot it and overlay it with some other data every time. Can I plot it only once and then overlay it in with plots of other data?










share|improve this question













In Mathematica you can store plots in variables and then overlay them at some later time. For example,



plt1 = Plot[Cos[x],{x,0,Pi}];
plt2 = Plot[Sin[x],{x,0,Pi}];
plt3 = Plot[x,{x,0,Pi}];

Show[plt1,plt2]
Show[plt1,plt3]


gives two plots, one overlays cos(x) and sin(x) plots, and the other overlays cos(x) and x plots. Therefore, I do not need to replot cos(x) for the second overlay since it is already saved in plt1.



I am wondering the same thing can happen in python too. I have a 2D function that is time-consuming to plot, and I need to replot it and overlay it with some other data every time. Can I plot it only once and then overlay it in with plots of other data?







python matplotlib plot wolfram-mathematica






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 18:21









Fluid

83




83












  • I am not certain this can be done with matplotlib as it creates an image for its plots. Mathematica (a.k.a Wolfram Language) can do this because of its box language which makes it easy for the front end to combine multiple plots by their underlying elements.
    – Edmund
    5 hours ago


















  • I am not certain this can be done with matplotlib as it creates an image for its plots. Mathematica (a.k.a Wolfram Language) can do this because of its box language which makes it easy for the front end to combine multiple plots by their underlying elements.
    – Edmund
    5 hours ago
















I am not certain this can be done with matplotlib as it creates an image for its plots. Mathematica (a.k.a Wolfram Language) can do this because of its box language which makes it easy for the front end to combine multiple plots by their underlying elements.
– Edmund
5 hours ago




I am not certain this can be done with matplotlib as it creates an image for its plots. Mathematica (a.k.a Wolfram Language) can do this because of its box language which makes it easy for the front end to combine multiple plots by their underlying elements.
– Edmund
5 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I will interprete the question as to ask about matplotlib (since it is tagged by that), but there are of course other python plotting tools, which might behave differently.



In matplotlib an artist (e.g. a line) is necessarily part of exactly one figure. You cannot add the same artist to more than one figure.

So the usual solution is to not wanting to duplicate the artist itself, but rather the procedure to create the artist.



def mycos(x, ax=None, **kwargs):
ax = ax or plt.gca()
ax.plot(x, np.cos(x), **kwargs)

def mysin(x, ax=None, **kwargs):
ax = ax or plt.gca()
ax.plot(x, np.sin(x), **kwargs)

x = np.linspace(0,2*np.pi)

# Create one figure with two subplots, plot one function in each subplot
fig, axes = plt.subplots(2)
mycos(x, ax=axes[0])
mysin(x, ax=axes[1])

# Create another figure with one subplot, plot both functions
fig, ax = plt.subplots(1)
mycos(x, ax=ax)
mysin(x, ax=ax)





share|improve this answer





















  • Thank you for your reply. You are correct about matplotlib. From your code, it is my understanding that both mycos and mysin are called twice in the code, while I am interested in a situation that we call them only once, store each corresponding plot in a variable, and afterward, we only call those variables (similar to the Mathematica code) rather than replotting the functions each time.
    – Fluid
    Nov 11 at 22:06










  • Yes, to see n curves you call n times the function. In order not to slide into an xyproblem here, you may update the question with your actual problem.
    – ImportanceOfBeingErnest
    Nov 12 at 0:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251798%2fstore-python-plot-as-a-variable-and-reload-it-to-overlay-another-plot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













I will interprete the question as to ask about matplotlib (since it is tagged by that), but there are of course other python plotting tools, which might behave differently.



In matplotlib an artist (e.g. a line) is necessarily part of exactly one figure. You cannot add the same artist to more than one figure.

So the usual solution is to not wanting to duplicate the artist itself, but rather the procedure to create the artist.



def mycos(x, ax=None, **kwargs):
ax = ax or plt.gca()
ax.plot(x, np.cos(x), **kwargs)

def mysin(x, ax=None, **kwargs):
ax = ax or plt.gca()
ax.plot(x, np.sin(x), **kwargs)

x = np.linspace(0,2*np.pi)

# Create one figure with two subplots, plot one function in each subplot
fig, axes = plt.subplots(2)
mycos(x, ax=axes[0])
mysin(x, ax=axes[1])

# Create another figure with one subplot, plot both functions
fig, ax = plt.subplots(1)
mycos(x, ax=ax)
mysin(x, ax=ax)





share|improve this answer





















  • Thank you for your reply. You are correct about matplotlib. From your code, it is my understanding that both mycos and mysin are called twice in the code, while I am interested in a situation that we call them only once, store each corresponding plot in a variable, and afterward, we only call those variables (similar to the Mathematica code) rather than replotting the functions each time.
    – Fluid
    Nov 11 at 22:06










  • Yes, to see n curves you call n times the function. In order not to slide into an xyproblem here, you may update the question with your actual problem.
    – ImportanceOfBeingErnest
    Nov 12 at 0:17















up vote
0
down vote













I will interprete the question as to ask about matplotlib (since it is tagged by that), but there are of course other python plotting tools, which might behave differently.



In matplotlib an artist (e.g. a line) is necessarily part of exactly one figure. You cannot add the same artist to more than one figure.

So the usual solution is to not wanting to duplicate the artist itself, but rather the procedure to create the artist.



def mycos(x, ax=None, **kwargs):
ax = ax or plt.gca()
ax.plot(x, np.cos(x), **kwargs)

def mysin(x, ax=None, **kwargs):
ax = ax or plt.gca()
ax.plot(x, np.sin(x), **kwargs)

x = np.linspace(0,2*np.pi)

# Create one figure with two subplots, plot one function in each subplot
fig, axes = plt.subplots(2)
mycos(x, ax=axes[0])
mysin(x, ax=axes[1])

# Create another figure with one subplot, plot both functions
fig, ax = plt.subplots(1)
mycos(x, ax=ax)
mysin(x, ax=ax)





share|improve this answer





















  • Thank you for your reply. You are correct about matplotlib. From your code, it is my understanding that both mycos and mysin are called twice in the code, while I am interested in a situation that we call them only once, store each corresponding plot in a variable, and afterward, we only call those variables (similar to the Mathematica code) rather than replotting the functions each time.
    – Fluid
    Nov 11 at 22:06










  • Yes, to see n curves you call n times the function. In order not to slide into an xyproblem here, you may update the question with your actual problem.
    – ImportanceOfBeingErnest
    Nov 12 at 0:17













up vote
0
down vote










up vote
0
down vote









I will interprete the question as to ask about matplotlib (since it is tagged by that), but there are of course other python plotting tools, which might behave differently.



In matplotlib an artist (e.g. a line) is necessarily part of exactly one figure. You cannot add the same artist to more than one figure.

So the usual solution is to not wanting to duplicate the artist itself, but rather the procedure to create the artist.



def mycos(x, ax=None, **kwargs):
ax = ax or plt.gca()
ax.plot(x, np.cos(x), **kwargs)

def mysin(x, ax=None, **kwargs):
ax = ax or plt.gca()
ax.plot(x, np.sin(x), **kwargs)

x = np.linspace(0,2*np.pi)

# Create one figure with two subplots, plot one function in each subplot
fig, axes = plt.subplots(2)
mycos(x, ax=axes[0])
mysin(x, ax=axes[1])

# Create another figure with one subplot, plot both functions
fig, ax = plt.subplots(1)
mycos(x, ax=ax)
mysin(x, ax=ax)





share|improve this answer












I will interprete the question as to ask about matplotlib (since it is tagged by that), but there are of course other python plotting tools, which might behave differently.



In matplotlib an artist (e.g. a line) is necessarily part of exactly one figure. You cannot add the same artist to more than one figure.

So the usual solution is to not wanting to duplicate the artist itself, but rather the procedure to create the artist.



def mycos(x, ax=None, **kwargs):
ax = ax or plt.gca()
ax.plot(x, np.cos(x), **kwargs)

def mysin(x, ax=None, **kwargs):
ax = ax or plt.gca()
ax.plot(x, np.sin(x), **kwargs)

x = np.linspace(0,2*np.pi)

# Create one figure with two subplots, plot one function in each subplot
fig, axes = plt.subplots(2)
mycos(x, ax=axes[0])
mysin(x, ax=axes[1])

# Create another figure with one subplot, plot both functions
fig, ax = plt.subplots(1)
mycos(x, ax=ax)
mysin(x, ax=ax)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 19:18









ImportanceOfBeingErnest

122k10124198




122k10124198












  • Thank you for your reply. You are correct about matplotlib. From your code, it is my understanding that both mycos and mysin are called twice in the code, while I am interested in a situation that we call them only once, store each corresponding plot in a variable, and afterward, we only call those variables (similar to the Mathematica code) rather than replotting the functions each time.
    – Fluid
    Nov 11 at 22:06










  • Yes, to see n curves you call n times the function. In order not to slide into an xyproblem here, you may update the question with your actual problem.
    – ImportanceOfBeingErnest
    Nov 12 at 0:17


















  • Thank you for your reply. You are correct about matplotlib. From your code, it is my understanding that both mycos and mysin are called twice in the code, while I am interested in a situation that we call them only once, store each corresponding plot in a variable, and afterward, we only call those variables (similar to the Mathematica code) rather than replotting the functions each time.
    – Fluid
    Nov 11 at 22:06










  • Yes, to see n curves you call n times the function. In order not to slide into an xyproblem here, you may update the question with your actual problem.
    – ImportanceOfBeingErnest
    Nov 12 at 0:17
















Thank you for your reply. You are correct about matplotlib. From your code, it is my understanding that both mycos and mysin are called twice in the code, while I am interested in a situation that we call them only once, store each corresponding plot in a variable, and afterward, we only call those variables (similar to the Mathematica code) rather than replotting the functions each time.
– Fluid
Nov 11 at 22:06




Thank you for your reply. You are correct about matplotlib. From your code, it is my understanding that both mycos and mysin are called twice in the code, while I am interested in a situation that we call them only once, store each corresponding plot in a variable, and afterward, we only call those variables (similar to the Mathematica code) rather than replotting the functions each time.
– Fluid
Nov 11 at 22:06












Yes, to see n curves you call n times the function. In order not to slide into an xyproblem here, you may update the question with your actual problem.
– ImportanceOfBeingErnest
Nov 12 at 0:17




Yes, to see n curves you call n times the function. In order not to slide into an xyproblem here, you may update the question with your actual problem.
– ImportanceOfBeingErnest
Nov 12 at 0:17


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251798%2fstore-python-plot-as-a-variable-and-reload-it-to-overlay-another-plot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine