Matplotlib notebook magic inline/plotting commands not working - python

I am running a jupyter notebook (5.7.0) with pandas (0.23.4) and matplotlib (3.0.1). When I try to plot using the
pandas.DataFrame.plot()
function it gives me the text of the object:
<pandas.plotting._core.FramePlotMethods object at 0x7f612fdb74a8>
Naturally I googled this, and have sinced tried the magic commands:
%matplotlib inline
and
%matplotlib notebook
in the very first cell after shutting down the notebook and restarting it, and still it seems to make no difference.
So for completeness sake, my first cell is the magic command, and in the second I import numpy,pandas and matplotlib. In the following cells are create my array and pass it to the DataFrame command. Finally I call:
pandas.DataFrame.plot(df)
and the text of the object (from above) appears, regardless of whether I run the magic commands or not. Have I got the incorrect version of something? Do I need to restart the entire notebook server?

You misunderstood the way plotting works. What you are trying here is
import pandas
df=pandas.DataFrame([1,2,3])
pandas.DataFrame.plot(df)
What you need to do instead is
import pandas
df=pandas.DataFrame([1,2,3])
df.plot()

Related

Autocompletion not working in Jupyter notebook on Vscode

I am using Jupyter on VsCode. I have two questions about its autocomplete feature. I have Jupyter Keymap, Jupyter Notebook Renderers, IntelliCode, and Pylance installed.
I have titanic_train.csv in the folder I m working on and my first few lines of codes are as follows:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
train=pd.read_csv('./titanic_train.csv')
train.head()
which gives this
but when I type first initial of Survived column name as train['Sur'] I expect it to give me some suggestion about its autocomplete feature but nothing comes up and I have to type the full name each letter correctly, every time, which is tiring and easy to mistype of course. How to make its autocomplete feature work?
My second question is about tab key. When I use it, it doesn't work as one expects, meaning autocompletion again. it creates 4 to 5 spaces instead. How can I use tab instead of ctrl+space on Vscode and Jupyter?
For the first question, there is no way to gain the name.
For the second question, TAB means 4 space usually. You can change it in the lower right corner of the vscode interface.
You can also change the size of TAB.

What does % mean when it's placed at the beginning of a code in Python? [duplicate]

