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?
python matplotlib plot wolfram-mathematica
add a comment |
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?
python matplotlib plot wolfram-mathematica
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
add a comment |
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?
python matplotlib plot wolfram-mathematica
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
python matplotlib plot wolfram-mathematica
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
add a comment |
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
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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