Back to main page
Back to draw contents

Plotting functions in 2d

This section shows how to draw functions in 2d.


Function draw2d describes a scene in two dimensions. In this case, an explicit function in the screen terminal.

draw2d(explicit(u^2,u,-3,3));

Another explicit function. In this case, axis are drawn with different styles.

draw2d(explicit(x^3,x,-1,1),
       xaxis       = true,
       xaxis_color = blue,
       yaxis       = true,
       yaxis_width = 2,
       yaxis_type  = solid,
       yaxis_color = "#f3b507");

With the secondary y-axis, it is possible to plot functions with respect to two scales of ordinates. The secondary y-axis is drawn on the right. All 2d objects are sensible to local graphics option yaxis_secondary. Other possible options related to the secondary y-axis are: yrange_secondary, ytics_secondary, ytics_axis_secondary and ytics_rotate_secondary.

draw2d(
   transparent = true,
   explicit(sin(x),x,0,10),
   rectangle([1,-1/2],[4,1/2]),
   yaxis_secondary = true,
   ytics_secondary = true,
   color = blue,
   explicit(100*sin(x+0.3)+2,x,0,10),
   rectangle([5,-30],[8,60]),
   terminal = wxt) $

The secondary x-axis is drawn on top of the scene. All 2d objects are sensible to local graphics option xaxis_secondary. Other possible options related to the secondary x-axis are: xrange_secondary, xtics_secondary, xtics_axis_secondary and xtics_rotate_secondary.

draw2d(
   /* activate both secondary axes */
   xtics_secondary = auto,
   ytics_secondary = auto,

   /* red curve is plotted against secondary x-axis and primary y-axis */
   xaxis_secondary = true,
   color = red,
   key = "top x, left y",
   explicit(sin(x),x,0,20),

   /* blue curve is plotted against primary x-axis and secondary y-axis */
   xaxis_secondary = false,
   yaxis_secondary = true,
   color = blue,
   key = "bottom x, right y",
   explicit(100*sin(x+0.3)+2,x,0,10),
   terminal = wxt) $

Piecewise function. This time, the result is saved in a png file. Options terminal and title, since they are global, can be written in any place.

draw2d(color = green,
           explicit(u^2,u,-2,2), 
           explicit(sin(z),z,2,6),
       terminal  = png, 
       title     = "My 1st title");

Two different explicit functions. We want the result in an eps file.

draw2d(terminal   = eps_color,
       key        = "Exponential func",
       color      = blue,
       line_width = 4,
            explicit(exp(x),x,-1,3),
       line_width = 2,
       color      = "#00ff00",   /* green, in hexadecimal */
       key        = "Cubic poly",
            explicit(%pi*x^3+sqrt(2)*x^2+10,x,0,3),
       xlabel     = "Horizontal axis",
       ylabel     = "Vertical axis");

A rational function and its asymptotes.

draw2d(/* the rational function */
       grid   = true,
       key    = "y = x^2/(x-2)",
       yrange = [-10,20],
       color  = red,
         explicit(x^2/(x-2),x,-9,15),

       /* asymptotes */
       key       = "",
       line_type = dots,
       color     = blue,
         explicit(x+2,x,-9,15),
       nticks = 70,
         parametric(2,t,t,-10,20),

       /* labels and arrows */
       head_length = 0.3,
       color     = black,
       line_type   = solid,
           vector([5.35,2.45],[-1.53,3.25]),
           vector([-1,7.5],[3,0]),
       label_alignment = left,
           label(["y = x+2",6,2.5]),
       label_alignment = right,
           label(["x = 2",-1.7,7.5])    );

Logarithmic scales.

draw2d(logy=true,
       xlabel = "x",
       ylabel = "log(y)",
       color  = red,
         explicit(exp(x),x,0.1,20),
       axis_top   = false,
       axis_right = false,
       grid       = true,
       title      = "Logarithmic scale",
       terminal   = png);

The following examples show how to place tics on axis.

draw2d(
  xlabel = "Default tics",
  ylabel = "No tics",
  ytics  = 'none,
  explicit(x^3,x,-1,1)  );
draw2d(
  xlabel = "Start-increment-end",
  ylabel = "Tics intervals 0.25",
  xtics  = [-3/4,1/8,3/4],
  ytics  = 1/4,
  explicit(x^3,x,-1,1)  );
draw2d(
  xlabel     = "User selected tics on axis",
  ylabel     = "Rotated labeled tics",
  xtics      = {-1/2,-1/4,3/4}, /* set of numbers */
  xtics_axis = true,            /* plot tics on x-axis */
  ytics      = {["High",0.75],["Medium",0],["Low",-0.75]},
  ytics_rotate = true,
  grid       = true,
  explicit(x^3,x,-1,1)  );

Filled functions. By default, graphics option filled_func is set to false. When it is set to true, the region bounded by the function and the bottom of the graphics region is filled with color fill_color.

load(distrib)$
draw2d(terminal    = eps_color,
       title       = "Normal probability",
       grid        = true,
       axis_top    = false,
       axis_right  = false,
       filled_func = true,
       fill_color  = "light-blue",
       key         = "Pr(-1 < X < 0)",
            explicit(pdf_normal(x,0,1),x,-1,0),
       key         = "Pr(1 < X <2)",
       fill_color  = "dark-blue",
            explicit(pdf_normal(x,0,1),x,1,2),
       filled_func = false,
       color       = red,
       key         = "Normal density N(0,1)",
         explicit(pdf_normal(x,0,1),x,-3,3)  );

It is also possible to fill the region bounded by two explicit functions. You only need to set graphics option filled_func to the expression of the second function. Take into account that the variable used in this expression must be the same used in the explicit object. In this example, note that when you call explicit the second time, filled_func= sin(x) is still active.

draw2d(fill_color  = grey,
       filled_func = sin(x),
       explicit(-sin(x),x,0,%pi),
       fill_color  = cyan,
       explicit(-sin(x),x,%pi,2*%pi));

A more elaborated example.

f1: 2*x^2-5*x+3$
f2: -8*x^2-x+30$

[x1,x2]: map('rhs, solve(f1=f2));

draw2d(title       = "Region bounded by two functions",
       fill_color  = grey,
       filled_func = f2,
       explicit(f1,x,x1,x2),
       filled_func = false,
       xaxis       = true,
       xtics_axis  = true,
       yaxis       = true,
       line_width  = 2,
       key         = string(f1),
       color       = red,
       explicit(f1,x,-3,3),
       key         = string(f2),
       color       = blue,
       explicit(f2,x,-3,3) );

With object bars you can create bars plots and histograms. Arguments are Maxima lists of length three, declaring: the central position on the x-axis, height (positive or negative) and bar width.

draw2d(bars([1,5,0.2],[2,7,0.2],[3,-4,0.1],[4,-2,1],[5,3,1]),
       xaxis = true,
       xtics = {["Ford",1],["Opel",2],["Citroen",3],["Toyota",4],["Teletransportation",5]} );

With options fill_color, fill_density and line_width, you can control how to plot bars.

draw2d(key          = "Group A",
       fill_color   = blue,
       fill_density = 0.2,
       bars([0.8,5,0.4],[1.8,7,0.4],[2.8,-4,0.4]),
       key          = "Group B",
       fill_color   = red,
       fill_density = 0.6,
       line_width   = 4,
       bars([1.2,4,0.4],[2.2,-2,0.4],[3.2,5,0.4]),
       xaxis = true);

Back to main page
Back to draw contents


by Mario Rodríguez Riotorto