Data series animation with subplots using GridSpec vs pos












1














I have a number of data series which I want to save as a video. The different data series should appear one by one, keeping the former series. In the end, all the series is shown in the plot. I want to do this with multiple data series/subplots in a synchronized way. I want the subplots to have different sizes, so - as far as I have understood - I need to do this with GridSpec. However, with GridSpec I am not able to keep the old series while adding new ones (I only get the last data series).



An example of my code is (showing the principle - my actual script is more complex):
Working with index/pos (but only equal size on subplots):



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim

figure = plt.figure()

data = np.array([1,2,3,4,5])
set = np.array([1,2,3,4])

def make_frame(i):
ax1 = plt.subplot(121)
x = data
y = i*x
ax1.set_ylim(0,25)
ax1.plot(x,y)

ax2 = plt.subplot((122), sharey = ax1)
a = data*2
b = i/x*3
ax2.plot(a,b)

plt.pause(1)

ani = anim.FuncAnimation(figure, make_frame, frames = set, repeat = False)

plt.show()


Not keeping old series (but with ability of adjust number of columns/rows for each plot):



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim
from matplotlib.gridspec import GridSpec


figure = plt.figure()

data = np.array([1,2,3,4,5])
set = np.array([1,2,3,4])

print(data,set)

def make_frame(i):
gs1 = GridSpec(1,2)

ax1 = plt.subplot(gs1[0,0])
x = data
y = i*x
ax1.plot(x,y)

ax2 = plt.subplot(gs1[0,1], sharey = ax1)
a = data
b = i/x*3
ax2.plot(a,b)

ax1.set_ylim(0,25)

plt.pause(0.5)

ani = anim.FuncAnimation(figure, make_frame, frames = set, repeat = False)

plt.show()


How can I keep the "plotting history" in the GridSpec solution?










share|improve this question






















  • Thanks! That was exactly what I needed!
    – brobole
    Nov 12 at 15:40
















1














I have a number of data series which I want to save as a video. The different data series should appear one by one, keeping the former series. In the end, all the series is shown in the plot. I want to do this with multiple data series/subplots in a synchronized way. I want the subplots to have different sizes, so - as far as I have understood - I need to do this with GridSpec. However, with GridSpec I am not able to keep the old series while adding new ones (I only get the last data series).



An example of my code is (showing the principle - my actual script is more complex):
Working with index/pos (but only equal size on subplots):



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim

figure = plt.figure()

data = np.array([1,2,3,4,5])
set = np.array([1,2,3,4])

def make_frame(i):
ax1 = plt.subplot(121)
x = data
y = i*x
ax1.set_ylim(0,25)
ax1.plot(x,y)

ax2 = plt.subplot((122), sharey = ax1)
a = data*2
b = i/x*3
ax2.plot(a,b)

plt.pause(1)

ani = anim.FuncAnimation(figure, make_frame, frames = set, repeat = False)

plt.show()


Not keeping old series (but with ability of adjust number of columns/rows for each plot):



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim
from matplotlib.gridspec import GridSpec


figure = plt.figure()

data = np.array([1,2,3,4,5])
set = np.array([1,2,3,4])

print(data,set)

def make_frame(i):
gs1 = GridSpec(1,2)

ax1 = plt.subplot(gs1[0,0])
x = data
y = i*x
ax1.plot(x,y)

ax2 = plt.subplot(gs1[0,1], sharey = ax1)
a = data
b = i/x*3
ax2.plot(a,b)

ax1.set_ylim(0,25)

plt.pause(0.5)

ani = anim.FuncAnimation(figure, make_frame, frames = set, repeat = False)

plt.show()


How can I keep the "plotting history" in the GridSpec solution?










share|improve this question






















  • Thanks! That was exactly what I needed!
    – brobole
    Nov 12 at 15:40














1












1








1







I have a number of data series which I want to save as a video. The different data series should appear one by one, keeping the former series. In the end, all the series is shown in the plot. I want to do this with multiple data series/subplots in a synchronized way. I want the subplots to have different sizes, so - as far as I have understood - I need to do this with GridSpec. However, with GridSpec I am not able to keep the old series while adding new ones (I only get the last data series).



An example of my code is (showing the principle - my actual script is more complex):
Working with index/pos (but only equal size on subplots):



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim

figure = plt.figure()

data = np.array([1,2,3,4,5])
set = np.array([1,2,3,4])

