Processing math: 100%
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 7 years ago

B r u n o gravatar image

If I correctly understand your question, you want to plot some symbolic function such as xAsin(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 xsin(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 xsin(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)

For more information on ticks and tick_formatter, please refer to the documentation on 2D-plotting (search for "ticks" in the page) and the options of show().

click to hide/show revision 2
No.2 Revision

If I correctly understand your question, you want to plot some symbolic function such as xAsin(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 xsin(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 xsin(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)

For more information on ticks and tick_formatter, please refer to the documentation on 2D-plotting (search for "ticks" in the page) and the options of 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