1

I wrote the following code based on the matplotlib site example.

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

fig = plt.figure()
nFreqs = 1024
nFFTWindows = 512
viewport = np.ones((nFreqs, nFFTWindows))
im = plt.imshow(viewport, animated=True)


def updatefig(*args):
    global viewport
    print viewport
    viewport = np.roll(viewport, -1, axis=1)
    viewport[:, -1] = 0
    im.set_array(viewport)
    return im,


ani = animation.FuncAnimation(fig, updatefig, interval=50, blit=True)
plt.show()

Before changing the animation works, but now it doesn't. I expected it to start with a purple plot, which slowly turns yellow from the right edge to the left. The viewport variable does update correctly (checked it with print in my function).

I get the static image (all ones, like it was initially):

enter image description here

Where did I go wrong here?

3 Answers 3

4

The problem is you are defining a plot initially with a single colour (1.0) so the colour range is set to this. When you update the figure, the range of colours is 1.0 +- some small value so you don't see the change. You need to set the colour range to between one and zero with vmin/vmax arguments as follows:

im = plt.imshow(viewport, animated=True, vmin=0., vmax=1.)

The rest of the code stays the same and this should work as expected. Another alternative is to add the call,

im.autoscale()

after im.set_array(viewpoint) to force the colour range to be updated each time.

Sign up to request clarification or add additional context in comments.

Comments

0

The imshow plot is initialized with one single value (1 in this case), so any value normalized to the range between 1 and 1 becomes the same color.

In order to change this, you may

  1. initiate the imshowplot with limits for the color (vmin=0, vmax=1).
  2. initiate the imshow plot with a normalization instance

    norm = matplotlib.colors.Normalize(vmin=0, vmax=1)
    im = plt.imshow(arr, norm=norm)
    
  3. Set the limits afterwards using im.set_clim(0,1).

Comments

0

Preferences > IPython Console > Graphics > Backend and change it from "Inline" to "Automatic"

Do not forget to restart you IDE (Spyder, PyCharm, etc.) after applying above change.

Cheers

:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.