What exactly is the use of %matplotlib inline?
%matplotlib is a magic function in IPython. I'll quote the relevant documentation here for you to read for convenience:
IPython has a set of predefined ‘magic functions’ that you can call with a command line style syntax. There are two kinds of magics, line-oriented and cell-oriented. Line magics are prefixed with the % character and work much like OS command-line calls: they get as an argument the rest of the line, where arguments are passed without parentheses or quotes. Lines magics can return results and can be used in the right hand side of an assignment. Cell magics are prefixed with a double %%, and they are functions that get as an argument not only the rest of the line, but also the lines below it in a separate argument.
%matplotlib inline sets the backend of matplotlib to the 'inline' backend:
With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.
When using the 'inline' backend, your matplotlib graphs will be included in your notebook, next to the code. It may be worth also reading How to make IPython notebook matplotlib plot inline for reference on how to use it in your code.
If you want interactivity as well, you can use the nbagg backend with %matplotlib notebook (in IPython 3.x), as described here.
Provided you are running IPython, the %matplotlib inline will make your plot outputs appear and be stored within the notebook.
According to documentation
To set this up, before any plotting or import of matplotlib is
performed you must execute the %matplotlib magic command. This
performs the necessary behind-the-scenes setup for IPython to work
correctly hand in hand with matplotlib; it does not, however,
actually execute any Python import commands, that is, no names are
added to the namespace.
A particularly interesting backend, provided by IPython, is the
inline backend. This is available only for the Jupyter Notebook and
the Jupyter QtConsole. It can be invoked as follows:
%matplotlib inline
With this backend, the output of plotting commands is displayed inline
within frontends like the Jupyter notebook, directly below the code
cell that produced it. The resulting plots will then also be stored in
the notebook document.
To explain it clear:
If you don't like it like this:
add %matplotlib inline
and there you have it in your jupyter notebook.
If you want to add plots to your Jupyter notebook, then %matplotlib inline is a standard solution. And there are other magic commands will use matplotlib interactively within Jupyter.
%matplotlib: any plt plot command will now cause a figure window to open, and further commands can be run to update the plot. Some changes will not draw automatically, to force an update, use plt.draw()
%matplotlib notebook: will lead to interactive plots embedded within the notebook, you can zoom and resize the figure
%matplotlib inline: only draw static images in the notebook
It just means that any graph which we are creating as a part of our code will appear in the same notebook and not in separate window which would happen if we have not used this magic statement.
TL;DR
%matplotlib inline - Displays output inline
IPython kernel has the ability to display plots by executing code. The IPython kernel is designed to work seamlessly with the matplotlib plotting library to provide this functionality.
%matplotlib is a magic command which performs the necessary behind-the-scenes setup for IPython to work correctly hand-in-hand with matplotlib;
it does not execute any Python import commands, that is, no names are added to the namespace.
Display output in separate window
%matplotlib
Display output inline
(available only for the Jupyter Notebook and the Jupyter QtConsole)
%matplotlib inline
Display with interactive backends
(valid values 'GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template')
%matplotlib gtk
Example - GTK3Agg - An Agg rendering to a GTK 3.x canvas (requires PyGObject and pycairo or cairocffi).
More details about matplotlib interactive backends: here
Starting with IPython 5.0 and matplotlib 2.0 you can avoid the use of
IPython’s specific magic and use matplotlib.pyplot.ion()/matplotlib.pyplot.ioff()
which have the advantages of working outside of IPython as well.
Refer: IPython Rich Output - Interactive Plotting
Starting with IPython 5.0 and matplotlib 2.0 you can avoid the use of
IPython’s specific magic and use
matplotlib.pyplot.ion()/matplotlib.pyplot.ioff() which have the
advantages of working outside of IPython as well.
ipython docs
If you don't know what backend is , you can read this:
https://matplotlib.org/stable/users/explain/backends.html
Some people use matplotlib interactively from the python shell and
have plotting windows pop up when they type commands. Some people run
Jupyter notebooks and draw inline plots for quick data analysis.
Others embed matplotlib into graphical user interfaces like wxpython
or pygtk to build rich applications. Some people use matplotlib in
batch scripts to generate postscript images from numerical
simulations, and still others run web application servers to
dynamically serve up graphs. To support all of these use cases,
matplotlib can target different outputs, and each of these
capabilities is called a backend; the "frontend" is the user facing
code, i.e., the plotting code, whereas the "backend" does all the hard
work behind-the-scenes to make the figure.
So when you type %matplotlib inline , it activates the inline backend. As discussed in the previous posts :
With this backend, the output of plotting commands is displayed inline
within frontends like the Jupyter notebook, directly below the code
cell that produced it. The resulting plots will then also be stored in
the notebook document.
Provided you are running Jupyter Notebook, the %matplotlib inline command will make your plot outputs appear in the notebook, also can be stored.
I think that with recent versions of Jupyter/matplotlib, the figures are plotted "inline" without the need to use %matplotlib inline.
So one might think that this command is now useless… but to my understanding, it creates a "manager" which configures the plotting parameters. Matplotlib looks for an existing manager when creating a figure, and creates one if necessary. Inside matplotlib.pyplot.figure:
manager = _pylab_helpers.Gcf.get_fig_manager(num)
if manager is None:
# not relevant stuff…
manager = new_figure_manager(
num, figsize=figsize, dpi=dpi,
facecolor=facecolor, edgecolor=edgecolor, frameon=frameon,
FigureClass=FigureClass, **kwargs)
Now, setting a plotting parameter (rcParams) will not create a "manager" by itself. So when plotting a figure for the first time, a new manager will be created and will overwrite your parameters.
Comment/un-comment the %matplotlib inline and see what happens. (Do not forget to restart the kernel between each try!)
import matplotlib.pyplot as plt
from matplotlib.image import imread
# %matplotlib inline
plt.rcParams["figure.dpi"] = 200
plt.imshow(imread("path_to_your_image"))
print(plt.rcParams["figure.dpi"])
It is not mandatory to write that. It worked fine for me without (%matplotlib) magic function.
I am using Sypder compiler, one that comes with in Anaconda.

What does “‘%’” when calling packages inline in python [duplicate]

