0

I am trying to create two animated subplots which are shown simultaneously. One of them is supposed to be showing trajectories of particles from physics simulation and the other histogram of their velocity distribution.

I have succeeded in subplotting my particle animation with a randomly generated plot. They both are working as expected. Now, I am trying to change randomly generated plot with histogram but I do not succeed in that. I present code that is working for me.

fig, axes = plt.subplots(1,2)

lines=[]

l1, = axes[0].plot(box.state[:,0], box.state[:,1], 'bo', ms=6) 
lines.append(l1)

l2, = axes[1].plot(data[2,0],Z)
lines.append(l2)

def run(it):
    global box, dt
    box.step(dt)

    l1.set_data(box.state[:, 0], box.state[:, 1])
    l2.set_data(data[1, it],Z)
    return lines    

ani=animation.FuncAnimation(fig, run, frames=6000, 
                        interval=10, blit=True)

plt.show()

If I now replace l2, = axes[1].plot(data[2,0],Z) with l2, = axes[1].hist(data[2,0]) then I get ValueError: too many values to unpack (expected 1).

I tried my best going through internet resources and especially StackOverflow but unfortunately could not make it.

1) I would appreciate if you could tell me why I get this error.

2) Is there a nice way to do histogram subplots?

3) What do you think is the optimal solution for me to replace the plot to histogram?

  • 1
    `hist` returns more than one value. But you are trying to assign it to a single variable `l2`. I would suggest you look at how other people have animated histograms in matplotlib. In particular there is an [official example](https://matplotlib.org/gallery/animation/animated_histogram.html) in the docs. – ImportanceOfBeingErnest Jun 08 '19 at 03:05
  • @ImportanceOfBeingErnest Thanks for the link - it is an amazing resource. Unfortunately, I am not able to fix it, but I will try going through the link you gave me. – Daniels Krimans Jun 08 '19 at 03:48
  • 1
    Else, see e.g. [this](https://stackoverflow.com/questions/53235798/python-animation-large-amount-of-data-slow-hist-animation/53244820#53244820), or [this](https://stackoverflow.com/questions/42910622/animation-of-histograms-in-subplot/42912753#42912753), or if you're brave, [this](https://stackoverflow.com/questions/43196308/connector-patch-between-subplots-with-animation-not-visible-matplotlib/43205512#43205512). – ImportanceOfBeingErnest Jun 08 '19 at 09:35

0 Answers0