What's wrong with Pandas plot? - python

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.

Related

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.

Matplotlib - force display within a script

This is highly related to an earlier question by another person a couple of years ago: Matplotlib - Force plot display and then return to main code
I am using Canopy 1.5.5 on MacOSX 10.8.5, with matplotlib 1.4.3.
I will need to load data, look at it, press enter to approve and move to the next dataset (and do that a few thousand times, so it's kind of critical to get this functionality). Here is my MWE:
import numpy as np
from matplotlib import pyplot as plt
plt.ion()
plt.figure()
ind=np.arange(5)
for i in ind:
plt.clf()
plt.scatter(ind,ind+i)
plt.title('this is plot number %i' % i)
plt.show()
u=raw_input("Press any button")
The code seems to do everything EXCEPT actually showing me the plot. If I finish the script (or interrupt it), then I see the current figure.
I have tried everything from the previous answer: with and without interactive mode, with and without plt.show(block=False), every permutation of plt.draw and plt.show, and every backend on my available list.
This seems like a very basic functionality! Please tell me that this can be done. I find it weird that matplolib says here http://matplotlib.org/users/shell.html that "by default the drawing is deferred until the end of the script", but does not have suggestions on how to override the default. Please help!
Your example works for me (my backend is osx), although the figure window appears behind other windows at first. I needed to use alt-tab to raise it to the front.
Try starting your script with the --matplotlib option of IPython. You can select a backend or let it be auto-detected like so: ipython --matplotlib auto yourscript.py
Not sure if you now, but the raw_input function waits for you to press the return key, not just any key.
Edit:
About your last remark: this section explains how to force drawing before the end of the script. This can be done with the draw function. In interactive mode every pyplot command calls draw as well. Drawing in this context means rendering the figure by the backend.

Saving to disk directly with matplotlib

I would like to save my figures to disk without rendering them on the screen and without having to change my rendering backend.
I tried the instructions in here, namely avoiding calling fig.show() nor fig.draw() and just calling fig.savefig, but I noticed that the mere statement fig = plt.figure() already opens a figure on the screen.
How can I save a figure to disk without having to render it, and without having to change my backend?
pyplot has an interactive functionality which will automatically call draw() after most plt.* calls for you.
draw is not automatically called if you don't go through the state machine interface (ex gca().plot(...) would not automatically redraw, but plt.plot(...) would).
See the code, the important function in draw_if_interactive.
This can be turn off via plt.ioff() or by not calling plt.ion() (ipython --pylab automatically turns it on for you).
doc

show() doesn't redraw anymore

I am working in linux and I don't know why using python and matplotlib commands draws me only once the chart I want.
The first time I call show() the plot is drawn, wihtout any problem, but not the second time and the following.
I close the window showing the chart between the two calls. Do you know why and hot to fix it?
Thanks AFG
from numpy import *
from pylab import *
data = array( [ 1,2,3,4,5] )
plot(data)
[<matplotlib.lines.Line2D object at 0x90c98ac>]
show() # this call shows me a plot
#..now I close the window...
data = array( [ 1,2,3,4,5,6] )
plot(data)
[<matplotlib.lines.Line2D object at 0x92dafec>]
show() # this one doesn't shows me anything
in windows this works perfect:
from pylab import *
plot([1,2,3,4])
[<matplotlib.lines.Line2D object at 0x03442C10>]
#close window here
plot([1,2,3,4])
[<matplotlib.lines.Line2D object at 0x035BC570>]
did you try with:
from matplotlib import interactive
interactive(True)
sometimes matplotlib produces some headaches because we have to remember that some options are set in matplotlibrc (such as the backend or the interactive parameters). If you use matplotlib from different editors (IDLE-tk, pycrust-wxpython) or alternating interactive with scripting, then you have to take into account that the configuration that works in one mode could give you problems in the other mode and must be modified programmatically or using a dedicated configuration file.
The example I give, works directly (and without show()) because in matplotlibrc I have interactive set to True as default
You likely have conflicts between your editor/IDE windowing system, and your plot windows.
A very good way around this is to use IPython. IPython is a great interactive environment, and has worked out these issues plus has many other advantages. At the beginning, start IPython with the command (from a terminal window) ipython -pylab to put it in the interactive pylab mode.
I'm guessing that you are doing this in IDLE on Windows because that's where I've noticed this same problem.
From what I've deduced, there is a problem with using the TkAgg backend,which comes with the basic Python dist and appears to be the default for matplotlib, when using matplotlib with IDLE. It has something to do with the way IDLE uses subprocesses because if I start IDLE with the -n option, which disables subprocesses, I don't have this problem. An easy way to start it IDLE with the -n option on Windows is to right click and file and select 'Open with IDLE'. If you do this you should get an IDLE shell which says
=== No Subprocess ===
just above the prompt. For instance, borrowing code from joaquin's solution, you could try this simple code:
from matplotlib import interactive
interactive(True)
from pylab import *
plot([1,2,3,4])
then close the window and type the last line into the console again. It works for me in IDLE with the -n option.
So what can you do? You can always run IDLE in the mode without subprocesses, but there are dangers to that. You can use a different IDE. Many people suggest IPython though I'm not sold on it yet myself. You could also try a different backend for matplotlib. I'm going to try that in a little while cause I've been wondering whether it will work.
show() is only meant to be used once in a program, at the very end: it is a never ending loop that checks for events in the graphic windows.
The normal way of doing what you want is:
# … plot …
draw() # Draws for real
raw_input() # Or anything that waits for user input
# … 2nd plot …
draw()
raw_input()
# Last plot
show() # or, again, draw(); raw_input()
You could try to see whether this works for you.
Alternatively, you can try to change the backend, as some backends work better than others:
import matplotlib
matplotlib.use('TkAgg') # For other backends, do matplotlib.use('') in a shell

Categories

Resources