def make_frame(i):
ax1 = plt.subplot(121)
x = data
y = i*x
ax1.set_ylim(0,25)
ax1.plot(x,y)

ax2 = plt.subplot((122), sharey = ax1)
a = data*2
b = i/x*3
ax2.plot(a,b)

plt.pause(1)

ani = anim.FuncAnimation(figure, make_frame, frames = set, repeat = False)

plt.show()


Not keeping old series (but with ability of adjust number of columns/rows for each plot):



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim
from matplotlib.gridspec import GridSpec


figure = plt.figure()

data = np.array([1,2,3,4,5])
set = np.array([1,2,3,4])

print(data,set)

def make_frame(i):
gs1 = GridSpec(1,2)

ax1 = plt.subplot(gs1[0,0])
x = data
y = i*x
ax1.plot(x,y)

ax2 = plt.subplot(gs1[0,1], sharey = ax1)
a = data
b = i/x*3
ax2.plot(a,b)

ax1.set_ylim(0,25)

plt.pause(0.5)

ani = anim.FuncAnimation(figure, make_frame, frames = set, repeat = False)

plt.show()


How can I keep the "plotting history" in the GridSpec solution?










share|improve this question













I have a number of data series which I want to save as a video. The different data series should appear one by one, keeping the former series. In the end, all the series is shown in the plot. I want to do this with multiple data series/subplots in a synchronized way. I want the subplots to have different sizes, so - as far as I have understood - I need to do this with GridSpec. However, with GridSpec I am not able to keep the old series while adding new ones (I only get the last data series).



An example of my code is (showing the principle - my actual script is more complex):
Working with index/pos (but only equal size on subplots):



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim

figure = plt.figure()

data = np.array([1,2,3,4,5])
set = np.array([1,2,3,4])

def make_frame(i):
ax1 = plt.subplot(121)
x = data
y = i*x
ax1.set_ylim(0,25)
ax1.plot(x,y)

ax2 = plt.subplot((122), sharey = ax1)
a = data*2
b = i/x*3
ax2.plot(a,b)

plt.pause(1)

ani = anim.FuncAnimation(figure, make_frame, frames = set, repeat = False)

plt.show()


Not keeping old series (but with ability of adjust number of columns/rows for each plot):



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim
from matplotlib.gridspec import GridSpec


figure = plt.figure()

data = np.array([1,2,3,4,5])
set = np.array([1,2,3,4])

print(data,set)

def make_frame(i):
gs1 = GridSpec(1,2)

ax1 = plt.subplot(gs1[0,0])
x = data
y = i*x
ax1.plot(x,y)

ax2 = plt.subplot(gs1[0,1], sharey = ax1)
a = data
b = i/x*3
ax2.plot(a,b)

ax1.set_ylim(0,25)

plt.pause(0.5)

ani = anim.FuncAnimation(figure, make_frame, frames = set, repeat = False)

plt.show()


How can I keep the "plotting history" in the GridSpec solution?







python-3.x animation matplotlib subplot






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 14:20









brobole

91




91












  • Thanks! That was exactly what I needed!
    – brobole
    Nov 12 at 15:40


















  • Thanks! That was exactly what I needed!
    – brobole
    Nov 12 at 15:40
















Thanks! That was exactly what I needed!
– brobole
Nov 12 at 15:40




Thanks! That was exactly what I needed!
– brobole
Nov 12 at 15:40












1 Answer
1






active

oldest

votes


















0














You need to define the gridspec and the subplots outside of the animating function. Else they are recreated from scratch for each frame.



Also, do not use plt.pause in an animation, but instead use the interval argument. And don't use the names of python functions (set) as variable names.



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim
from matplotlib.gridspec import GridSpec

data = np.array([1,2,3,4,5])
seti = np.array([1,2,3,4])


gs1 = GridSpec(1,2)
figure = plt.figure()
ax1 = plt.subplot(gs1[0,0])
ax2 = plt.subplot(gs1[0,1], sharey = ax1)
ax1.set_ylim(0,25)

print(data,seti)

def make_frame(i):
x = data
y = i*x
ax1.plot(x,y)

a = data
b = 3.*i/x
ax2.plot(a,b)

ani = anim.FuncAnimation(figure, make_frame, frames = seti,
interval =500, repeat = False)

plt.show()





