Jupyter Notebook - Matplotlib keep running - python

I just started to use Jupiter Notebook to learn Python. while I am trying out matplotlib with this basic code:
import numpy as np
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
The kernel just keep running and nothing happen. How to resolve this? Is there an dependency issue? My newly installed matplotlib is 1.5.1, python is 3.5.2, numpy is 1.11. Running on MacBook Pro 10.11(El Capitan).

To Visualize the plots created by the matplotlib in Jupiter Notebook or ipython notebook you have add one extra line at the beginning.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
If your matplotlib version is above 1.4, and you are using IPython 3.x you have to use the below code.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook

It likely showed you a Matplotlib popup, with the IP(y) logo. To show it inline you have to use some IPython magic. Run this in a cell somewhere:
%matplotlib inline
After you close the popup it will finish the statement in your kernel

It sometimes takes time until the kernel starts.
Check that the code is color-highlighted. If it is, it means that the kernel is running. Evaluate the cell again. You will notice a * beside that cell, meaning it's running.
And one more thing: Sometimes the plot is displayed but its window hides behind the notebook... Access it from the task bar

Related

Not possible to see the plot created in Jupyter notebooks when using magic: %matplotlib notebook. Windows 10 and Chrome

Hi everyone. I am running jupyter notebook through Chrome and Windows
10. the versions are : Windows 10 Chrome Version 84.0.4147.105 (Official Build) (64-bit) I am using the magic %matplotlib notebook.
To be able to zoom in and out in the plot. The code works in other
machines with same windows and version. However, when I run it in my
personal laptop, the plot is not possible to see it:
I was trying to follow the following info found: help
I have run out of ideas, I restarted the laptop, reinstalled jupyter,
and nothing seems to work. Did anyone have a similar issue? Here is the code:
%matplotlib notebook
import matplotlib.pyplot as plt
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
import numpy as np
import pandas as pd
def g(qi,d):
fig = plt.figure()
x_1 = df['dprod_year']
y_1 = qi*np.exp(-x_1*d)
plt.scatter(x_1,y_1,marker='+')
plt.scatter(df['dprod_year'],df['qoil_1000b'],marker='o',color='grey')
plt.ylim(0,4000)
plt.xlim(0,60)
plt.grid(True,axis='both')
"""
#interact(g,qi=(0.0000,5000,10),d=(0.0000,0.02000,0.0010))
"""
interact(g,qi=widgets.FloatSlider(value=3900,min=0,max=4000,step=10,description='qi:',readout_format='.1f'),
d=widgets.FloatSlider(value=0.0061,min=0.0001,max=0.01,step=0.001,description='d:',readout_format='.5f'))
plt.show()
Thank you very much for your help.
You should try to change from %matplotlib notebook to %matplotlib inline.
If that doesn't work then try to restart the kernel.
It seems that in some cases it helps to repeat the setting of the notebook backend, i.e. try calling it twice like
%matplotlib notebook
%matplotlib notebook
An analysis for why that is can be found in this comment

Matplotlib window doesn't show up, with TkAgg installed

I run this script in a Jupyter notebook:
import matplotlib.pyplot as plt
plt.plot(range(10))
I get a nice plot as an output. I run this debug.py from terminal:
import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()
./debug.py doesn't give a plot. My cursor changes shape, it becomes a cross pointer, but I don't get any plot, and I don't get back my terminal prompt.
Following the recommendation given in this answer, I run in a local python3 shell this:
>>> import matplotlib
>>> print(matplotlib.rcParams['backend'])
The output is TkAgg, as it should be, based on the answer to the aforementioned question.
Why I don't get a matplotlib window then?

Issue with interactive plot on matplotlib within jupyter notebook

I have this issue after selecting the zoom button on the interactive toolbar beneath the plot. When I click to draw a rectangle (for zooming in to), black lines appears. This only happens in the bottom and right-most side of the plot.
I am using Python 3.6.4 and the following packages:
matplotlib (2.1.2)
jupyter (1.0.0)
Code to reproduce issue (within jupyter notebook):
import numpy as np
from numpy.random import random
import matplotlib.pyplot as plt
%matplotlib notebook
plt.plot(random(100), random(100), '.')

Matplotlib not displaying image

I am trying to use matplotlib to view an image in ipython. However, I solely obtain a description of the image as opposed to seeing the actual image:
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> plt.imshow(an_array)
Out[4]: <matplotlib.image.AxesImage at 0x7fb626f063d0>
This is with the backend set to Qt5Agg or TkAgg in .matplotlibrc
I have the same issues whether or not I am working in a conda virtual environment
I am running: Scientific Linux release 6.8 (Carbon), Python 2.7.13, Anaconda 4.3.0 (64-bit)
I have had similar issues on my local computer running MacOSX
I would be most grateful for any advice,
Thanks
Rob
Add %matplotlib inline before import matplotlib.pyplot as plt to enable automatic visualization of your plots when the cell has finished executing.
Note, however, that this works just in iPython notebooks. Normally, you'd need to call plt.show() (without any arguments) to visualize the plots.

Jupyter Notebook ImportError, %matplotlib qt

I am using
%matplotlib inline
to display plots inside the notebook. I would like to disable this for several cells. So, I try
%matplotlib qt
This outputs the following error:
ImportError: Matplotlib qt-based backends require an external PyQt4, PyQt5,
or PySide package to be installed, but it was not found.
I am not sure how to solve this, as everything is up to date.
How can I solve the above?
Is there another way to disable %matplotlib inline in a certain cell without restarting the entire kernel?
You might be able to use plt.switch_backend, although as the documentation states, this is an experimental feature. The following works for me, using matplotlib 1.5 and an IPython 4.0.1:
In [1]: from matplotlib import pyplot as plt
In [2]: import numpy as np
# plot appears inline (default)
In [3]:plt.plot(np.random.randn(10))
Out[3]:[<matplotlib.lines.Line2D at 0x7fac4408e390>]
In [4]: plt.switch_backend('QtAgg4')
# plot appears inside a separate Qt4 window
In [5]:plt.plot(np.random.randn(10))
Out[5]:[<matplotlib.lines.Line2D at 0x7fac3b408a90>]
You might need to change 'QtAgg4' according to whichever version of PyQt you have installed - this could be the cause of the error you mentioned in the question. Another interactive backend that should work on Mac would be 'CocoaAgg'. If the images are very large you could also use the 'Agg' backend to suppress plotting altogether, and instead save the resulting figure(s) straight to disk.
If you don't have a specific backend installed use "agg":
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
Reference: https://github.com/matplotlib/matplotlib/issues/9017

Categories