Ipython interact function plots multiple plots instead of editing the one - python

I am using jupyter v1.00,Ipython v6.0 and conda v4.3.16 for creating interactive plots. I'm using the following code which is supposed to create one plot and editing it after the change, but it creates multiple plots every time the power variable is changed. why it behaves like this? is it a new thing in Ipython 6.0? I can confirm that it is working in Ipython v5.0
%matplotlib inline
from ipywidgets import interact, IntSlider
import matplotlib.pylab as plt
import numpy as np
power_slider = IntSlider(min=1, max=5)
#interact(power=power_slider)
def plot(power):
plt.figure(figsize=(10, 8))
plt.plot(np.power(range(10), power))
return plt

This works for me:
%matplotlib notebook
from ipywidgets import interact, IntSlider
import matplotlib.pylab as plt
import numpy as np
power_slider = IntSlider(min=1, max=5)
#interact(power=power_slider)
def plot(power):
plt.figure(figsize=(10, 8))
plt.plot(np.power(range(10), power))
return plt

Related

Matplotlib.pyplot.ginput() in Python

I am using Jupyter Notebook while running the below code,
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(10)
b = np.sin(a)
plt.plot(a,b)
print("After 3 clicks:")
x = plt.ginput(3)
print(x)
plt.show()
While running this code I get the below warning
UserWarning: Matplotlib is currently using module://matplotlib_inline.backend_inline, which is a non-GUI backend, so cannot show the figure.
x = plt.ginput(3)
Due to this issue, I am not able to click the points on graph nor I am getting the clicked points in output.
The python in my system is of version is 3.9.7 and matplotlib is of version 3.4.3.
The issue is resolved. I used matplotlib.use() before importing pyplot.
import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt
import numpy as np
a = np.arange(10)
b = np.sin(a)
plt.plot(a,b)
print("After 3 clicks:")
x = plt.ginput(3)
print(x)
plt.show()
matplotlib.use('Qt5Agg') this changed the non -gui backend to GUI backend of Qt5Agg.

Is %matplot inline obsolete?

Based on my understanding from this question %matplotlib inline is used so figures can be shown in Jupyter. But figures are shown in Jupyter without using %matplotlib inline perfectly fine. for example the following code:
import numpy as np
import matplotlib.pyplot as plt
plt.plot(np.random.randn(50).cumsum())
plt.show()
So is %matplotlib inline obsolete or am I misunderstanding it's purpose?
The default matplotlib backend in a jupyter notebook is inline. You can inspect this by using print(plt.get_backend()) after loading matplotlib. For example:
import matplotlib.pyplot as plt
print(plt.get_backend())
returns module://ipykernel.pylab.backend_inline
The magic %matplotlib can be used to switch back to inline if you had switched to some other backend. The following cells can illustrate this when run in a notebook.
In [100]:
import matplotlib.pyplot as plt
plt.plot(np.random.randn(50).cumsum())
In [101]:
%matplotlib notebook
plt.plot(np.random.randn(50).cumsum())
In [103]:
%matplotlib inline
plt.plot(np.random.randn(50).cumsum())

Figure not displayed with matplotlib.use('Agg')

I work with matplotlib. When I add the following lines, the figure is not displayed.
import matplotlib
matplotlib.use('Agg')
here is my code :
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plot
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = plot.figure(figsize=(12,9))
def convert_sin_cos(x):
fft_axes = fig.add_subplot(331)
y = np.cos(x)
fft_axes.plot(x,y,'g*')
for i in range(3):
fft_axes = fig.add_subplot(332)
x=np.linspace(0,10,100)
fft_axes.plot(x,i*np.sin(x),'r+')
plot.pause(0.1)
convert_sin_cos(x)
Thanks
That's the idea!
When I run your code, the console says:
matplotlibAgg.py:15: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
How can it be useful? When you're running matplotlib code in a terminal with no windowing system, for example: a cluster (running the same code with different inputs, getting lot of results and without the need to move the data I can plot whatever I need).

Printing cursor coordinates in a matplotib figure in a Jupyter notebook: the smooth way

I want to display the coordinates of my cursor in an image displayed with matplotlib within a Jupyter notebook.
I am using the %matplotlib notebook magic as per this question.
While this provides a nice answer for a static figure, this results in a huge amount of flickering and bugs (the figure sometimes not showing) when used in an interactive setting where the figure is constantly redrawn during slicing. For example,
%matplotlib notebook
from ipywidgets import interact
import matplotlib.pyplot as plt
import numpy as np
vol = np.random.uniform(size=(16, 16, 16))
#interact(z=(0, 15))
def show(z):
plt.imshow(vol[z])
plt.show()
Without %matplotlib notebook, the figure is updating without any flicker, but does not show the cursor coordinates. With the magic, the coordinates are displayed, but the flickering is unbearable.
Is there a way to have pixel coordinates without flickering in that simple situation?
The problem is the use of plt.show(), which will replace the figure. Instead you probably want to update the existing figure.
%matplotlib notebook
from ipywidgets import interact
import matplotlib.pyplot as plt
import numpy as np
vol = np.random.uniform(size=(16, 16, 16))
fig, ax = plt.subplots()
im = ax.imshow(vol[0])
#interact(z=(0, 15))
def show(z):
im.set_array(vol[z])
im.set_clim(vol[z].min(), vol[z].max())
fig.canvas.draw_idle()
Note the the above provides the same functionality as the code in the question, i.e. each array is normalized individually. However, you might decide to set the color normalization only once such that all arrays share the same color limits.
%matplotlib notebook
from ipywidgets import interact
import matplotlib.pyplot as plt
import numpy as np
vol = np.random.uniform(size=(16, 16, 16))
fig, ax = plt.subplots()
im = ax.imshow(vol[0], vmin=vol.min(), vmax=vol.max())
fig.colorbar(im)
#interact(z=(0, 15))
def show(z):
im.set_array(vol[z])
fig.canvas.draw_idle()

Pycharm: show x/y coordinates with matplotlib automatically

If I plot with ipython, I automatically see the x/y coordinates when I move with the mouse over the canvas (see bottom right in screenshot):
import matplotlib.pyplot as plt
import numpy as np
my_random = np.random.random(5)
plt.plot(my_random)
plt.show()
How can the same achieved with Pycharm (my plots appear in the SciView toolwindow)?
If not: is there perhaps an easy workaround for it? (and do I have more possibilities if the plot does not appear in the SciView toolwindow?)
using TkAgg it works:
import matplotlib
matplotlib.use('TkAgg')
import numpy as np
import matplotlib.pyplot as plt
my_random = np.random.random(5)
plt.plot(my_random)
plt.show()

Categories

Resources