Can we control where Matplotlib places figures on the screen?
I want to generate four figures (in four separate windows) that do not overlap.
From IPython you can do the following:
figure()
get_current_fig_manager().window.wm_geometry("400x600+20+40")
Or equivalently in a Python script:
import pylab as pl
pl.figure()
pl.get_current_fig_manager().window.wm_geometry("400x600+20+40")
pl.show()
Note that this assumes you're using the TkAgg backend.
It is also possible to use the IPython interface with the Qt backend to achieve a similar result:
import matplotlib
import pylab as pl
f1 = pl.figure()
f_manager = pl.get_current_fig_manager()
f_manager.window.move(600, 600)
pl.show()
With f_manager you basically have a PyQt4 object that allows you to modify the window properties as you like.
Not using show() and Matplotlib alone. The simplest solution may be to use savefig(..) and use your favorite OS image viewer. If you need interactivity with the plots, Matplotlib offers backends.
The easiest way I know to do this is to make the window for the figure in your preferred GUI application, and then put the matplotlib figure into this window. There are a bunch of examples of how to do this embedding using different GUI frameworks here.
The code samples can look a bit complicated, but it's mostly boilerplate where you'll only need to modify a few lines.
Related
I have a simple python script which plots some graphs in the same figure. All graphs are created by the draw() and in the end I call the show() function to block.
The script used to work with Python 2.6.6, Matplotlib 0.99.3, and Ubuntu 11.04. Tried to run it under Python 2.7.2, Matplotlib 1.0.1, and Ubuntu 11.10 but the show() function returns immediately without waiting to kill the figure.
Is this a bug? Or a new feature and we'll have to change our scripts? Any ideas?
EDIT: It does keep the plot open under interactive mode, i.e., python -i ..., but it used to work without that, and tried to have plt.ion() in the script and run it in normal mode but no luck.
I had this same problem, and it was caused by calling show() on the Figure object instead of the pyplot object.
Incorrect code. Causes the graph to flash on screen for a brief instant:
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,6,7]
fig = plt.figure()
plt.plot(x, y)
fig.show()
Last line should be as follows to show the graph until it is dismissed:
plt.show()
I think that using show(block=True) should fix your problem.
Had the inverse problem, and it seems that matplotlib will work in interactive or non-interaxctive mode based on a number of things that I could not trace (One way in IDLE, another in system console, one way in normal spyder console, another in a dedicated one ...)
This worked for me:
import matplotlib
matplotlib.interactive(False)
(Actually, I wanted interactive mode, but in your case the inverse should help.)
ion() and ioff() should do the same but the above is on matplotlib's level, not just pyplot or pylab. This works for me although I'm (later) importing pyplot separately and never call matplotlib as such again. I'm thinking that plt.ion() only has an effect on pyplot, not other components of matplotlib that may or may not be involved when using pyplot.
This method works for me on Windows 7, using both Python 2.65 with matplotlib 0.99 and Python 2.75 with matplotlib 1.3.1, across all available python consoles and IDEs on both systems (64-bit, both of them). It did, however, not work on Linux (SuSe 11.3, 64 bit), so there is definitely some platform dependency at play here
To replicate the matplotlib.show() behaviour with the tkagg backend when calling show() on the Figure object:
import Tkinter as Tk
import matplotlib.pyplot as plt
fig = plt.figure()
... your plot commands...
fig.show()
Tk.mainloop()
I had the same problem with this code below.
import matplotlib.pyplot as plt
plt.ion()
fig,ax0 = plt.subplots(figsize=(5.3,4))
plt.show()
I removed plt.ion(), and the plot stays without closing automatically.
It's not clear to me why plotting is done like this:
import pandas as pd
import matplotlib.pyplot as plt
df.boxplot(column='initial_cost', by='Borough', rot=90)
plt.show()
How is the dataframe tied to plt.show()? I've done a few web searches and even took a look at the documentation(!) but couldn't find anything addressing this specifically.
I would expect something more like:
boxplot = df.boxplot(column='initial_cost', by='Borough', rot=90)
plt.show(boxplot)
Or even something like this:
boxplot = df.boxplot(column='initial_cost', by='Borough', rot=90)
boxplot.plt.show()
Matplotlib provides a MATLAB-like state-machine, the pyplot module, that takes care under the hood of instantiating and managing all the objects you need to draw a plot.
Pandas hooks into that in the same fashion. When you call it takes care of loading pyplot and creating a matplotlib Figure, Axes, several Line2D objects and everything that makes a boxplot.
When you call plt.show() it will track all the figures you created with the state-machine API, create a GUI with those figures and take care of displaying it.
If you need more control, you can of course do it all yourself with the object-oriented API. Create a figure, axes, manually draw the canvas, it's all there if needed.
As far as I've seen the common practice is a mix of both: hook into the object-oriented API when needed but still let pyplot take care of displaying or saving everything to a file.
I have a simple python script which plots some graphs in the same figure. All graphs are created by the draw() and in the end I call the show() function to block.
The script used to work with Python 2.6.6, Matplotlib 0.99.3, and Ubuntu 11.04. Tried to run it under Python 2.7.2, Matplotlib 1.0.1, and Ubuntu 11.10 but the show() function returns immediately without waiting to kill the figure.
Is this a bug? Or a new feature and we'll have to change our scripts? Any ideas?
EDIT: It does keep the plot open under interactive mode, i.e., python -i ..., but it used to work without that, and tried to have plt.ion() in the script and run it in normal mode but no luck.
I had this same problem, and it was caused by calling show() on the Figure object instead of the pyplot object.
Incorrect code. Causes the graph to flash on screen for a brief instant:
import matplotlib.pyplot as plt
x = [1,2,3]
y = [5,6,7]
fig = plt.figure()
plt.plot(x, y)
fig.show()
Last line should be as follows to show the graph until it is dismissed:
plt.show()
I think that using show(block=True) should fix your problem.
Had the inverse problem, and it seems that matplotlib will work in interactive or non-interaxctive mode based on a number of things that I could not trace (One way in IDLE, another in system console, one way in normal spyder console, another in a dedicated one ...)
This worked for me:
import matplotlib
matplotlib.interactive(False)
(Actually, I wanted interactive mode, but in your case the inverse should help.)
ion() and ioff() should do the same but the above is on matplotlib's level, not just pyplot or pylab. This works for me although I'm (later) importing pyplot separately and never call matplotlib as such again. I'm thinking that plt.ion() only has an effect on pyplot, not other components of matplotlib that may or may not be involved when using pyplot.
This method works for me on Windows 7, using both Python 2.65 with matplotlib 0.99 and Python 2.75 with matplotlib 1.3.1, across all available python consoles and IDEs on both systems (64-bit, both of them). It did, however, not work on Linux (SuSe 11.3, 64 bit), so there is definitely some platform dependency at play here
To replicate the matplotlib.show() behaviour with the tkagg backend when calling show() on the Figure object:
import Tkinter as Tk
import matplotlib.pyplot as plt
fig = plt.figure()
... your plot commands...
fig.show()
Tk.mainloop()
I had the same problem with this code below.
import matplotlib.pyplot as plt
plt.ion()
fig,ax0 = plt.subplots(figsize=(5.3,4))
plt.show()
I removed plt.ion(), and the plot stays without closing automatically.
I can plot in Python using either:
import matplotlib
matplotlib.pyplot.plot(...)
Or:
import pylab
pylab.plot(...)
Both of these use matplotlib.
Which is recommend as the correct method to plot? Why?
Official docs: Matplotlib, pyplot and pylab: how are they related?
Both of those imports boil down do doing exactly the same thing and will run the exact same code, it is just different ways of importing the modules.
Also note that matplotlib has two interface layers, a state-machine layer managed by pyplot and the OO interface pyplot is built on top of, see How can I attach a pyplot function to a figure instance?
pylab is a clean way to bulk import a whole slew of helpful functions (the pyplot state machine function, most of numpy) into a single name space. The main reason this exists (to my understanding) is to work with ipython to make a very nice interactive shell which more-or-less replicates MATLAB (to make the transition easier and because it is good for playing around). See pylab.py and matplotlib/pylab.py
At some level, this is purely a matter of taste and depends a bit on what you are doing.
If you are not embedding in a gui (either using a non-interactive backend for bulk scripts or using one of the provided interactive backends) the typical thing to do is
import matplotlib.pyplot as plt
import numpy as np
plt.plot(....)
which doesn't pollute the name space. I prefer this so I can keep track of where stuff came from.
If you use
ipython --pylab
this is equivalent to running
from pylab import *
It is now recommended that for new versions of ipython you use
ipython --matplotlib
which will set up all the proper background details to make the interactive backends to work nicely, but will not bulk import anything. You will need to explicitly import the modules want.
import numpy as np
import matplotlib.pyplot as plt
is a good start.
If you are embedding matplotlib in a gui you don't want to import pyplot as that will start extra gui main loops, and exactly what you should import depends on exactly what you are doing.
From the official documentation, as shown below, the recommendation is to use matplotlib.pyplot. This is not an opinion.
The documentation at Matplotlib, pyplot and pylab: how are they related?, which also describes the difference between pyplot and pylab, states: "Although many examples use pylab, it is no longer recommended.".
2021-05-06 Edit:
From The pylab API (disapproved)
Since heavily importing into the global namespace may result in unexpected behavior, the use of pylab is strongly discouraged. Use matplotlib.pyplot instead.
Here is a simple matlab script to read a csv file, and generate a plot (with which I can zoom in with the mouse as I desire). I would like to see an example of how this is done in python and mathplotlib.
data = csvread('foo.csv'); % read csv data into vector 'data'
figure; % create figure
plot (data, 'b'); % plot the data in blue
In general, the examples in mathplotlib tutorials I've seen will create a static graph, but it's not interactively "zoomable". Would any python expert care to share an equivalent?
Thanks
import matplotlib.pyplot as plt
import numpy as np
arr=np.genfromtxt('foo.csv',delimiter=',')
plt.plot(arr[:,0],arr[:,1],'b-')
plt.show()
on this data (foo.csv):
1,2
2,4
3,9
produces
When you setup the matplotlibrc, one of the key parameters you need to set is the backend. Which backend you choose depends on your OS and installation.
For any typical OS there should be a backend that allows you to pan and zoom the plot interactively. (GtkAgg works on Ubuntu). The buttons highlighted in red allow you to pan and zoom, respectively.
Since you're familiar with Matlab, I'd suggest using the pylab interface to matplotlib - it mostly mimics Matlab's plotting. As unutbu says, the zoomability of the plot is determined by the backend you use, a separate issue.
from pylab import *
data = genfromtxt("file.csv")
plot(data, 'b')