Plot Basic tutorial: Difference between revisions

From FreeCAD Documentation
(Rewording...)
(Rewording...)
Line 64: Line 64:


<!--T:8-->
<!--T:8-->
You can start working here due to plot command will start a new document, but all plot commands that you execute will append series to created plot until you don't create a new document, so usually is better options control the opened plot documents. First thing that we need to do is create the data for sine and cosine functions that we want to plot:
You can also start working from here because, as already explained, the plot command will create a new document if required. But it is usually better to create a plot document first. The next thing we need to do is create the data for sine and cosine functions that we want to plot:


</translate>
</translate>
Line 83: Line 83:


<!--T:10-->
<!--T:10-->
In order to plot both function we only need to launch next commands:
In order to plot both function we only need to launch the next commands:


</translate>
</translate>
Line 93: Line 93:


<!--T:11-->
<!--T:11-->
That will plot our functions. '''plot''' command allows the series label as argument, but since we will edit it later using Plot module tools we don't pass this data yet.
That will plot our functions. The '''plot''' command allows the series label as an argument, but since we will edit it later using the Plot module tools we don't pass this data yet.


==Configuring plot== <!--T:12-->
==Configuring plot== <!--T:12-->
Line 100: Line 100:


<!--T:13-->
<!--T:13-->
Change FreeCAD workbench to [[Plot_Module|Plot module]] in View/Workbench menu (you must install the add-on first with the [[Std_AddonMgr|Add-on manager]]). When module has been loaded use grid tool in order to show it.
Change the FreeCAD workbench to the [[Plot_Module|Plot module]] with {{MenuCommand|ViewWorkbench → }} (you must install the add-on first with the [[Std_AddonMgr|Add-on manager]]). When the module has been loaded, use the [[Plot_Grid|grid tool]] to show the grid.


</translate>
</translate>
Line 109: Line 109:


<!--T:15-->
<!--T:15-->
You can repeat the action in order to hide it. Also you can show the legend with the tool provided.
You can repeat the action to hide the grid. Use the [[Plot_Legend|legend tool]] to show, or hide, the legend.


</translate>
</translate>
Line 118: Line 118:


<!--T:17-->
<!--T:17-->
As you can see, legend is empty because we have not set any series label yet. In [[Plot_Module|Plot module]] series without label are not represented at legend, in order to allow you to draw auxiliary lines.
As you can see, the legend is empty because we have not set any series label yet. In the [[Plot_Module|Plot module]] series without a label are not represented in the legend, in order to allow you to draw auxiliary lines.


===Setting series labels===
===Setting series labels===


<!--T:18-->
<!--T:18-->
With the series tool you can edit some series parameters.
With the [[Plot_Series|series tool]] you can edit some series parameters.


</translate>
</translate>
Line 132: Line 132:


<!--T:20-->
<!--T:20-->
First for all select the line that you want to edit, for example we will start with the first one. Uncheck '''No label''' and set this label:
Select the line you want to edit, we will start with the first one. Uncheck '''No label''' and set this label:


</translate>
</translate>
Line 141: Line 141:


