ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 27 Dec 2011 06:38:40 -0600Can I add mathematical formulas to a plot?http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/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.Mon, 26 Dec 2011 10:11:26 -0600http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/Answer by Felix Lawrence for <p>I know that using</p>
<pre><code>text("Sage is really neat!!",(1,2))
</code></pre>
<p>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}}$.</p>
<blockquote>
<p>So is there a way to add a formula to a plot?</p>
</blockquote>
<p>Ideally using LaTeX to enter the formula.</p>
http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/?answer=13067#post-id-13067Shashank 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 stringif 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).Mon, 26 Dec 2011 11:05:10 -0600http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/?answer=13067#post-id-13067Comment by Michael for <p>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.</p>
<p>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:</p>
<pre><code>text(r"Here's a formula $\frac{\sqrt{3}}{2}$ and some text",(1,2))
</code></pre>
<p>Here the <code>r"</code> is necessary due to all the slashes in the stringif you don't include the <code>r</code> then you'll have to escape each of the slashes (check out the python String documentation for more info on that).</p>
http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/?comment=20626#post-id-20626Thanks, this is the kind of solution I was hoping for.Mon, 26 Dec 2011 22:40:15 -0600http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/?comment=20626#post-id-20626Comment by v_2e for <p>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.</p>
<p>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:</p>
<pre><code>text(r"Here's a formula $\frac{\sqrt{3}}{2}$ and some text",(1,2))
</code></pre>
<p>Here the <code>r"</code> is necessary due to all the slashes in the stringif you don't include the <code>r</code> then you'll have to escape each of the slashes (check out the python String documentation for more info on that).</p>
http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/?comment=20625#post-id-20625As 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.Tue, 27 Dec 2011 06:38:40 -0600http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/?comment=20625#post-id-20625Answer by Shashank for <p>I know that using</p>
<pre><code>text("Sage is really neat!!",(1,2))
</code></pre>
<p>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}}$.</p>
<blockquote>
<p>So is there a way to add a formula to a plot?</p>
</blockquote>
<p>Ideally using LaTeX to enter the formula.</p>
http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/?answer=13066#post-id-13066Yes 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.)
l1.draw_frame(0)
fig.subplots_adjust(left=0.17,bottom=0.20,right=0.96,top=0.95)
plt.xlabel(r'{time (s)}')
plt.ylabel(r'{voltage (mV)}')
labels = ax.get_xticklabels()
plt.savefig('tex_demo.eps')
plt.show()
Mon, 26 Dec 2011 10:28:32 -0600http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/?answer=13066#post-id-13066Comment by Michael for <p>Yes you can use the module rc in matplotlib to do it. I just modified a sample file I had to illustrate it.</p>
<pre><code>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.)
l1.draw_frame(0)
fig.subplots_adjust(left=0.17,bottom=0.20,right=0.96,top=0.95)
plt.xlabel(r'{time (s)}')
plt.ylabel(r'{voltage (mV)}')
labels = ax.get_xticklabels()
plt.savefig('tex_demo.eps')
plt.show()
</code></pre>
http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/?comment=20627#post-id-20627Thanks for this example, i'll need some time though to learn what all the commands do.Mon, 26 Dec 2011 22:39:46 -0600http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/?comment=20627#post-id-20627Answer by kcrisman for <p>I know that using</p>
<pre><code>text("Sage is really neat!!",(1,2))
</code></pre>
<p>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}}$.</p>
<blockquote>
<p>So is there a way to add a formula to a plot?</p>
</blockquote>
<p>Ideally using LaTeX to enter the formula.</p>
http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/?answer=13068#post-id-13068Also, 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](http://sagemath.org/doc/reference/sage/plot/plot.html#sage.plot.plot.Graphics.set_legend_options) and the documentation for [axes labels](http://sagemath.org/doc/reference/sage/plot/plot.html#sage.plot.plot.Graphics.axes_labels).Mon, 26 Dec 2011 17:00:13 -0600http://ask.sagemath.org/question/8595/can-i-add-mathematical-formulas-to-a-plot/?answer=13068#post-id-13068