Plot MultiAxes tutorial: Difference between revisions

From FreeCAD Documentation
(Wording...)
Line 51: Line 51:


<!--T:6-->
<!--T:6-->
As ''x'' moves from 0 to 2, the ''y'' function has a maximum value of 4, so if we try to plot this function with the trigonometrical ones, at least one function will be truncated or bad scaled, therefore we we need a multiaxes plot. A multiaxes plot in FreeCAD is intended to get a plot with multiple axes, not to get multiple plots in the same document.
As ''x'' moves from 0 to 2, the ''y'' function has a maximum value of 4, so if we try to plot this function with the trigonometrical ones, at least one function will be truncated or badly scaled, therefore we we need a multiaxes plot. A multiaxes plot in FreeCAD is intended to get a plot with multiple axes, not to get multiple plots in the same document.


===Drawing functions, adding new axes===
===Drawing functions, adding new axes===

Revision as of 09:23, 8 September 2021

Tutorial
Topic
Plot workbench
Level
Intermediate
Time to complete
Authors
FreeCAD version
0.19
Example files
See also
None

Please complete the basic tutorial before starting with this tutorial. In this tutorial we will learn how to create and edit a multiaxes plot. You can learn more about the Plot module here.

Multiaxes plot example

In the image you can see the result that we will approximately obtain. Following this tutorial you will learn:

  • How to create a multiaxes Plot from the Python console.
  • How to edit axes properties.
  • How to control the grid and the legend when several axes sets are present.
  • How to edit the position of labels, titles and legends.

Plotting data

As we did in the previous tutorial we will use the Python console or macros to plot the data, but in this case we will plot the data using two axes sets.

Creating plot data

In this example we will plot 3 functions, the two used in the previous tutorial, and a new polynomial one. The range of the polynomial function is different from the other functions therefore new axes are required. The next commands will create the data arrays for us:

import math
p = range(0,1001)
x = [2.0*xx/1000.0 for xx in p]
y = [xx**2.0 for xx in x]
t = [tt/1000.0 for tt in p]
s = [math.sin(math.pi*2.0*tt) for tt in t]
c = [math.cos(math.pi*2.0*tt) for tt in t]

As x moves from 0 to 2, the y function has a maximum value of 4, so if we try to plot this function with the trigonometrical ones, at least one function will be truncated or badly scaled, therefore we we need a multiaxes plot. A multiaxes plot in FreeCAD is intended to get a plot with multiple axes, not to get multiple plots in the same document.

Drawing functions, adding new axes

We will plot the polynomial function using the main axes. If all your axes have the same size it is not relevant which function is plotted first, but if this is not the case the function that uses the main, the biggest, axes must be plotted first (to ensure they are plotted on the white background). In order to do this we only need to launch a command.

try:
    from FreeCAD.Plot import Plot
except ImportError:
    from freecad.plot import Plot
Plot.plot(x,y,r"$x^2$")

In this example we pass the series label for the legend directly. Note that the label string has the r prefix in order to prevent Python from trying to interpret special characters (the \ symbol is used frequently in LaTeX syntax).

Before we can plot the trigonometrical functions, we need to create new axes. In the Plot module new axes are automatically selected as the active ones, and new plots will be associated with these axes.

Plot.addNewAxes()
Plot.plot(t,s,r"$\sin\left( 2 \pi t \right)$")
Plot.plot(t,c,r"$\cos\left( 2 \pi t \right)$")

As you can see your plot has gone crazy, with axes ticks overlapping, curves of the same color, etc. Now we need to use the Plot module to fix this graph.

Configuring plot

Configuring axes

Plot module provides a tool to modify the properties of axes.

Axes configuration tool icon

With the axes tool you can add or remove axes, and set the active axes, which are then used if you to plot more data. For the moment we will work with the selected axes, the last axes, which are associated with the trigonometrical functions.

We will move the horizontal and vertical dimension sliders to reduce the size of the axes (try to emulate example). Then we can set the axes alignment, changing it to top and right, and apply a small offset of two units.

Configuring series

Set the series properties as we did in the previous tutorial.

Showing grid and legend

The grid and legend can be shown, and hidden, with the tools already described in the previous tutorial, but in this case the behavior is a little different because there are two axes sets.

Grid lines are added to the active axes set. To add lines to the main axes set in our example, it has to be activated first by changing the active axes from 1 to 0 in the axes tool.

The legend will be the same for both axes sets, so you can choose either set, but it is strongly recommended to use the main, the biggest, axes set (0 in our example) because the legend will be positioned relative to these axes. If you show the legend now you will see that it is really badly placed, but we will fix that later.

Setting axes labels

When it comes to setting the axes labels we again have to deal with our two axes sets. But since axes labels are usually set per axis, the procedure is the same as described in the previous tutorial. The Plot module allows you to set a title per axes set. In this case we only want to set a title for the main axes.

Axes 0:

  • Title = Multiaxes example
  • X Label = $x$
  • Y Label = $\mathrm{f} \left( x \right)$

Axes 1:

  • X Label = $t$
  • Y Label = $\mathrm{f} \left( t \right)$

Change the font size of all labels to 20, and the font size of the title to 24. Again there is an element, the title, that is badly placed.

Setting elements position

The Plot module provides a tool to change the position of several plot elements, such as as titles, labels and legends.

Position editor icon

When you run the tool you will see a list of all editable elements. Titles and legends can be moved in both directions, but axis labels can only be moved along the axis they belong to. Select the title of axes 0 and move it to (0.24,1.01), then select the legend and move it to a better position. You can increase font size of the legend labels as well.

Saving plot

Now you can save your work. See the previous tutorial if you don't remember how to do it.


Template:Tutorials navi