<!--T:21-->
<!--T:21-->
Since [http://matplotlib.org/ matplotlib] supports [http://www.latex-project.org LaTeX] you can set all the labels or titles that you want using it. Set the following label to second serie:
Since [http://matplotlib.org/ matplotlib] supports [http://www.latex-project.org LaTeX] you can set all labels and titles using LaTeX. Set the following label for the second series:


</translate>
</translate>
Line 152: Line 152:


<!--T:22-->
<!--T:22-->
Series allows you to set a lot of series properties. Try to set the properties shown at the example image, changing series colors and drawing style of the second one.
Series allow you to set many series properties. Try to set the properties shown in the example image, changing the colors and drawing style of the second series.


===Setting axes labels===
===Setting axes labels===


<!--T:23-->
<!--T:23-->
With the labels tool you can set labels associated to all created axes.
With the [[Plot_Labels|labels tool]] you can set labels for all created axes.


</translate>
</translate>
Line 177: Line 177:


<!--T:27-->
<!--T:27-->
With saving plot tool you can save your plot as image file in several formats.
With the [[Plot_Save|save tool]] you can save your plot as an image file in several formats.


</translate>
</translate>

Revision as of 09:08, 5 September 2021

Tutorial
Topic
Plot Workbench Basic Tutorial
Level
Beginner
Time to complete
Authors
FreeCAD version
Example files
See also
None

In this tutorial we will learn how to create a basic plot using the Plot Workbench and the Python console.

Basic plot example

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

Plotting data

To plot data you don't need to create a FreeCAD document, simply open the Python console and start sending commands, or use macros.

Creating plot document

Plots are special documents that can be created manually in order to add data later, or the module can create one automatically when you start plotting data. Creating your own plot document has two advantages:

  • You can set the document window label.
  • You can control the document where you plot your data.

To create a new plot document simply launch the following commands:

try:
    from FreeCAD.Plot import Plot
except ImportError:
    from freecad.plot import Plot
Plot.figure("TrigonometricTest")

In FreeCAD 0.19 it is required to install the Plot Workbench with the Add-on manager, while from FreeCAD 0.20 onward the external add-on is no longer required to perform plots. The commands above will create a new tab in the Main view area called TrigonometricTest. The newly created document already has a set of axes. Each plot document has at least one set of axes that can be removed without fully using matplotlib control.

Drawing functions

You can also start working from here because, as already explained, the plot command will create a new document if required. But it is usually better to create a plot document first. The next thing we need to do is create the data for sine and cosine functions that we want to plot:

import math
t = range(0,101)
t = [tt/100.0 for tt in t]
s = [math.sin(2.0*math.pi*tt) for tt in t]
c = [math.cos(2.0*math.pi*tt) for tt in t]

That will create 3 arrays of data (with 101 points):

  • t = Time in seconds.
  • s = Sine function.
  • c = Cosine function.

In order to plot both function we only need to launch the next commands:

Plot.plot(t,s)
Plot.plot(t,c)

That will plot our functions. The plot command allows the series label as an argument, but since we will edit it later using the Plot module tools we don't pass this data yet.

Configuring plot

Showing grid and legend

Change the FreeCAD workbench to the Plot module with View → Workbench → (you must install the add-on first with the Add-on manager). When the module has been loaded, use the grid tool to show the grid.

Show/hide grid tool icon

You can repeat the action to hide the grid. Use the legend tool to show, or hide, the legend.

Show/hide legend tool icon

As you can see, the legend is empty because we have not set any series label yet. In the Plot module series without a label are not represented in the legend, in order to allow you to draw auxiliary lines.

Setting series labels

With the series tool you can edit some series parameters.

Series configuration tool icon

Select the line you want to edit, we will start with the first one. Uncheck No label and set this label:

$y = \sin \left( 2 \pi t \right)$

Since matplotlib supports LaTeX you can set all labels and titles using LaTeX. Set the following label for the second series:

$y = \cos \left( 2 \pi t \right)$

Setting series style

Series allow you to set many series properties. Try to set the properties shown in the example image, changing the colors and drawing style of the second series.

Setting axes labels

With the labels tool you can set labels for all created axes.

Labels tool icon

Set this data:

  • Title = Trigonometric functions example
  • X Label = $t$
  • Y Label = $y = \mathrm{f} \left( t \right)$

Also change the size of all of them to 20.

Saving plot

With the save tool you can save your plot as an image file in several formats.

Save tool icon

First for all select the path of the output file. You can use file selection dialog using the button at right of the path edition line.

You can set the output image size in inches, for example we can set 11.7x8.3 that is a DIN A4 paper size. DPI (Dots per inch) will control the image resolution, for example using 100 dpi you will get an image of 1170x830 pixels.


Template:Tutorials navi