animated subplots using matplotlib and info text with current values











up vote
0
down vote

favorite












I am trying to combine examples from here and here. info_text is not working, not updating.
I am getting error:



AttributeError: 'list' object has no attribute 'set_animated'



Anyone knows why?
Here is my code:



import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from pprint import pprint

total_subplots = 2
# initialize the data arrays
#xdata, y1data, y2data = , ,
signals =list()
for i in range(0, total_subplots):
signals.append() # [,]
xdata = list()
ax = list()
lines = list()
info_text = list()

colors=['blue', 'red']


def data_gen():

t_max = 1000.0
t = 0
dt = 0.05

y = [''] * total_subplots

while t < t_max:

t += dt
y[0] = np.sin(2*np.pi*t) * np.exp(-t/10.)
y[1] = np.cos(2*np.pi*t) * np.exp(-t/10.)
# adapted the data generator to yield both sin and cos
yield t, y


def run(data):
# update the data
t, y = data

xdata.append(t)

for i in range(0, total_subplots):
signals[i].append(y[i])

# axis limits checking. Same as before, just for both axes
for i in range(len(ax)):
xmin, xmax = ax[i].get_xlim()
if t >= xmax:
ax[i].set_xlim(xmin, 2*xmax)
ax[i].figure.canvas.draw()

# update the data of both line objects
for i in range(len(lines)):
lines[i].set_data(xdata, signals[i])
text = 'Time = %.1f s nValue = %.1f'%(t, y[i])
info_text[i].set_text(text)

return lines, info_text


# Initialize
# create a figure with two subplots
#fig, (ax1, ax2) = plt.subplots(2,1)
fig = plt.figure()
for i in range(0, total_subplots):
axis = fig.add_subplot(2, 1, (i+1))
ax.append(axis)

# intialize two line objects (one in each axes)
line, = ax[i].plot(, , lw=2, color=colors[i])
lines.append(line)

# the same axes initalizations as before (just now we do it for both of them)
ax[i].set_ylim(-1.1, 1.1)
ax[i].set_xlim(0, 5)
ax[i].grid()


text = ax[i].text(0.05, 0.9, '', transform=ax[i].transAxes)
info_text.append(text)

# Run
ani = animation.FuncAnimation(fig, run,
data_gen,
interval=10,
blit=True,
repeat=False)

plt.show()









