Plot Basic tutorial

From FreeCAD Documentation
Revision as of 18:23, 5 December 2014 by Renatorivo (talk | contribs) (Created page with "Lo que graficará los datos. El comando '''plot''' admite que se le pase el título de la serie como tercer argumento, no obstante como vamos a editar manualmente esos datos o...")

En este tutorial vamos a aprender a crear gráficos usando el módulo de graficado y la consola Python. Puedes aprender más sobre el módulo de graficado aquí.

Ejemplo de gráfico
Ejemplo de gráfico
Ejemplo de gráfico.

En la imagen anterior puedes ver una muestra del gráfico que pretendemos generar. Mediante este tutorial aprenderás:

  • A crear un documento de gráfico mediante la consola de Python.
  • A graficar series de datos mediante la consola de Python.
  • A mostrar la malla.
  • A mostrar la legenda.
  • A configurar los títulos de las series usando LaTeX.
  • A configurar los títulos de los ejes usando LaTeX.
  • A configurar la apariencia de las series.
  • A guardar el gráfico como archivo de imagen.

Graficar

El módulo de graficado no necesita de ningún documento de FreeCAD para poder comenzar a trabajar, simplemente despliega la consola de Python, o genera los macros oportunos y comienza a trabajar.

Crear un documento de gráfico

Los gráficos son documentos especiales dentro de FreeCAD, y pueden ser creados bien manualmente, bien automáticamente cuando se pretende trazar graficos y todavía no existe un documento activo. Crear manualmente los gráficos tiene dos ventajas:

  • Puedes elegir la etiqueta de la ventana.
  • Puedes controlar facilmente en que documento se trazan los gráficos.

Para crear un documento de gráfico nuevo simplemente lanza los siguientes comandos desde la terminal:

 import Plot
 Plot.figure("TrigonometricTest")

Esto creará un nuevo documento llamado TrigonometricTest. Éste nuevo documento ya contiene unos ejes. Cada documento de gráfico debe tener al menos un set de ejes que no se podrá borrar a menos que se entre en profundidad en la interfaz matplotlib.

Trazar curvas

Usted podría haber comenzado su trabajo en este punto, pues cuando se solicita al módulo de graficado que traze una curva sin existir un documento apropiado, el módulo genera uno nuevo, no obstante todos los consiguientes trazados se incluirán en el mismo documento hasta que se cree uno nuevo. Por esta razón es buena idea siempre tener controlado el número de documentos.

Lo primero que necesitamos para trazar las curvas es generar los datos:

 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]

Lo que creará tres vectores (con 101 puntos):

  • t = Tiempo en segundos.
  • s = Función seno.
  • c = Función coseno.

Para trazar las curvas correspondientes tan sólo será necesario lanzar los siguientes comandos:

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

Lo que graficará los datos. El comando plot admite que se le pase el título de la serie como tercer argumento, no obstante como vamos a editar manualmente esos datos optaremos por no emplear esta propiedad.

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.