Plot Basic tutorial: Difference between revisions

From FreeCAD Documentation
No edit summary
(Wiki code and captions.)
Line 1: Line 1:
<languages/>
<languages/>
<translate>
<translate>

<!--T:32-->
<!--T:32-->
{{TutorialInfo
{{TutorialInfo
Line 14: Line 15:
In this tutorial we will learn how to perform a basic plot using [[Plot Workbench]] and [[Python console]].
In this tutorial we will learn how to perform a basic plot using [[Plot Workbench]] and [[Python console]].


</translate>
[[Image:Plot_Trigonometric_Example.png|600px]]
<translate>
<!--T:2-->
<!--T:2-->
[[Image:Plot_Trigonometric_Example.png|600px|center|Basic plot example]]
{{Caption|Basic plot example}}
<center><span style="font-variant:small-caps">Basic plot example.</span></center>


<!--T:3-->
<!--T:3-->
Line 29: Line 32:
* How to save your plot.
* How to save your plot.


==Plotting data== <!--T:4-->
==Plotting data==

<!--T:4-->
In order to plot data you don't need to create a new FreeCAD document, simply show the Python console and start sending commands, or use [[Macros|macros]].
In order to plot data you don't need to create a new FreeCAD document, simply show the Python console and start sending commands, or use [[Macros|macros]].


===Creating plot document=== <!--T:5-->
===Creating plot document===

<!--T:5-->
Plots are special documents that can be created manually in order to add data later, or allow the module creates one automatically when you start plotting data. Create your own plot documents have 2 advantages:
Plots are special documents that can be created manually in order to add data later, or allow the module creates one automatically when you start plotting data. Create your own plot documents have 2 advantages:
* You can set the document window label.
* You can set the document window label.
Line 39: Line 46:
<!--T:6-->
<!--T:6-->
In order to create new plot document simply launch following commands:
In order to create new plot document simply launch following commands:
</translate>


</translate>
{{Code|code=
{{Code|code=
try:
try:
Line 48: Line 55:
Plot.figure("TrigonometricTest")
Plot.figure("TrigonometricTest")
}}
}}

<translate>
<translate>

<!--T:7-->
<!--T:7-->
In FreeCAD 0.19 it is required to install the [[Image:Workbench_Plot.svg|24px]] [[Plot Workbench|Plot Workbench]] with the [[Std_AddonMgr|Add-on manager]], while from FreeCAD 0.20 onwards the external add-on is not required anymore to perform plots.
In FreeCAD 0.19 it is required to install the [[Image:Workbench_Plot.svg|24px]] [[Plot Workbench|Plot Workbench]] with the [[Std_AddonMgr|Add-on manager]], while from FreeCAD 0.20 onwards the external add-on is not required anymore to perform plots.
The commands above will create a new tab on main windows called '''TrigonometricTest'''. The new created document already have a set of axes. Each plot document have at least one set of axes that can be removed without using fully matplotlib control.
The commands above will create a new tab on main windows called '''TrigonometricTest'''. The new created document already have a set of axes. Each plot document have at least one set of axes that can be removed without using fully matplotlib control.


===Drawing functions=== <!--T:8-->
===Drawing functions===

<!--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 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:
</translate>


</translate>
{{Code|code=
{{Code|code=
import math
import math
Line 65: Line 74:
c = [math.cos(2.0*math.pi*tt) for tt in t]
c = [math.cos(2.0*math.pi*tt) for tt in t]
}}
}}

<translate>
<translate>

<!--T:9-->
<!--T:9-->
That will create 3 arrays of data (with 101 points):
That will create 3 arrays of data (with 101 points):
Line 75: Line 84:
<!--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 next commands:
</translate>


</translate>
{{Code|code=
{{Code|code=
Plot.plot(t,s)
Plot.plot(t,s)
Plot.plot(t,c)
Plot.plot(t,c)
}}
}}

<translate>
<translate>

<!--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. '''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.
Line 88: Line 97:
==Configuring plot== <!--T:12-->
==Configuring plot== <!--T:12-->


===Showing grid and legend=== <!--T:13-->
===Showing grid and legend===

<!--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 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.


</translate>
[[Image:Plot_Grid.svg‎]]
<translate>
<!--T:14-->
<!--T:14-->
[[Image:Plot_Grid.svg‎|center|Show/hide grid tool icon]]
{{Caption|Show/hide grid tool icon}}
<center><span style="font-variant:small-caps">Show/hide grid tool icon.</span></center>


<!--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 in order to hide it. Also you can show the legend with the tool provided.


</translate>
[[Image:Plot_Legend.svg]]
<translate>
<!--T:16-->
<!--T:16-->
[[Image:Plot_Legend.svg‎|center|Show/hide legend tool icon]]
{{Caption|Show/hide legend tool icon}}
<center><span style="font-variant:small-caps">Show/hide legend tool icon.</span></center>


<!--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, 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.


===Setting series labels=== <!--T:18-->
===Setting series labels===

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


</translate>
[[Image:Plot_Series.svg‎]]
<translate>
<!--T:19-->
<!--T:19-->
[[Image:Plot_Series.svg‎|center|Series configuration tool icon]]
{{Caption|Series configuration tool icon}}
<center><span style="font-variant:small-caps">Series configuration tool icon.</span></center>


<!--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:
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:
</translate>


</translate>
{{Code|code=
{{Code|code=
$y = \sin \left( 2 \pi t \right)$
$y = \sin \left( 2 \pi t \right)$
}}
}}

<translate>
<translate>

<!--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 the labels or titles that you want using it. Set the following label to second serie:
</translate>


</translate>
{{Code|code=
{{Code|code=
$y = \cos \left( 2 \pi t \right)$
$y = \cos \left( 2 \pi t \right)$
}}
}}

<translate>
<translate>

===Setting series style=== <!--T:22-->
===Setting series style===

<!--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 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.


===Setting axes labels=== <!--T:23-->
===Setting axes labels===

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


</translate>
[[Image:Plot_Labels.svg‎]]
<translate>
<!--T:24-->
<!--T:24-->
[[Image:Plot_Labels.svg‎|center|Labels tool icon]]
{{Caption|Labels tool icon}}
<center><span style="font-variant:small-caps">Labels tool icon.</span></center>


<!--T:25-->
<!--T:25-->
Line 149: Line 174:
Also change the size of all of them to 20.
Also change the size of all of them to 20.


==Saving plot== <!--T:27-->
==Saving plot==

<!--T:27-->
With saving plot tool you can save your plot as image file in several formats.
With saving plot tool you can save your plot as image file in several formats.


</translate>
[[Image:Plot_Save.svg]]
<translate>
<!--T:28-->
<!--T:28-->
[[Image:Plot_Save.svg‎|center|Save tool icon]]
{{Caption|Save tool icon}}
<center><span style="font-variant:small-caps">Save tool icon.</span></center>


<!--T:29-->
<!--T:29-->

Revision as of 10:06, 30 August 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 perform a basic plot using Plot Workbench and Python console.

Basic plot example

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

Plotting data

In order to plot data you don't need to create a new FreeCAD document, simply show 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 allow the module creates one automatically when you start plotting data. Create your own plot documents have 2 advantages:

  • You can set the document window label.
  • You can control easily on which document you plot your data.

In order to create new plot document simply launch 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 onwards the external add-on is not required anymore to perform plots. The commands above will create a new tab on main windows called TrigonometricTest. The new created document already have a set of axes. Each plot document have at least one set of axes that can be removed without using fully matplotlib control.

Drawing functions

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:

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 next commands:

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

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.

Configuring plot

Showing grid and legend

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

Show/hide grid tool icon

You can repeat the action in order to hide it. Also you can show the legend with the tool provided.

Show/hide legend tool icon

As you can see, legend is empty because we have not set any series label yet. In Plot module series without label are not represented at 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

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:

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

Since matplotlib supports LaTeX you can set all the labels or titles that you want using it. Set the following label to second serie:

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

Setting series style

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.

Setting axes labels

With the labels tool you can set labels associated to 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 saving plot tool you can save your plot as 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