What exactly is the use of %matplotlib inline?
%matplotlib is a magic function in IPython. I'll quote the relevant documentation here for you to read for convenience:
IPython has a set of predefined ‘magic functions’ that you can call with a command line style syntax. There are two kinds of magics, line-oriented and cell-oriented. Line magics are prefixed with the % character and work much like OS command-line calls: they get as an argument the rest of the line, where arguments are passed without parentheses or quotes. Lines magics can return results and can be used in the right hand side of an assignment. Cell magics are prefixed with a double %%, and they are functions that get as an argument not only the rest of the line, but also the lines below it in a separate argument.
%matplotlib inline sets the backend of matplotlib to the 'inline' backend:
With this backend, the output of plotting commands is displayed inline within frontends like the Jupyter notebook, directly below the code cell that produced it. The resulting plots will then also be stored in the notebook document.
When using the 'inline' backend, your matplotlib graphs will be included in your notebook, next to the code. It may be worth also reading How to make IPython notebook matplotlib plot inline for reference on how to use it in your code.
If you want interactivity as well, you can use the nbagg backend with %matplotlib notebook (in IPython 3.x), as described here.
Provided you are running IPython, the %matplotlib inline will make your plot outputs appear and be stored within the notebook.
According to documentation
To set this up, before any plotting or import of matplotlib is
performed you must execute the %matplotlib magic command. This
performs the necessary behind-the-scenes setup for IPython to work
correctly hand in hand with matplotlib; it does not, however,
actually execute any Python import commands, that is, no names are
added to the namespace.
A particularly interesting backend, provided by IPython, is the
inline backend. This is available only for the Jupyter Notebook and
the Jupyter QtConsole. It can be invoked as follows:
%matplotlib inline
With this backend, the output of plotting commands is displayed inline
within frontends like the Jupyter notebook, directly below the code
cell that produced it. The resulting plots will then also be stored in
the notebook document.
To explain it clear:
If you don't like it like this:
add %matplotlib inline
and there you have it in your jupyter notebook.
If you want to add plots to your Jupyter notebook, then %matplotlib inline is a standard solution. And there are other magic commands will use matplotlib interactively within Jupyter.
%matplotlib: any plt plot command will now cause a figure window to open, and further commands can be run to update the plot. Some changes will not draw automatically, to force an update, use plt.draw()
%matplotlib notebook: will lead to interactive plots embedded within the notebook, you can zoom and resize the figure
%matplotlib inline: only draw static images in the notebook
It just means that any graph which we are creating as a part of our code will appear in the same notebook and not in separate window which would happen if we have not used this magic statement.
TL;DR
%matplotlib inline - Displays output inline
IPython kernel has the ability to display plots by executing code. The IPython kernel is designed to work seamlessly with the matplotlib plotting library to provide this functionality.
%matplotlib is a magic command which performs the necessary behind-the-scenes setup for IPython to work correctly hand-in-hand with matplotlib;
it does not execute any Python import commands, that is, no names are added to the namespace.
Display output in separate window
%matplotlib
Display output inline
(available only for the Jupyter Notebook and the Jupyter QtConsole)
%matplotlib inline
Display with interactive backends
(valid values 'GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template')
%matplotlib gtk
Example - GTK3Agg - An Agg rendering to a GTK 3.x canvas (requires PyGObject and pycairo or cairocffi).
More details about matplotlib interactive backends: here
Starting with IPython 5.0 and matplotlib 2.0 you can avoid the use of
IPython’s specific magic and use matplotlib.pyplot.ion()/matplotlib.pyplot.ioff()
which have the advantages of working outside of IPython as well.
Refer: IPython Rich Output - Interactive Plotting
Starting with IPython 5.0 and matplotlib 2.0 you can avoid the use of
IPython’s specific magic and use
matplotlib.pyplot.ion()/matplotlib.pyplot.ioff() which have the
advantages of working outside of IPython as well.
ipython docs
If you don't know what backend is , you can read this:
https://matplotlib.org/stable/users/explain/backends.html
Some people use matplotlib interactively from the python shell and
have plotting windows pop up when they type commands. Some people run
Jupyter notebooks and draw inline plots for quick data analysis.
Others embed matplotlib into graphical user interfaces like wxpython
or pygtk to build rich applications. Some people use matplotlib in
batch scripts to generate postscript images from numerical
simulations, and still others run web application servers to
dynamically serve up graphs. To support all of these use cases,
matplotlib can target different outputs, and each of these
capabilities is called a backend; the "frontend" is the user facing
code, i.e., the plotting code, whereas the "backend" does all the hard
work behind-the-scenes to make the figure.
So when you type %matplotlib inline , it activates the inline backend. As discussed in the previous posts :
With this backend, the output of plotting commands is displayed inline
within frontends like the Jupyter notebook, directly below the code
cell that produced it. The resulting plots will then also be stored in
the notebook document.
Provided you are running Jupyter Notebook, the %matplotlib inline command will make your plot outputs appear in the notebook, also can be stored.
I think that with recent versions of Jupyter/matplotlib, the figures are plotted "inline" without the need to use %matplotlib inline.
So one might think that this command is now useless… but to my understanding, it creates a "manager" which configures the plotting parameters. Matplotlib looks for an existing manager when creating a figure, and creates one if necessary. Inside matplotlib.pyplot.figure:
manager = _pylab_helpers.Gcf.get_fig_manager(num)
if manager is None:
# not relevant stuff…
manager = new_figure_manager(
num, figsize=figsize, dpi=dpi,
facecolor=facecolor, edgecolor=edgecolor, frameon=frameon,
FigureClass=FigureClass, **kwargs)
Now, setting a plotting parameter (rcParams) will not create a "manager" by itself. So when plotting a figure for the first time, a new manager will be created and will overwrite your parameters.
Comment/un-comment the %matplotlib inline and see what happens. (Do not forget to restart the kernel between each try!)
import matplotlib.pyplot as plt
from matplotlib.image import imread
# %matplotlib inline
plt.rcParams["figure.dpi"] = 200
plt.imshow(imread("path_to_your_image"))
print(plt.rcParams["figure.dpi"])
It is not mandatory to write that. It worked fine for me without (%matplotlib) magic function.
I am using Sypder compiler, one that comes with in Anaconda.