share|improve this question


























    up vote
    0
    down vote

    favorite












    I am trying to combine examples from here and here. info_text is not working, not updating.
    I am getting error:



    AttributeError: 'list' object has no attribute 'set_animated'



    Anyone knows why?
    Here is my code:



    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.animation as animation
    from pprint import pprint

    total_subplots = 2
    # initialize the data arrays
    #xdata, y1data, y2data = , ,
    signals =list()
    for i in range(0, total_subplots):
    signals.append() # [,]
    xdata = list()
    ax = list()
    lines = list()
    info_text = list()

    colors=['blue', 'red']


    def data_gen():

    t_max = 1000.0
    t = 0
    dt = 0.05

    y = [''] * total_subplots

    while t < t_max:

    t += dt
    y[0] = np.sin(2*np.pi*t) * np.exp(-t/10.)
    y[1] = np.cos(2*np.pi*t) * np.exp(-t/10.)
    # adapted the data generator to yield both sin and cos
    yield t, y


    def run(data):
    # update the data
    t, y = data

    xdata.append(t)

    for i in range(0, total_subplots):
    signals[i].append(y[i])

    # axis limits checking. Same as before, just for both axes
    for i in range(len(ax)):
    xmin, xmax = ax[i].get_xlim()
    if t >= xmax:
    ax[i].set_xlim(xmin, 2*xmax)
    ax[i].figure.canvas.draw()

    # update the data of both line objects
    for i in range(len(lines)):
    lines[i].set_data(xdata, signals[i])
    text = 'Time = %.1f s nValue = %.1f'%(t, y[i])
    info_text[i].set_text(text)

    return lines, info_text


    # Initialize
    # create a figure with two subplots
    #fig, (ax1, ax2) = plt.subplots(2,1)
    fig = plt.figure()
    for i in range(0, total_subplots):
    axis = fig.add_subplot(2, 1, (i+1))
    ax.append(axis)

    # intialize two line objects (one in each axes)
    line, = ax[i].plot(, , lw=2, color=colors[i])
    lines.append(line)

    # the same axes initalizations as before (just now we do it for both of them)
    ax[i].set_ylim(-1.1, 1.1)
    ax[i].set_xlim(0, 5)
    ax[i].grid()


    text = ax[i].text(0.05, 0.9, '', transform=ax[i].transAxes)
    info_text.append(text)

    # Run
    ani = animation.FuncAnimation(fig, run,
    data_gen,
    interval=10,
    blit=True,
    repeat=False)

    plt.show()









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am trying to combine examples from here and here. info_text is not working, not updating.
      I am getting error:



      AttributeError: 'list' object has no attribute 'set_animated'



      Anyone knows why?
      Here is my code:



      import numpy as np
      import matplotlib.pyplot as plt
      import matplotlib.animation as animation
      from pprint import pprint

      total_subplots = 2
      # initialize the data arrays
      #xdata, y1data, y2data = , ,
      signals =list()
      for i in range(0, total_subplots):
      signals.append() # [,]
      xdata = list()
      ax = list()
      lines = list()
      info_text = list()

      colors=['blue', 'red']


      def data_gen():

      t_max = 1000.0
      t = 0
      dt = 0.05

      y = [''] * total_subplots

      while t < t_max:

      t += dt
      y[0] = np.sin(2*np.pi*t) * np.exp(-t/10.)
      y[1] = np.cos(2*np.pi*t) * np.exp(-t/10.)
      # adapted the data generator to yield both sin and cos
      yield t, y


      def run(data):
      # update the data
      t, y = data

      xdata.append(t)

      for i in range(0, total_subplots):
      signals[i].append(y[i])

      # axis limits checking. Same as before, just for both axes
      for i in range(len(ax)):
      xmin, xmax = ax[i].get_xlim()
      if t >= xmax:
      ax[i].set_xlim(xmin, 2*xmax)
      ax[i].figure.canvas.draw()

      # update the data of both line objects
      for i in range(len(lines)):
      lines[i].set_data(xdata, signals[i])
      text = 'Time = %.1f s nValue = %.1f'%(t, y[i])
      info_text[i].set_text(text)

      return lines, info_text


      # Initialize
      # create a figure with two subplots
      #fig, (ax1, ax2) = plt.subplots(2,1)
      fig = plt.figure()
      for i in range(0, total_subplots):
      axis = fig.add_subplot(2, 1, (i+1))
      ax.append(axis)

      # intialize two line objects (one in each axes)
      line, = ax[i].plot(, , lw=2, color=colors[i])
      lines.append(line)

      # the same axes initalizations as before (just now we do it for both of them)
      ax[i].set_ylim(-1.1, 1.1)
      ax[i].set_xlim(0, 5)
      ax[i].grid()


      text = ax[i].text(0.05, 0.9, '', transform=ax[i].transAxes)
      info_text.append(text)

      # Run
      ani = animation.FuncAnimation(fig, run,
      data_gen,
      interval=10,
      blit=True,
      repeat=False)

      plt.show()









      share|improve this question













      I am trying to combine examples from here and here. info_text is not working, not updating.
      I am getting error:



      AttributeError: 'list' object has no attribute 'set_animated'



      Anyone knows why?
      Here is my code:



      import numpy as np
      import matplotlib.pyplot as plt
      import matplotlib.animation as animation
      from pprint import pprint

      total_subplots = 2
      # initialize the data arrays
      #xdata, y1data, y2data = , ,
      signals =list()
      for i in range(0, total_subplots):
      signals.append() # [,]
      xdata = list()
      ax = list()
      lines = list()
      info_text = list()

      colors=['blue', 'red']


      def data_gen():

      t_max = 1000.0
      t = 0
      dt = 0.05

      y = [''] * total_subplots

      while t < t_max:

      t += dt
      y[0] = np.sin(2*np.pi*t) * np.exp(-t/10.)
      y[1] = np.cos(2*np.pi*t) * np.exp(-t/10.)
      # adapted the data generator to yield both sin and cos
      yield t, y


      def run(data):
      # update the data
      t, y = data

      xdata.append(t)

      for i in range(0, total_subplots):
      signals[i].append(y[i])

      # axis limits checking. Same as before, just for both axes
      for i in range(len(ax)):
      xmin, xmax = ax[i].get_xlim()
      if t >= xmax:
      ax[i].set_xlim(xmin, 2*xmax)
      ax[i].figure.canvas.draw()

      # update the data of both line objects
      for i in range(len(lines)):
      lines[i].set_data(xdata, signals[i])
      text = 'Time = %.1f s nValue = %.1f'%(t, y[i])
      info_text[i].set_text(text)

      return lines, info_text


      # Initialize
      # create a figure with two subplots
      #fig, (ax1, ax2) = plt.subplots(2,1)
      fig = plt.figure()
      for i in range(0, total_subplots):
      axis = fig.add_subplot(2, 1, (i+1))
      ax.append(axis)

      # intialize two line objects (one in each axes)
      line, = ax[i].plot(, , lw=2, color=colors[i])
      lines.append(line)

      # the same axes initalizations as before (just now we do it for both of them)
      ax[i].set_ylim(-1.1, 1.1)
      ax[i].set_xlim(0, 5)
      ax[i].grid()


      text = ax[i].text(0.05, 0.9, '', transform=ax[i].transAxes)
      info_text.append(text)

      # Run
      ani = animation.FuncAnimation(fig, run,
      data_gen,
      interval=10,
      blit=True,
      repeat=False)

      plt.show()






      python matplotlib subplot data-generation livegraph






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 16:11









      ioaniatr

      9411




      9411
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I think you mean to return the iterable of artists for the animation. It should one single iterable though. E.g.



          return lines + info_text





          share|improve this answer





















          • It works!! Thank you!! But, how actually makes it iterable this way?
            – ioaniatr
            Nov 23 at 14:39












          • Lists are iterable in python.
            – ImportanceOfBeingErnest
            Nov 23 at 14:41










          • I saw that it actually merge the 2 lists into one and looks like: [<matplotlib.lines.Line2D object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>, <matplotlib.text.Text object>], but it thought that it should be like: [<matplotlib.lines.Line2D object>, <matplotlib.text.Text object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>]
            – ioaniatr
            Nov 23 at 14:51










          • What's the difference?
            – ImportanceOfBeingErnest
            Nov 23 at 15:44










          • Maybe, it puts the wrong info_text into the wrong graph. Every graph has it own info_text.
            – ioaniatr
            Nov 23 at 15:52











          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%2f53434792%2fanimated-subplots-using-matplotlib-and-info-text-with-current-values%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
          1
          down vote



          accepted










          I think you mean to return the iterable of artists for the animation. It should one single iterable though. E.g.



          return lines + info_text





          share|improve this answer





















          • It works!! Thank you!! But, how actually makes it iterable this way?
            – ioaniatr
            Nov 23 at 14:39












          • Lists are iterable in python.
            – ImportanceOfBeingErnest
            Nov 23 at 14:41










          • I saw that it actually merge the 2 lists into one and looks like: [<matplotlib.lines.Line2D object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>, <matplotlib.text.Text object>], but it thought that it should be like: [<matplotlib.lines.Line2D object>, <matplotlib.text.Text object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>]
            – ioaniatr
            Nov 23 at 14:51










          • What's the difference?
            – ImportanceOfBeingErnest
            Nov 23 at 15:44










          • Maybe, it puts the wrong info_text into the wrong graph. Every graph has it own info_text.
            – ioaniatr
            Nov 23 at 15:52















          up vote
          1
          down vote



          accepted










          I think you mean to return the iterable of artists for the animation. It should one single iterable though. E.g.



          return lines + info_text





          share|improve this answer





















          • It works!! Thank you!! But, how actually makes it iterable this way?
            – ioaniatr
            Nov 23 at 14:39












          • Lists are iterable in python.
            – ImportanceOfBeingErnest
            Nov 23 at 14:41










          • I saw that it actually merge the 2 lists into one and looks like: [<matplotlib.lines.Line2D object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>, <matplotlib.text.Text object>], but it thought that it should be like: [<matplotlib.lines.Line2D object>, <matplotlib.text.Text object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>]
            – ioaniatr
            Nov 23 at 14:51










          • What's the difference?
            – ImportanceOfBeingErnest
            Nov 23 at 15:44










          • Maybe, it puts the wrong info_text into the wrong graph. Every graph has it own info_text.
            – ioaniatr
            Nov 23 at 15:52













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          I think you mean to return the iterable of artists for the animation. It should one single iterable though. E.g.



          return lines + info_text





          share|improve this answer












          I think you mean to return the iterable of artists for the animation. It should one single iterable though. E.g.



          return lines + info_text






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 20:02









          ImportanceOfBeingErnest

          123k10125202




          123k10125202












          • It works!! Thank you!! But, how actually makes it iterable this way?
            – ioaniatr
            Nov 23 at 14:39












          • Lists are iterable in python.
            – ImportanceOfBeingErnest
            Nov 23 at 14:41










          • I saw that it actually merge the 2 lists into one and looks like: [<matplotlib.lines.Line2D object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>, <matplotlib.text.Text object>], but it thought that it should be like: [<matplotlib.lines.Line2D object>, <matplotlib.text.Text object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>]
            – ioaniatr
            Nov 23 at 14:51










          • What's the difference?
            – ImportanceOfBeingErnest
            Nov 23 at 15:44










          • Maybe, it puts the wrong info_text into the wrong graph. Every graph has it own info_text.
            – ioaniatr
            Nov 23 at 15:52


















          • It works!! Thank you!! But, how actually makes it iterable this way?
            – ioaniatr
            Nov 23 at 14:39












          • Lists are iterable in python.
            – ImportanceOfBeingErnest
            Nov 23 at 14:41










          • I saw that it actually merge the 2 lists into one and looks like: [<matplotlib.lines.Line2D object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>, <matplotlib.text.Text object>], but it thought that it should be like: [<matplotlib.lines.Line2D object>, <matplotlib.text.Text object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>]
            – ioaniatr
            Nov 23 at 14:51










          • What's the difference?
            – ImportanceOfBeingErnest
            Nov 23 at 15:44










          • Maybe, it puts the wrong info_text into the wrong graph. Every graph has it own info_text.
            – ioaniatr
            Nov 23 at 15:52
















          It works!! Thank you!! But, how actually makes it iterable this way?
          – ioaniatr
          Nov 23 at 14:39






          It works!! Thank you!! But, how actually makes it iterable this way?
          – ioaniatr
          Nov 23 at 14:39














          Lists are iterable in python.
          – ImportanceOfBeingErnest
          Nov 23 at 14:41




          Lists are iterable in python.
          – ImportanceOfBeingErnest
          Nov 23 at 14:41












          I saw that it actually merge the 2 lists into one and looks like: [<matplotlib.lines.Line2D object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>, <matplotlib.text.Text object>], but it thought that it should be like: [<matplotlib.lines.Line2D object>, <matplotlib.text.Text object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>]
          – ioaniatr
          Nov 23 at 14:51




          I saw that it actually merge the 2 lists into one and looks like: [<matplotlib.lines.Line2D object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>, <matplotlib.text.Text object>], but it thought that it should be like: [<matplotlib.lines.Line2D object>, <matplotlib.text.Text object>, <matplotlib.lines.Line2D object>,<matplotlib.text.Text object>]
          – ioaniatr
          Nov 23 at 14:51












          What's the difference?
          – ImportanceOfBeingErnest
          Nov 23 at 15:44




          What's the difference?
          – ImportanceOfBeingErnest
          Nov 23 at 15:44












          Maybe, it puts the wrong info_text into the wrong graph. Every graph has it own info_text.
          – ioaniatr
          Nov 23 at 15:52




          Maybe, it puts the wrong info_text into the wrong graph. Every graph has it own info_text.
          – ioaniatr
          Nov 23 at 15:52


















          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%2f53434792%2fanimated-subplots-using-matplotlib-and-info-text-with-current-values%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

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          Alexandru Averescu

          Trompette piccolo