OiO.lk Blog python several interactive plot cells in jupyter notwbook
python

several interactive plot cells in jupyter notwbook


Task: I am doing some analysis for which several plots are needed. The first one must be resizeavle and zoomable. Based on what I see in this one, I make other plots in new cells. Those plots do not necessarily need to be interactive. What is important is:
1- to be able to see and work with the first plot at all times,
2- visualise the other plots in their cells, and
3- be able to run "restart and run all"

I tried %matplotlib nbagg for the first plot. This way the first plot will indeed be interactive when I run the code cell by cell. However, when running the next ploting cells, the other plots will be desplayed in the first cell, overwritting it and the plots can not be examined simultaniously. Indeed, "restart and run all" will then display the last plot only.

I tried to stop the interactive display after the first cell using %matplotlib inline, %matplotlib auto or plt.ioff() but non of that worked.

I wonder if someone can help me out here.
Thank you in advance

Here is a minimal reproducible example

The first cell of the notebook reads an image and produce the Fourier Transform (ft) which I want to be able to zoom into at all times

%matplotlib nbagg
from skimage import data, io
from skimage.draw import disk
import numpy as np
import matplotlib.pyplot as plt
img = data.checkerboard()
ft = np.abs(np.fft.fftshift(np.fft.fft2(np.fft.ifftshift(img))))
ft[ft.shape[0]//2,ft.shape[1]//2]=np.mean(ft)
plt.imshow(ft)

The second cell places a mask at one of the peaks in ft and displays the output image. This image should be displayed on its own cell and should not overwrite the previous imge. However, it does not need to be interactive

%matplotlib inline
row, col = 96,104
rr, cc = disk((row, col), 7)
mask=np.zeros_like(img)
mask[rr, cc] = 1
plt.imshow(ft*mask)



You need to sign in to view this answers

Exit mobile version