简体   繁体   中英

Change colormap of existed matplotlib Figure

Im wonder if there is functionality which helps me to change colormap of already existed figure (FigureCanvasTkAgg actually, which is basically inherite most of Figure methods and features) I have little GUI which has Frame and FigureCanvasTkAgg object on it. And I want to change (using a button for example) color map of this figure. I create such butoon (RadioButton self.cmap) and callback:

def change_cmap(self):

    plt.set_cmap(self.cmap.get()) 
    self.canvas.draw () ## method (like show()) which draw figure

This works actually but when I deal with subplots as figure then this callback change cmap of last subplot ONLY. Any ideas how to deal with it? Why draw() method not update whole figure but only last subplot?

在此处输入图像描述

Process of subplots creating (need to be converted to OO approach)

axes = [] # list of axes objects

fig,ax = plt.subplots ( figsize = (15,10) )

ax = fig.add_subplot ( 311 )
fig.axes[0].plot( offset[:max] , 'k' )
axes.append(ax)

ax = fig.add_subplot ( 312 )
fig.axes[1].plot ( cdp[:max] , 'b' )
axes.append(ax)

ax = plt.subplot ( 313 )
fig.axes[2].imshow( seg[: , :max] , aspect = 'auto' , cmap = cmap)
axes.append(ax)


for i in axes:
  i.set_cmap('seismic) ### This raise 'AxesSubplot' object has no attribute 'set_cmap' error :(

You are using the pyplot functions plt.xxxx() which are acting on the current axes (see https://matplotlib.org/3.2.1/tutorials/introductory/lifecycle.html for more details).

When you have several axes, you are strongly encouraged to use the object-oriented approach. Essentially you will have to loop through your suplots and call im.set_cmap(...) where im is a reference to the image returned by imshow() .

self.ims = []
#create subplots:
fig, axs = plt.subplots(3,1,figsize = (15,10))
axs[0].plot( offset[:max] , 'k' )
axs[1].plot ( cdp[:max] , 'b' )
im = axs[2].imshow( seg[: , :max] , aspect = 'auto' , cmap = cmap)
self.ims.append(im)

def change_cmap(self):
    for im in self.ims:
        im.set_cmap(self.cmap.get()) 
    self.canvas.draw () ## method (like show()) which draw figure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM