writing greek letters with subscripts with matplotlib - python

I have to use greek letter with subscript as axes label, so i thought to use Latex symbols. I am using the following code:
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)
and in the plot I have, for example:
ylabel(r'$\boldsymbol{\delta_y}$')
It works fine, it is just slow (it take about 5 sec. to make a plot), I guess it is bacause python has to call an external package.
Any chances I can make it faster?
I am using python 2.6

Try using rc('text', usetex=False).
With this matplotlib will use the internal mathtext instead of your OS's latex installation to render math symbols. See the docs.

Related

Seaborn pairplot is closing immediately [duplicate]

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.

How to make Python wait for a function to end? [duplicate]

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.

How to make the matplotlib subscript and special characters?

I want to make a subscript for the axis label of my plot plotted using matplotlib. I have the following script snapshot:
import matplotlib.pylab as plt
plt.ylabel(r'$A^{2}$')
I have 2 questions:
In the plot, the subscript "2" seems occupying a whole word length in stead of half, which make the plot a little bit weird. How to make the subscript smaller (both in size and the length span)?
How to display "angstroms" (the length unit of 10^-10m) instead of A?
Thanks!
What version of matplotlib are you using? In my version the superscript seems fine (to me).
Im using python 2.6.5 and matplotlib 1.1.0
Use \AA for angstram --> plt.ylabel("$\AA$").
The circle is a bit small, though.
If it's available to you, try using TeX to render your text.
from matplotlib import rc
rc('text', usetex=True)
#the rest of your plotting code here
This should make your fonts and text look a whole lot better.
For more details on matplotlib's TeX rendering capabilities, look here: http://matplotlib.sourceforge.net/users/usetex.html

Matplotlib python show() returns immediately

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.

Multiple figure arrangement using Matplotlib

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.

Categories