Guida base per il modulo Grafico

From FreeCAD Documentation
Revision as of 18:12, 5 December 2014 by Renatorivo (talk | contribs) (Created page with "Questo crea 3 array di dati (con 101 punti): * ''t'' = Tempo in secondi. * ''s'' = Funzione seno. * ''c'' = Funzione coseno.")

In questo tutorial impareremo come creare un grafico di base utilizzando il modulo Grafico e la console Python. Altre informazioni sul modulo Grafico sono disponibili qui.

Esempio di grafico
Esempio di grafico
Esempio di grafico.

Nell'immagine precedente si può vedere il risultato che si intende ottenere. Questo tutorial descrive:

  • Come creare un grafico dalla console Python.
  • Come produrre un grafico da una serie di dati dalla console Python.
  • Come mostrare le linee della griglia.
  • Come visualizzare la legenda.
  • Come editare le etichette della serie, usando LaTeX.
  • Come modificare le etichette delle assi, usando LaTeX.
  • Come modificare gli stili della serie.
  • Come salvare il grafico.

Tracciare i dati

Per tracciare i dati non è necessario creare un nuovo documento di FreeCAD, è sufficiente visualizzare la console Python e iniziare a inviare i comandi, o usare le macro.

Creare un documento di grafico

I grafici sono documenti speciali che possono essere creati manualmente per inserire i dati in seguito, oppure si può consentire al modulo di crearli automaticamente quando si avvia la stampa dei dati.
Creare un documento personale per il grafico dà 2 vantaggi:

  • È possibile impostare l'etichetta del documento.
  • È possibile controllare facilmente in quale documento sono tracciati i dati (vedere più avanti per maggiori dettagli su questo aspetto).

Per creare un nuovo documento grafico lanciare semplicemente i seguenti comandi nel terminale Python:

 import Plot
 Plot.figure("TrigonometricTest")

Questi comandi creano un nuovo documento nella finestra principale chiamato TrigonometricTest.
Il nuovo documento appena creato possiede già di un sistema di assi. Ogni documento di Grafico deve avere almeno una serie di assi che non possono essere rimossi senza il completo controllo di matplotlib.

Tracciare le funzioni

Dato che il comando Plot avvia un nuovo documento, a questo punto è possibile iniziare a lavorare.
Bisogna ricordare che ogni comando del modulo Grafico che viene eseguito aggiunge una serie al grafico creato, questo fino a quando non si crea un nuovo documento, quindi, come regola generale, è bene controllare quali documenti sono aperti.

Ora si possono creare i dati per le funzioni seno e coseno che sono le funzioni che si vogliono tracciare:

 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]

Questo crea 3 array di dati (con 101 punti):

  • t = Tempo in secondi.
  • s = Funzione seno.
  • c = Funzione coseno.

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. When module has been loaded use grid tool in order to show it.

Show/hide grid tool icon
Show/hide grid tool icon
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
Show/hide legend tool icon
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 auxiliar lines.

Setting series labels

With the series tool you can edit some series parameters.

Series configuration tool icon
Series configuration tool icon
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
Labels tool icon
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
Save tool icon
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.