Quite often, when plotting an expression in involving trigonometric functions applied to (a rational polynomial of) the main variable, it is desirable to have the major ticks along the x-axis be labeled by multiples of Pi. In particular, it can be much more appealing to have those tickmarks be labeled with short rationals multiplied by the 2D Math symbol π.

 

This is a rough experiment in automating (or "smartly" deducing) x-axis tickmarks for a 2D plot based upon multiples of an exact symbol such as π (Pi).

 

The basic idea is that the current functionality offered by plot's tickmarks option is going to end up being used in a repetitive way. Why keep having to enter options like xtickmarks=[seq(...*Pi=...*Pi, i=...)] or xtickmarks=[spacing(Pi/4),default] over and over? Why can't the system infer such tickmarks by default (allowing a simple option to disable it forcefully, on demand)?

 

It's not going to be easy to make most people happy most of the time. Overriding options are still necessary. The spacing suboption of tickmarks is nice. But there seem to be other use cases where it's not adequate or succinct enough. One of my points is that automatic insertion of multiples of 2D symbols could be made the default behaviour. But right now it must be requested explicitly.

 

Perhaps the topic is not so difficult that plot, smartplot or the PlotBuilder could not deduce & populate some of the it automatically, or at least offer the functionality on demand more succinctly than at present.

 

restart:

 

The source code below is undocumented. It has little validation or checking of options, with no attempt at being bug-free. It's an illustrative experiment in what sorts of functionality are possible.

 

Most of the hard-coded use of Pi in the actual code is to handle the (dubious?) case of a range involving powers of Pi. See the very last example. Without that functionality, it would be easy enough to get this to work better for other exact symbols like or


If it's this easy to put together something rough but flexible in 20 lines of code then imagine what's possible with a real effort. The system could be made to detect the presence of trig functions applied inside the expression to rational polynomials of the main dummy plotting variable. That's just one possible trigger for automatic detection. The crude trigger that I'm using below is multiples of the symbol as the end-points of the plotting range argument.

 

p:=proc(expr,rng,{numxticks::nonnegint:=5},
        {niceticks::truefalse:=false},
        {xtickmultiples::{realcons,identical(':-DEFAULT')}:=:-DEFAULT})

local x,a,b,dega,degb,degpi,acon,bcon,inc;

  x := lhs(rng);

  a,b := op(rhs(rng));

  dega,degb:=degree(a,Pi),degree(b,Pi);

  if dega=degb and dega>0 then

    acon,bcon:=a/Pi^dega,b/Pi^dega;

    inc:=(bcon-acon)/(numxticks-1);

    plot(expr,rng,

         xtickmarks=
           `if`(xtickmultiples<>':-DEFAULT',
               [seq((ceil(acon)+i*xtickmultiples/Pi)*Pi^dega=
                    (ceil(acon)+i*xtickmultiples/Pi)*Pi^dega,

                    i=0..floor((bcon-acon)/(xtickmultiples/Pi)))],

             `if`(niceticks,

                [seq((ceil(acon)+i*convert(evalf[2](inc),rational))*Pi^dega=

                     (ceil(acon)+i*convert(evalf[2](inc),rational))*Pi^dega,

                     i=0..(numxticks-1))],

                [seq((acon+i*inc)*Pi^dega=(acon+i*inc)*Pi^dega,

                     i=0..(numxticks-1))])));

  else

    plot(expr,rng);

  end if;

end proc:

 

The plots below are mostly in pairs: one using the regular `plot` command, and another showing an alternative. Most of these examples use Pi as the tickmark symbol. But with a few changes it could be made to work with  or other exact quantities, if the `xtickmultiples` option were used.

 

Usual tickmarks, but using a 2D symbol for Pi. This is likely the most common case. It's should be easy for an application as smart as Maple to deduce the nice tickmarks here.

plot(sin(x-1), x=-2*Pi .. 3*Pi);
p(sin(x-1), x=-2*Pi .. 3*Pi);

 

 

As above, but forcing the number of ticks

p(sin(x-1), x=-2.1*Pi .. 2.9*Pi, numxticks=3);

 

As above, but letting it choose "nice" rational multiples of Pi (this could be made stronger).

p(sin(x-1), x=-2.1*Pi .. 2.9*Pi, niceticks=true);

 

As above, but forcing the spacing as a multiple of Pi/2 (more succint than using 'spacing' alongside 'tickmarks')

p(sin(x-1), x=-2.1*Pi .. 2.9*Pi, xtickmultiples=Pi/2);

 

Semi-forced number of ticks, also choosing nice rational multiples of Pi (but more straightforward that forming tickmarks=[seq(..*Pi=..*Pi,..)] which people don't always come up with on their own).

plot(sin(x-1), x=-2.7*sqrt(2)*Pi .. 2.9*sqrt(2)*Pi);
p(sin(x-1), x=-2.7*sqrt(2)*Pi .. 2.9*sqrt(2)*Pi, niceticks=true, numxticks=6);

 

 

Usual tickmarks, but using powers of symbol Pi

plot(sin(sqrt(x)-1), x=2*Pi^2 .. 18*Pi^2);
p(sin(sqrt(x)-1), x=2*Pi^2 .. 18*Pi^2);

 

 

Download nicetickmarks.mw

acer


Please Wait...