share|improve this answer





















  • Thanks! That was exactly what I needed!
    – brobole
    Nov 12 at 15:43











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',
autoActivateHeartbeat: false,
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%2f53264125%2fdata-series-animation-with-subplots-using-gridspec-vs-pos%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









0














You need to define the gridspec and the subplots outside of the animating function. Else they are recreated from scratch for each frame.



Also, do not use plt.pause in an animation, but instead use the interval argument. And don't use the names of python functions (set) as variable names.



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim
from matplotlib.gridspec import GridSpec

data = np.array([1,2,3,4,5])
seti = np.array([1,2,3,4])


gs1 = GridSpec(1,2)
figure = plt.figure()
ax1 = plt.subplot(gs1[0,0])
ax2 = plt.subplot(gs1[0,1], sharey = ax1)
ax1.set_ylim(0,25)

print(data,seti)

def make_frame(i):
x = data
y = i*x
ax1.plot(x,y)

a = data
b = 3.*i/x
ax2.plot(a,b)

ani = anim.FuncAnimation(figure, make_frame, frames = seti,
interval =500, repeat = False)

plt.show()





share|improve this answer





















  • Thanks! That was exactly what I needed!
    – brobole
    Nov 12 at 15:43
















0














You need to define the gridspec and the subplots outside of the animating function. Else they are recreated from scratch for each frame.



Also, do not use plt.pause in an animation, but instead use the interval argument. And don't use the names of python functions (set) as variable names.



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim
from matplotlib.gridspec import GridSpec

data = np.array([1,2,3,4,5])
seti = np.array([1,2,3,4])


gs1 = GridSpec(1,2)
figure = plt.figure()
ax1 = plt.subplot(gs1[0,0])
ax2 = plt.subplot(gs1[0,1], sharey = ax1)
ax1.set_ylim(0,25)

print(data,seti)

def make_frame(i):
x = data
y = i*x
ax1.plot(x,y)

a = data
b = 3.*i/x
ax2.plot(a,b)

ani = anim.FuncAnimation(figure, make_frame, frames = seti,
interval =500, repeat = False)

plt.show()





share|improve this answer





















  • Thanks! That was exactly what I needed!
    – brobole
    Nov 12 at 15:43














0












0








0






You need to define the gridspec and the subplots outside of the animating function. Else they are recreated from scratch for each frame.



Also, do not use plt.pause in an animation, but instead use the interval argument. And don't use the names of python functions (set) as variable names.



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim
from matplotlib.gridspec import GridSpec

data = np.array([1,2,3,4,5])
seti = np.array([1,2,3,4])


gs1 = GridSpec(1,2)
figure = plt.figure()
ax1 = plt.subplot(gs1[0,0])
ax2 = plt.subplot(gs1[0,1], sharey = ax1)
ax1.set_ylim(0,25)

print(data,seti)

def make_frame(i):
x = data
y = i*x
ax1.plot(x,y)

a = data
b = 3.*i/x
ax2.plot(a,b)

ani = anim.FuncAnimation(figure, make_frame, frames = seti,
interval =500, repeat = False)

plt.show()





share|improve this answer












You need to define the gridspec and the subplots outside of the animating function. Else they are recreated from scratch for each frame.



Also, do not use plt.pause in an animation, but instead use the interval argument. And don't use the names of python functions (set) as variable names.



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as anim
from matplotlib.gridspec import GridSpec

data = np.array([1,2,3,4,5])
seti = np.array([1,2,3,4])


gs1 = GridSpec(1,2)
figure = plt.figure()
ax1 = plt.subplot(gs1[0,0])
ax2 = plt.subplot(gs1[0,1], sharey = ax1)
ax1.set_ylim(0,25)

print(data,seti)

def make_frame(i):
x = data
y = i*x
ax1.plot(x,y)

a = data
b = 3.*i/x
ax2.plot(a,b)

ani = anim.FuncAnimation(figure, make_frame, frames = seti,
interval =500, repeat = False)

plt.show()






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 15:17









ImportanceOfBeingErnest

124k10127203




124k10127203












  • Thanks! That was exactly what I needed!
    – brobole
    Nov 12 at 15:43


















  • Thanks! That was exactly what I needed!
    – brobole
    Nov 12 at 15:43
















Thanks! That was exactly what I needed!
– brobole
Nov 12 at 15:43




Thanks! That was exactly what I needed!
– brobole
Nov 12 at 15:43


















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%2f53264125%2fdata-series-animation-with-subplots-using-gridspec-vs-pos%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