matplotlib not displaying image on Jupyter Notebook - python

I am using ubuntu 14.04 and coding in jupyter notebook using anaconda2.7 and everything else is up to date . Today I was coding, every thing worked fine. I closed the notebook and when I reopened it, every thing worked fine except that the image was not being displayed.
%matplotlib inline
import numpy as np
import skimage
from skimage import data
from matplotlib import pyplot as plt
%pylab inline
img = data.camera()
plt.imshow(img,cmap='gray')
this is the code i am using, a really simple one but doesn't display the image
<matplotlib.image.AxesImage at 0xaf7017ac>
this is displayed in the output area
please help

You need to tell matplotlib to actually show the image. Add this at the end of your segment:
plt.show()

To show image in Jupyter Notebook by matplotlib, one should use the %matplotlib inline magic command and plt.show().
As for your code, adding plt.show() after plt.imshow() expression will make the image shown.

If using the inline backend, you just need to call plt.show().
If you are using the notebook backend (%matplotlib notebook), then you should call plt.figure() before plt.imshow(img). This is especially important if you wish to use interactive figures!

change kernel from xpython to original python kernel

Related

Inline Interactive Plots with Julia in jupyter notebook

when I use
%matplotlib notebook
import matplotlib.pyplot as plt
I get interactive plots, i.e. I can also zoom into the figure.
For julia this command does not seem to exist. Any ideas?
You can get interactive plots by using Plotly, either directly with Plotly.jl or through its Plots backend.
Today I am thinking about how to enlarge the plot window interactively without referring to python or plotly or something else in Jupiter notebook. And I find a good solution. It is to use the package Interact. A simple example is as follows. You can get an interactive plot in 3 lines.
import Pkg; Pkg.add("Interact")
using LinearAlgebra, Plots, Interact
#manipulate for n in 10:100, w in 200:1000, h in 200:1000
plot(randn(n),randn(n),seriestype=:scatter,label="n=$(n)",size = (w, h))
end
The result looks like this

Draw matplotlib plot to PNG in Jupyter when inline matplotlib is enabled

I am drawing a confusion matrix heatmap in Jupyter using code similar to the example here using imshow
Matplotlib is set to draw plots inline.
This works fine for outputting to the cell in the notebook, but I want to not output to the cell but instead get PNG data (ideally raw, not saved to a file) in this case only, not in general (in general I want matplotlib to display inline).
I'm not quite sure how to do that; examples I've seen seem to be global in nature (e.g. calling matplotlib.use() before importing pyplot).
Is this possible? How?
A simple way to not display the plot inline, is to use plt.close() at the end of the cell.
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot([1,2,3],[1,2,3])
plt.savefig("image.png")
plt.close()
Turn off interactive mode:
plt.ioff()
To reactivate inline images, use
plt.ion()
%matplotlib inline
To save the PNG image as bytes, but not to a file, pass a file-like io.BytesIO object to plt.savefig instead of a file:
import io
data = io.BytesIO()
plt.savefig(data)

Seaborn Plot doesn't show up

I am creating a bar chart with seaborn, and it's not generating any sort of error, but nothing happens either.
This is the code I have:
import pandas
import numpy
import matplotlib.pyplot as plt
import seaborn
data = pandas.read_csv('fy15crime.csv', low_memory = False)
seaborn.countplot(x="primary_type", data=data)
plt.xlabel('crime')
plt.ylabel('amount')
seaborn.plt.show()
I added "seaborn.plt.show() in an effort to have it show up, but it isn't working still.
You should place this line somewhere in the top cell in Jupyter to enable inline plotting:
%matplotlib inline
It's simply plt.show() you were close. No need for seaborn
I was using PyCharm using a standard Python file and I had the best luck with the following:
Move code to a Jupyter notebook (which can you do inside of PyCharm by right clicking on the project and choosing new - Jupyter Notebook)
If running a chart that takes a lot of processing time it might not have been obvious before, but in Jupyter mode you can easily see when the cell has finished processing.

My graph is not showing up and my iPython Notebooks never stops working

When I run the code below my iPython Notebooks starts working on it (I see black dot in the top right corner), but it never stops. I cannot stop it either by pressing button with the black square.
I opened my other notebook and it shows the histogram without any problem.
What can be the reason?
Thank you.
import matplotlib.pyplot as plt
from numpy.random import normal
gaussian_numbers = normal(size=1000)
plt.hist(gaussian_numbers)
plt.title("Gaussian Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
UPDATE: Created a new notebook. Passed all the code from the previous notebook (including code that gets the data from the internet). Can any other code affect working and displaying the histogram?
There is not other code working when I run histogram.
yo need to use%matplotlib inline
%matplotlib inline
import matplotlib.pyplot as plt
from numpy.random import normal
gaussian_numbers = normal(size=1000)
plt.hist(gaussian_numbers)
plt.title("Gaussian Histogram")
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show()
see this other thread to automatically do this on the IPython configuration Automatically run %matplotlib inline in iPython Notebook

Matplotlib imshow displays blank plot

I figured this should be simple but I guess not.
from scipy import misc
import matplotlib.pyplot as plt
img = misc.imread("Alyson.jpg")
plt.imshow(img)
plt.show()
It draws a blank canvas. Inspecting the array the values were obviously loaded correctly. I don't really know what the issue could be. I've also tried loading it with matplotlib.image.imread and with PIL.Image.open to the same effect.
I'm running Lubuntu 13.04. Here are some versions of various libraries:
Pillow==2.5.2
matplotlib==1.3.1
numpy==1.8.2
scipy==0.14.0
EDIT: SOLVED!
I switched the rendering backend using matplotlib.use("WX").
Not certain what the cause of the problem was, but I switched the backend to wxPython and images loaded with misc.imread or matplotlib.image.imread worked fine.
import matplotlib
matplotlib.use("WX")
import matplotlib.pyplot as plt
...etc

Categories