manpagez: man pages & more
info mathgl
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

2.2.2 Axis and grids

MathGL library can draw not only the bounding box but also the axes, grids, labels and so on. The limits of axes and their origin (the point of intersection) are determined by function Axis(). Also you can use XRange(), YRange(), ZRange() functions (see section Ranges (bounding box)). Ticks on axis are specified by function SetTicks (see section Ticks). First argument the direction for each change will be applied. Second argument gives the step between ticks (if positive) or gives the number of ticks on the axis (if negative) or set to use logarithmic ticks (if zero). Third argument gives numbers of sub-ticks between ticks (default is zero). Last argument define the initial ticks position.

Function Axis draws axes. Its textual string shows in which directions the axis or axes will be drawn (by default "xyz", function draws axes in all directions). Function Grid draws grid perpendicularly to specified directions. Example of axes and grid drawing is:

    int sample(mglGraph *gr, void *)
    {
        gr->SubPlot(2,2,0);
        gr->SetTicks('x', 0.4, 3);  // sets tick step to 0.5
        gr->SetTicks('y', 0.4, 3);  // and draws 3 subticks
        gr->Box();                  // should be after the ticks change
        gr->Axis("xy");
        gr->Grid();
        gr->Puts(mglPoint(0,1.3,1),"Axis and grid");

        gr->SetTicks('x');  gr->SetTicks('y'); // restore back
        gr->Axis(mglPoint(-1,-1,-1),mglPoint(1,1,1),mglPoint(0,0,0));

        gr->SubPlot(2,2,1);
        gr->Rotate(60,40);
        gr->Axis();
        gr->Label('x',"x");
        gr->Label('y',"y");
        gr->Label('z',"z");
        gr->Puts(mglPoint(0,0,1.5),"Axis and labels");

        gr->SubPlot(2,2,2);
        gr->Rotate(60,40);
        gr->SetTicks('x', 0.2); gr->SetTicks('y', 0.2);
        gr->SetTicks('z', 0.2); // too low step of ticks
        gr->Axis(mglPoint(-1,-1,-1),mglPoint(1,1,1),mglPoint(-1,-1,-1));
        gr->Axis();
        gr->Grid();
        gr->Puts(mglPoint(0,0,1.5),"Shift origin and add grid");
        gr->Puts(mglPoint(0,0,1.2),"(note, too many ticks)");

        gr->SubPlot(2,2,3);
        gr->Rotate(60,40);
        gr->SetTicks('x', -6);  // decrease the number of ticks
        gr->SetTicks('y', -6);
        gr->Axis("yz");
        gr->Label('y',"Y axis",0);
        gr->Label('z',"Z axis",0);
        gr->Puts(mglPoint(0,0,1.5),"Remove X axis, and");
        gr->Puts(mglPoint(0,0,1.2),"decrease number of ticks");
        return 0;
    }

This example shows the importance of the correct choosing of the number of ticks on axis. If tick step is too small then its text may overlap and becomes unreadable. This code has the example of Label function. It draws label for axis in specified direction. The text position on axis is specified by third argument of Label function. If it is positive then then text is drawn near the axis maximum, if negative then the same takes place near the minimum of axis, if zero - then at the center of axis.

../png/sample2

Example of setting up axis range and axis ticks.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© manpagez.com 2000-2024
Individual documents may contain additional copyright information.