Issue
I am trying to change content of an image interactively using a slider (e.g. for applying a median operation with different kernel sizes).
While this works well if I only show one resulting image (cf commented lines), I run into trouble when using the subplot function, since the image will not get updated.
What am I missing?
%matplotlib inline
from ipywidgets import interact, widgets
import matplotlib.pyplot as plt
import warnings
from skimage.morphology import disk
from skimage.filters import rank
from skimage.color import rgb2gray
import skimage.data
def f(Median_Size):
selem = disk(int(Median_Size))
with warnings.catch_warnings():
warnings.simplefilter("ignore")
img_median = rank.median(img_gray, selem=selem)
ax_neu.imshow(img_median, cmap="gray")
fig.canvas.draw()
#plt.imshow(img_median, cmap="gray") #This would work
#plt.show()
image = skimage.data.camera() #plt.imread("Test.png")
img_gray = rgb2gray(image)
fig = plt.figure(figsize=(6, 4))
ax_orig = fig.add_subplot(121)
ax_neu = fig.add_subplot(122)
ax_orig.imshow(img_gray, cmap="gray")
ax_neu.imshow(img_gray, cmap="gray")
interact(f, Median_Size=widgets.IntSlider(min=1,max=21,step=2,value=1))
Solution
Using %matplotlib notebook
Instead of the inline backend, you may use the notebook backend. This will allow to call figure.canvas.draw()
as expected from running the code as a script. Replace the line %matplotlib inline
by
%matplotlib notebook
and restart the Kernel.
Using display
You may display
the newly changed figure after it has been changed. The drawback is that it creates the output twice. A workaround would then be to put interact in a new cell and capture the output from the first cell.
%%capture
%matplotlib inline
from ipywidgets import interact, widgets
from IPython.display import display
import matplotlib.pyplot as plt
import warnings
from skimage.morphology import disk
from skimage.filters import rank
from skimage.color import rgb2gray
import skimage.data
def f(Median_Size):
selem = disk(int(Median_Size))
with warnings.catch_warnings():
warnings.simplefilter("ignore")
img_median = rank.median(img_gray, selem=selem)
ax_neu.imshow(img_median, cmap="gray")
fig.canvas.draw()
display(fig)
image = skimage.data.camera() #plt.imread("Test.png")
img_gray = rgb2gray(image)
fig = plt.figure(figsize=(6, 4))
ax_orig = fig.add_subplot(121)
ax_neu = fig.add_subplot(122)
ax_orig.imshow(img_gray, cmap="gray")
ax_neu.imshow(img_gray, cmap="gray")
In a new cell
interact(f, Median_Size=widgets.IntSlider(min=1,max=21,step=2,value=1));
The output would then look like:
Answered By - ImportanceOfBeingErnest
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.