How to stop bokeh from opening a new tab in Jupyter Notebook?

First of all, before this is tagged as duplicate, I have read the other solutions and unfortunately none of them worked for me.
My problem is that I want to display a bokeh plot in Juypter Notebook (and only in Jupyter Notebook), not in a new tab/window.
In the official documentation here I am told that I only need to change
output_file
to
output_notebook
Even though the plot is now displayed inline if I do that, bokeh won't stop also opening a new tab and needlessly displaying the plot there.
Since I'm gonna create lots of plots in my project, it'd be very nice to not always have to close this new tab and return to notebook, but just have it stop creating new tabs, just as it would work with e.g. matplotlib.
What confuses me is that if I load up the official tutorial and enter code there, for example
import numpy as np
x = np.linspace(0, 10, 100)
y = np.exp(x)
p = figure()
p.line(x, y)
show(p)
there is no new tab opened. If I now run the same code locally on my machine's Juypter Notebook, it does open up a new tab.
I've been trying for a while now to fix this, any help would be very much appreciated.
Thanks in advance, Vincent
You need to call output_notebook at the top of your notebook, but only call output_notebook. If you call output_file at all, that activates a persistent mode that saves output to files, and causes show to open new tabs with those files. You would need to call reset_output to clear that persistent mode.
As a convenience, since several users asked for it, if no output mode is supplied, show behaves as though output_file was called as a default. The reason a tab is not opened from the Binder tutorial is because it is not technically possible for code running remotely on Binder server to open a tab on your local browser (which is a very good thing).
Adding an explicit example to #bigreddot's answer:
You might have ran bokeh.io.output_file() somewhere in your notebook, to save noteable graphs. However, now you only want to experiment with some plots quickly to inspect the data.
Simply reset your settings to stop saving to file in any cell in your notebook like so:
import bokeh.io
# this is here only for completeness to clarify where
# the methods are nested (you probably already imported this earlier)
bokeh.io.reset_output()
bokeh.io.output_notebook()
You can activate saving to file again later to keep the interesting graphs.
You can import:
from bokeh.plotting import output_notebook
And call output_notebook before your figures declaration, then you just show the figure using show. See the doc.

What's wrong with Pandas plot?

I'm following a book by Wes McKinney and in the section introducing pandas, he's given a simple example of plotting a pandas Data Frame. Here're the lines I wrote:
tz_counts = frame['tz'].value_counts() # frame is a Data Frame
tz_counts[:10] # works fine till here. I can see the key-value I wanted
tz_counts[:10].plot(kind='barh', rot=0)
It just prints a line on screen that says
<matplotlib.axes.AxesSubplot object at 0x3d14ed0>
rather than displaying a plot window as I'd expect with matplotlib's plot function. What's wrong here? How can I make it work?
Matplotlib doesn't show the plot until you tell it to, unless you're in "interactive" mode.
Short Answer: Call plt.show() when you're ready to display the plot.
This starts the gui mainloop of whatever backend you're using, so it is blocking. (i.e. execution will stop until you close the window)
If you want it to show up automatically without stopping execution, you can turn on interactive mode either with plt.ion() or by using ipython --pylab.
However, using --pylab mode in ipython will import all of numpy, matplotlib.pyplot, and a few other things into the global namespace. This is convenient for interactive use, but teaches very bad habits and overwrites functions in the standard library (e.g. min, max, etc).
You can still use matplotlib's interactive mode in ipython without using "pylab" mode to avoid the global import. Just call plt.ion()
Matplotlib's default TkAgg backend will work in interactive mode with any python shell (not just ipython). However, other backends can't avoid blocking further execution without the gui mainloop being run in a separate thread. If you're using a different backend, then you'll need to tell ipython this with the --gui=<backend> option.
Try using:
%matplotlib
tz_counts = frame['tz'].value_counts()
tz_counts[:10].plot(kind='barh', rot=0)
Using % matplotlib prevents importing * from pylab and numpy which in turn prevents variable clobbering.

Categories

Resources