Skip to main content
Logo image

Section 3.3 Grid and Axes

We’ll begin our discussion of graphical elements by looking at grids and axes, which will typically be in the background of a diagram. Since these are commonly used elements, there are many features that can be customized by an author.

Subsection 3.3.1 <grid-axes>

The simplest way to include a grid and axes in a diagram is with the <grid-axes> element, which is demonstrated in Figure 3.3.1. This element is convenient way to add both a <grid> and <axes>, which are both described below. The attributes given to a <grid-axes> element will be passed on the <grid> and <axes>.
Figure 3.3.1. The use of a <grid-axes> element.
<diagram dimensions="(300,300)" margins="5">
  <coordinates bbox="(-2,-2,5,5)">
    <grid-axes xlabel="x" ylabel="y"/>
  </coordinates>
</diagram>
Listing 3.3.2. The PreFigure source for Figure 3.3.1.
Notice that PreFigure makes choices about the spacing of the grid lines and the locations at which labels are added to the axes. All of these features can be modified as will be described in the next two subsections, Subsection 3.3.2 and Subsection 3.3.3.
To place the grid lines and label locations at multiples of \(\pi\text{,}\) add the attributes @h-pi-format="yes" or @v-pi-format="yes", as illustrated in Figure 3.3.3. These attributes may be individually applied to a <grid> or <axes> element.
Figure 3.3.3. The use of @h-pi-format and @v-pi-format attributes.
<diagram dimensions="(300,250)" margins="(30,5,5,5)">
  <coordinates bbox="(-pi, -pi, 10*pi, pi)">
    <definition>f(x)=2*cos(x/4-1)</definition>
    <grid-axes h-pi-format="yes"
               v-pi-format="yes"/>
    <graph function="f"/>
  </coordinates>
</diagram>
Listing 3.3.4. The PreFigure source for Figure 3.3.3.
The @xlabel and @ylabel tags are optional but provide a simple way to label the axes. We will have a lot more to say about labels later, but these particular labels will be processed by MathJax as if they were contained in an @m element.
If a diagram has a lot of graphical components, you may want the axes to appear on the outside of the diagram rather than where \(y=0\) and \(x=0\text{.}\) You can use the @h-frame and @v-frame attributes to do this, as illustrated in Figure 3.3.5. The possible values @h-frame="bottom","top" and @v-frame="left","right".
Figure 3.3.5. The axes may be drawn on the outside of the diagram with the attributes @h-frame and @v-frame.
<diagram dimensions="(300,250)" margins="(35,30,50,30)">
  <coordinates bbox="(-4, -4, 4, 4)">
    <grid-axes h-frame="bottom"
               v-frame="left"/>
    <axes decorations="no">
      <xlabel>Time <m>t</m></xlabel>
      <ylabel alignment="ne">Temperature <m>F</m></ylabel>
    </axes>
  </coordinates>
</diagram>
Listing 3.3.6. The PreFigure source for Figure 3.3.5.
We will also discuss annotations in more depth later, but it is worth mentioning now that a <grid-axes> element will automatically annotate itself as the first component in the annotation tree.

Subsection 3.3.2 Grids

The <grid> element provides a grid without adding axes. Without any attributes, this element will choose horizontal and vertical spacings and draw the grid lines @stroke="lightgray" and @thickness="1". The stroke attributes can be modified as can the spacings using the @spacings attribute. The value of the @spacings consists of two lists, one for each of the horizontal and vertical grid lines, with each list having the form (start, space, end). Figure 3.3.7 provides an example.
Figure 3.3.7. The attributes of a <grid> element.
<diagram dimensions="(300,300)" margins="5">
  <coordinates bbox="(0,0,10,10)">
    <grid spacings="((0,1,10),(0,1/2,10))" stroke="blue"
	  thickness="2"/>
  </coordinates>
</diagram>
Listing 3.3.8. The PreFigure source for Figure 3.3.7.
Including a @basis attribute will produce a grid drawn in the given basis, as shown in Figure 3.3.9.
Figure 3.3.9. The result of @basis="((2,1),(1,2))".
When included in a <grid-axes> element, these attributes will be inherited by the resulting grid.

Subsection 3.3.3 Axes

Axes are a bit more involved since there is a wider range of properties they possess. For instance, an <axes> tag with no attributes will produce axes similar to those seen in Figure 3.3.10. In addition to the axes, we see that there are tick marks and labels as well. The positions of the tick marks and labels are automatically generated.
Figure 3.3.10. An <axes> element without attributes.
There are attributes that may be used to change this default behavior.
decorations
Setting @decorations="no" suppresses the automatic inclusion of tick marks and labels and produces unadorned axes. Regardless of the value of this attribute, features may be customized and added to the axes by including some of the following attributes.
xlabel and ylabel
As we have seen @xlabel="t" and @ylabel="f(t)" will place labels on the horizontal and vertical axes. These labels will be automatically wrapped in <m> elements and processed as by MathJax.
hticks and vticks
These attributes specify the position of tick marks on the axes. For instance, @hticks="(-2,2,4)" will place tick marks on the horizontal axis beginning at -2 and ending at 4 with a spacing of two units.
hlabels and vlabels
These attributes are similar to @hticks and @vticks except they specify the position of labels on the axes.
arrows
Arrows may be placed on ends of the axes by setting the @arrows attribute. If @arrows="1", then arrows will be included in the direction of increasing coordinates. @arrows="2" includes arrows at each end of the axes.
Figure 3.3.11 illustrates the use of some of these attributes.
Figure 3.3.11. The effect of some attributes of an <axes> element.
<diagram dimensions="(300,300)" margins="5">
  <coordinates bbox="(-2,-2,5,5)">
    <axes xlabel="\alpha" ylabel="g(\alpha)"
	  hticks="(-2,1,5)" hlabels="(-2,1,5)"
	  arrows="1"/>
  </coordinates>
</diagram>
Listing 3.3.12. The PreFigure source for Figure 3.3.11.
As with grids, these attributes may be included in a <grid-axes> element and inherited by the resulting axes.
The @xlabel and @ylabel attributes are included so that simple labels may be added to the axes with ease. More complicated labels can be added with <xlabel> and <ylabel> elements inside of an <axes> or <grid-axes> element. By default, these will be place to the right of the horizontal axis and directly above the vertical axis, as seen in Figure 3.3.13. This behavior can be modified using the usual <label> attributes described in Section 3.4.
Figure 3.3.13. Adding labels to an <axes> element.
<diagram dimensions="(300,250)" margins="(35,30,50,30)">
  <coordinates bbox="(-4, -4, 4, 4)">
    <axes decorations="no">
      <xlabel>Time <m>t</m></xlabel>
      <ylabel alignment="ne">Temperature <m>F</m></ylabel>
    </axes>
  </coordinates>
</diagram>
Listing 3.3.14. The PreFigure source for Figure 3.3.13.