Matplotlib: set_data effect in imshow for plot

I have a strange error that I cannot fix without your help. After I installed the image with imshow in matplotlib, it remains unchanged all the time, even if I change it using the set_data method. Just take a look at this example:

 import numpy as np from matplotlib import pyplot as plt def newevent(event): haha[1,1] += 1 img.set_data(haha) print img.get_array() # the data is change at this point plt.draw() haha = np.zeros((2,2)) img = plt.imshow(haha) print img.get_array() # [[0,0],[0,0]] plt.connect('button_press_event', newevent) plt.show() 

After I build it, the set_data method set_data not change anything inside the graph. Can someone explain to me why?

EDIT

Just added a few lines to indicate what I really want to do. I want to redraw the data after clicking the mouse button. I do not want to delete the whole figure, because it will be stupid if only one thing changes.

+6
source share
2 answers

The problem is that you did not update the pixel scale after the first call.

When you create an imshow instance, it installs vmin and vmax from the source data and never affects it again. In your code, it sets both vmin and vmax to 0, since your data haha = zeros((2,2)) is zero everywhere.

Your new event should include autoscaling with img.autoscale() or explicitly set new scaling conditions by setting img.norm.vmin/vmax to whatever you prefer.

+12
source

Does this give you the result you expect?

 import numpy as np from matplotlib import pyplot as plt haha = np.zeros((2,2)) img = plt.imshow(haha) print img.get_array() # [[0,0],[0,0]] haha[1,1] += 1 img.set_data(haha) img = plt.imshow(haha) # <<------- added this line print img.get_array() # [[0,0],[0,1]] plt.show() 

When I show the chart twice (once before changing to haha and at the end), it changes.

+2
source

Source: https://habr.com/ru/post/917733/


All Articles