Ask Your Question

Can I add mathematical formulas to a plot?

asked 2011-12-26 10:11:26 -0500

Michael gravatar image

I know that using

text("Sage is really neat!!",(1,2))

one can easily add text to a plot in Sage. But searching the documentation I couldn't find a way to add a formula, say $\frac{1}{\sqrt{x^2-1}}$.

So is there a way to add a formula to a plot?

Ideally using LaTeX to enter the formula.

edit retag flag offensive close merge delete

3 answers

Sort by » oldest newest most voted

answered 2011-12-26 11:05:10 -0500

Felix Lawrence gravatar image

updated 2011-12-26 11:05:45 -0500

Shashank told you how to do it in matplotlib (which comes with Sage) - matplotlib allows you a lot of control over your plots (I often use it for this reason), but as you can see, it involves a fair bit of boilerplate compared to Sage's plotting.

If you want to add a formula to a Sage plot, then you can just use latex inside dollar signs, like you would in a .tex document:

text(r"Here's a formula $\frac{\sqrt{3}}{2}$ and some text",(1,2))

Here the r" is necessary due to all the slashes in the string–if you don't include the r then you'll have to escape each of the slashes (check out the python String documentation for more info on that).

edit flag offensive delete link more



Thanks, this is the kind of solution I was hoping for.

Michael gravatar imageMichael ( 2011-12-26 22:40:15 -0500 )edit

As for me, I didn't know about **r""** and thus just had been using two consecutive backslashes. For example, "$\\frac{1}{\\sqrt{2}}$". And it used to work for me. But now I think: is it a right way too or does it just work until one day... ? Thanks.

v_2e gravatar imagev_2e ( 2011-12-27 06:38:40 -0500 )edit

answered 2011-12-26 10:28:32 -0500

Shashank gravatar image

updated 2011-12-26 10:29:13 -0500

Yes you can use the module rc in matplotlib to do it. I just modified a sample file I had to illustrate it.

from matplotlib import rc
from numpy import arange, cos, pi, sin, sqrt

import matplotlib.pyplot as plt
plt.rc('text', usetex=True)

plt.rc('font',**{'family':'serif','serif':['Computer Modern Roman'],'size':14})
fig = plt.figure(figsize=(4,2.7))
ax = fig.add_subplot(111)
t = arange(0.0, 1.0+0.01, 0.01)
u = 1/sqrt(t*t+1)

p2=plt.plot(t, u,'g-',label=r'$\frac{1}{\sqrt{x^2+1}}$')
l1=plt.legend(loc=1, borderaxespad=0.)

plt.xlabel(r'{time (s)}')
plt.ylabel(r'{voltage (mV)}')
labels = ax.get_xticklabels()

edit flag offensive delete link more


Thanks for this example, i'll need some time though to learn what all the commands do.

Michael gravatar imageMichael ( 2011-12-26 22:39:46 -0500 )edit

answered 2011-12-26 17:00:13 -0500

kcrisman gravatar image

Also, if you simply want to add a legend with LaTeX or to add labels for the axes, we have wrapped the matplotlib functionality for this. See for instance ways to mess with legends and the documentation for axes labels.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2011-12-26 10:11:26 -0500

Seen: 2,722 times

Last updated: Dec 26 '11