ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 27 Oct 2017 19:10:00 +0200Is it possible to plot with symbols for x and y ticks?https://ask.sagemath.org/question/39260/is-it-possible-to-plot-with-symbols-for-x-and-y-ticks/ I'd like to plot a function similar to something like: y= A*sin(w*x)
Something along the lines of
x = var('x')
w = var('w')
A = var('A')
y = A*sin(w*x)
assume(w>0)
assume(A>0)
Then be able to do:
plot(y(x=x), (x,-2*pi()/w,2*pi()/w)
And have sage plot the function, where for the x-axis the ticks would be something like (-2*pi/w, -pi/w, 0, pi/w, 2*pi/w)
and the y-ticks would look like (-A, -A/2,0, A/2, A).
Hopefully that makes sense? Is this possible?
Tue, 24 Oct 2017 04:31:08 +0200https://ask.sagemath.org/question/39260/is-it-possible-to-plot-with-symbols-for-x-and-y-ticks/Comment by dan_fulea for <p>I'd like to plot a function similar to something like: y= A<em>sin(w</em>x)
Something along the lines of </p>
<p>x = var('x')</p>
<p>w = var('w')</p>
<p>A = var('A')</p>
<p>y = A<em>sin(w</em>x)</p>
<p>assume(w>0)</p>
<p>assume(A>0)</p>
<p>Then be able to do:</p>
<p>plot(y(x=x), (x,-2<em>pi()/w,2</em>pi()/w)</p>
<p>And have sage plot the function, where for the x-axis the ticks would be something like (-2<em>pi/w, -pi/w, 0, pi/w, 2</em>pi/w)
and the y-ticks would look like (-A, -A/2,0, A/2, A).</p>
<p>Hopefully that makes sense? Is this possible?</p>
https://ask.sagemath.org/question/39260/is-it-possible-to-plot-with-symbols-for-x-and-y-ticks/?comment=39268#post-id-39268Please give the mathematical description of the task. (To plot something, one needs to evaluate to a real number a function, so there should be no variables as obstacles for the plot around.)
So we **first fix** parameters $w,A$, and need a 2D-plot of the function of one variable, $x$, given by the formula
$$ f(x) = A\sin(wx)\ ,$$
or we **fix** (and give) a variable $A$, and need a $3D$-Plot of the function
$$g(x,w)=A\sin(wx)\ ,$$
or something related...?!
The "ticks" are only simple marks on the coordinate axes?Tue, 24 Oct 2017 19:38:27 +0200https://ask.sagemath.org/question/39260/is-it-possible-to-plot-with-symbols-for-x-and-y-ticks/?comment=39268#post-id-39268Comment by Bob for <p>I'd like to plot a function similar to something like: y= A<em>sin(w</em>x)
Something along the lines of </p>
<p>x = var('x')</p>
<p>w = var('w')</p>
<p>A = var('A')</p>
<p>y = A<em>sin(w</em>x)</p>
<p>assume(w>0)</p>
<p>assume(A>0)</p>
<p>Then be able to do:</p>
<p>plot(y(x=x), (x,-2<em>pi()/w,2</em>pi()/w)</p>
<p>And have sage plot the function, where for the x-axis the ticks would be something like (-2<em>pi/w, -pi/w, 0, pi/w, 2</em>pi/w)
and the y-ticks would look like (-A, -A/2,0, A/2, A).</p>
<p>Hopefully that makes sense? Is this possible?</p>
https://ask.sagemath.org/question/39260/is-it-possible-to-plot-with-symbols-for-x-and-y-ticks/?comment=39272#post-id-39272Hi Dan,
thanks for your reply. The first option you gave was the situation I was trying to describe: Fix parameters *w*, *A* and get a 2D-plot of the function of variable *x*.
And yes the tics I'm referring to are simple marks on the coordinate axes -I'm using gnuplot terminology there.
I know you could do this trivially with something like plot(y(x=x, w=2, A=5), (x,-pi(),pi())
But I was interested to see if Sage could plot it without me providing it a specific constant for *A* and *w*, but instead using the "assume" operator to define those constants as being real and positive, and plot with the axes expressed in terms of *A* and *w* rather than numerically.
Obviously this is just an example, the function I'm dealing with is a bit more complicated.
Does that clarify my question?Wed, 25 Oct 2017 03:38:32 +0200https://ask.sagemath.org/question/39260/is-it-possible-to-plot-with-symbols-for-x-and-y-ticks/?comment=39272#post-id-39272Answer by B r u n o for <p>I'd like to plot a function similar to something like: y= A<em>sin(w</em>x)
Something along the lines of </p>
<p>x = var('x')</p>
<p>w = var('w')</p>
<p>A = var('A')</p>
<p>y = A<em>sin(w</em>x)</p>
<p>assume(w>0)</p>
<p>assume(A>0)</p>
<p>Then be able to do:</p>
<p>plot(y(x=x), (x,-2<em>pi()/w,2</em>pi()/w)</p>
<p>And have sage plot the function, where for the x-axis the ticks would be something like (-2<em>pi/w, -pi/w, 0, pi/w, 2</em>pi/w)
and the y-ticks would look like (-A, -A/2,0, A/2, A).</p>
<p>Hopefully that makes sense? Is this possible?</p>
https://ask.sagemath.org/question/39260/is-it-possible-to-plot-with-symbols-for-x-and-y-ticks/?answer=39274#post-id-39274If I correctly understand your question, you want to plot some *symbolic* function such as $x\mapsto A \sin(wx)$, where $A$ and $w$ are symbolic variables. This is not *per se* possible since plotting needs floating point values. The solution consists in plotting $x\mapsto \sin(x)$ and changing the labels on the axes. The command
plot(sin(x), (x,-2*pi, 2*pi), ticks = [xt, yt], tick_formatter = [xt_lbl, yt_lbl])
plots the function $x\mapsto \sin(x)$, putting *ticks* on the x-axis at each location given in the list `xt`, and *ticks* on the y-axis using `yt`. The lists `xt_lbl` and `yt_lbl` are the labels at each tick. For instance,
sage: xt = [k * pi for k in range(-2,3)]
sage: xt_lbl = ['$-2\pi/w$', '$-\pi/w$', '$0$', '$\pi/w$', '$2\pi/w$']
sage: yt = [k / 2 for k in range(-2, 3)]
sage: yt_lbl = ['$-A$', '$-A/2$', '$0$', '$A/2$', '$A$']
sage: plot(sin(x),(x,-2*pi,2*pi), ticks=[xt, yt], tick_formatter = [xt_lbl, yt_lbl])
Launched png viewer for Graphics object consisting of 1 graphics primitive
creates the following plot:
![Plot of A sin(wx)](/upfiles/15089202075755107.png)
For more information on `ticks` and `tick_formatter`, please refer to [the documentation on 2D-plotting](http://doc.sagemath.org/html/en/reference/plotting/sage/plot/plot.html?highlight=tick) (search for "ticks" in the page) and the [options of `show()`](http://doc.sagemath.org/html/en/reference/plotting/sage/plot/graphics.html?highlight=tick#sage.plot.graphics.Graphics.show).
**Addendum.** You may want to automatize the process a bit more. If you accept to personalize a bit less the labels, this is possible: you can use SageMath's capabilities in the simplification of symbolic expressions, combined with the function `latex` which produces a LaTeX output from an expression. The following example should be self-explanatory.
sage: xt = [k * pi for k in range(-2, 3)]
sage: yt = [k/2 for k in range(-2, 3)]
sage: var('w,A')
(w, A)
sage: xt_lbl = ['$' + latex(t/w) + '$' for t in xt]
sage: yt_lbl = ['$' + latex(A*t) + '$' for t in yt]
sage: plot(sin(x),(x,-2*pi, 2*pi), ticks=[xt, yt], tick_formatter=[xt_lbl, yt_lbl])
Launched png viewer for Graphics object consisting of 1 graphics primitive
![New plot with more automatic tick labels](/upfiles/15091241184897046.png)Wed, 25 Oct 2017 10:36:36 +0200https://ask.sagemath.org/question/39260/is-it-possible-to-plot-with-symbols-for-x-and-y-ticks/?answer=39274#post-id-39274Comment by Bob for <p>If I correctly understand your question, you want to plot some <em>symbolic</em> function such as $x\mapsto A \sin(wx)$, where $A$ and $w$ are symbolic variables. This is not <em>per se</em> possible since plotting needs floating point values. The solution consists in plotting $x\mapsto \sin(x)$ and changing the labels on the axes. The command</p>
<pre><code>plot(sin(x), (x,-2*pi, 2*pi), ticks = [xt, yt], tick_formatter = [xt_lbl, yt_lbl])
</code></pre>
<p>plots the function $x\mapsto \sin(x)$, putting <em>ticks</em> on the x-axis at each location given in the list <code>xt</code>, and <em>ticks</em> on the y-axis using <code>yt</code>. The lists <code>xt_lbl</code> and <code>yt_lbl</code> are the labels at each tick. For instance,</p>
<pre><code>sage: xt = [k * pi for k in range(-2,3)]
sage: xt_lbl = ['$-2\pi/w$', '$-\pi/w$', '$0$', '$\pi/w$', '$2\pi/w$']
sage: yt = [k / 2 for k in range(-2, 3)]
sage: yt_lbl = ['$-A$', '$-A/2$', '$0$', '$A/2$', '$A$']
sage: plot(sin(x),(x,-2*pi,2*pi), ticks=[xt, yt], tick_formatter = [xt_lbl, yt_lbl])
Launched png viewer for Graphics object consisting of 1 graphics primitive
</code></pre>
<p>creates the following plot:</p>
<p><img alt="Plot of A sin(wx)" src="/upfiles/15089202075755107.png"></p>
<p>For more information on <code>ticks</code> and <code>tick_formatter</code>, please refer to <a href="http://doc.sagemath.org/html/en/reference/plotting/sage/plot/plot.html?highlight=tick">the documentation on 2D-plotting</a> (search for "ticks" in the page) and the <a href="http://doc.sagemath.org/html/en/reference/plotting/sage/plot/graphics.html?highlight=tick#sage.plot.graphics.Graphics.show">options of <code>show()</code></a>. </p>
<p><strong>Addendum.</strong> You may want to automatize the process a bit more. If you accept to personalize a bit less the labels, this is possible: you can use SageMath's capabilities in the simplification of symbolic expressions, combined with the function <code>latex</code> which produces a LaTeX output from an expression. The following example should be self-explanatory.</p>
<pre><code>sage: xt = [k * pi for k in range(-2, 3)]
sage: yt = [k/2 for k in range(-2, 3)]
sage: var('w,A')
(w, A)
sage: xt_lbl = ['$' + latex(t/w) + '$' for t in xt]
sage: yt_lbl = ['$' + latex(A*t) + '$' for t in yt]
sage: plot(sin(x),(x,-2*pi, 2*pi), ticks=[xt, yt], tick_formatter=[xt_lbl, yt_lbl])
Launched png viewer for Graphics object consisting of 1 graphics primitive
</code></pre>
<p><img alt="New plot with more automatic tick labels" src="/upfiles/15091241184897046.png"></p>
https://ask.sagemath.org/question/39260/is-it-possible-to-plot-with-symbols-for-x-and-y-ticks/?comment=39284#post-id-39284Thanks Bruno, this is exactly what I want to do. However the function I wish to plot is a bit more complicated than just sin(x) so I was hoping that there would be a way to get sage to figure out where to put the symbolic ticks.
But as you say, if plot requires floating point numbers, then sage is probably not capable of automagically doing the symbolic plotting I'm after.
I think it would be a neat capability to add though!Thu, 26 Oct 2017 07:37:02 +0200https://ask.sagemath.org/question/39260/is-it-possible-to-plot-with-symbols-for-x-and-y-ticks/?comment=39284#post-id-39284Comment by B r u n o for <p>If I correctly understand your question, you want to plot some <em>symbolic</em> function such as $x\mapsto A \sin(wx)$, where $A$ and $w$ are symbolic variables. This is not <em>per se</em> possible since plotting needs floating point values. The solution consists in plotting $x\mapsto \sin(x)$ and changing the labels on the axes. The command</p>
<pre><code>plot(sin(x), (x,-2*pi, 2*pi), ticks = [xt, yt], tick_formatter = [xt_lbl, yt_lbl])
</code></pre>
<p>plots the function $x\mapsto \sin(x)$, putting <em>ticks</em> on the x-axis at each location given in the list <code>xt</code>, and <em>ticks</em> on the y-axis using <code>yt</code>. The lists <code>xt_lbl</code> and <code>yt_lbl</code> are the labels at each tick. For instance,</p>
<pre><code>sage: xt = [k * pi for k in range(-2,3)]
sage: xt_lbl = ['$-2\pi/w$', '$-\pi/w$', '$0$', '$\pi/w$', '$2\pi/w$']
sage: yt = [k / 2 for k in range(-2, 3)]
sage: yt_lbl = ['$-A$', '$-A/2$', '$0$', '$A/2$', '$A$']
sage: plot(sin(x),(x,-2*pi,2*pi), ticks=[xt, yt], tick_formatter = [xt_lbl, yt_lbl])
Launched png viewer for Graphics object consisting of 1 graphics primitive
</code></pre>
<p>creates the following plot:</p>
<p><img alt="Plot of A sin(wx)" src="/upfiles/15089202075755107.png"></p>
<p>For more information on <code>ticks</code> and <code>tick_formatter</code>, please refer to <a href="http://doc.sagemath.org/html/en/reference/plotting/sage/plot/plot.html?highlight=tick">the documentation on 2D-plotting</a> (search for "ticks" in the page) and the <a href="http://doc.sagemath.org/html/en/reference/plotting/sage/plot/graphics.html?highlight=tick#sage.plot.graphics.Graphics.show">options of <code>show()</code></a>. </p>
<p><strong>Addendum.</strong> You may want to automatize the process a bit more. If you accept to personalize a bit less the labels, this is possible: you can use SageMath's capabilities in the simplification of symbolic expressions, combined with the function <code>latex</code> which produces a LaTeX output from an expression. The following example should be self-explanatory.</p>
<pre><code>sage: xt = [k * pi for k in range(-2, 3)]
sage: yt = [k/2 for k in range(-2, 3)]
sage: var('w,A')
(w, A)
sage: xt_lbl = ['$' + latex(t/w) + '$' for t in xt]
sage: yt_lbl = ['$' + latex(A*t) + '$' for t in yt]
sage: plot(sin(x),(x,-2*pi, 2*pi), ticks=[xt, yt], tick_formatter=[xt_lbl, yt_lbl])
Launched png viewer for Graphics object consisting of 1 graphics primitive
</code></pre>
<p><img alt="New plot with more automatic tick labels" src="/upfiles/15091241184897046.png"></p>
https://ask.sagemath.org/question/39260/is-it-possible-to-plot-with-symbols-for-x-and-y-ticks/?comment=39296#post-id-39296I added an addendum to my answer which may at least partially answer your question.Fri, 27 Oct 2017 19:10:00 +0200https://ask.sagemath.org/question/39260/is-it-possible-to-plot-with-symbols-for-x-and-y-ticks/?comment=39296#post-id-39296