How to plot results

Pyleecan has generic built-in plot methods which allow to easily plot:

  • simulated or experimental fields (unit conversions supported)

  • slices (normalizations available)

  • Fourier Transforms

but also to compare on the same graph a list of fields.

This flexibility is provided by the SciDataTool module, which has been created to ease the handling of scientific data, and considerately simplify plot commands. It unifies the extraction of relevant data (e.g. slices), whether they are stored in the time/space or in the frequency domain. The call to Fourier Transform functions is transparent, although it still can be parameterized.

This tutorial explains how to use Pyleecan’s plot commands. It is available on GitHub. For more details on the SciDataTool package, please refer to the specific tutorials:

The first step consists in getting simulation data. In order to demonstrate the capabilities of the plot commands, a simulation over 2048 time steps has been performed in the simulation tutorial, and the resulting Output object has been stored on Pyleecan’s website. First, these results need to be downloaded:

[ ]:
# Import Pyleecan modules
from pyleecan.definitions import USER_DIR
from os.path import join, isfile
from pyleecan.Functions.load import load
from json import load as jload

data_path = join(USER_DIR, 'tuto_Simu_FEMM_results.json')

# Download simulation data (if needed)
if not isfile(data_path):
    print('Downloading the simulation data...')
    url = 'https://www.pyleecan.org/Data/tuto_Simu_FEMM_results.json'
    urllib.request.urlretrieve(url, data_path)
    print("Done")

# Load Output object from json file
print("Loading json file...")
out = load(data_path)
print("Done")

The following example demonstrates the syntax to quickly plot the airgap radial flux density obtained at the end of the simulation:

[ ]:
%matplotlib notebook
#------------------------------------------------------
# Plot the flux density as a function of space
out.plot_2D_Data("mag.B","angle")
#------------------------------------------------------

The plot command requires the name of the field (“mag.Br” for instance). Titles and legends are automatically generated. In this tutorial, only the airgap radial flux density is plotted, but the commands work with any field computed in Pyleecan.

Note that each plot method (with the same parameters) also exists as a function, so that the command plot_2D_Data(out.mag.Br, "angle") would give the same plot.

There are, for the time being, 8 plot commands in Pyleecan, which can apply to any DataTime, DataFreq or VectorField object from SciDataTool:

  • plot_A_space: field as a function of the angle

  • plot_A_time: field as a function of time

  • plot_A_fft_space: fft as a function of the wavenumber

  • plot_A_fft_time: fft as a function of the frequency

  • plot_A_surf: 3D surface in time/space domain

  • plot_A_time_space: 3D surface + angle + time

  • plot_A_cfft2: 3D stem plot of the 2D Fourier Transform

  • plot_A_fft2: pcolor plot of the 2D Fourier Transform

There is also a function to animate any of those plots as a gif:

  • animate_as_gif

Fonts and colors are in general taken from the config_dict, and can also be tuned for each plot. If a field is not stored in a Data object, plot functions such as plot_A_2D, plot_A_3D and plot_A_4D allow to benefit from the same formatting.

plot_A_space

Description

This plot command plots a field as a function of the angle, at a given time step (first time step by default, or value prescribed by the user). It can also add a subplot with the space Fourier Transform if the is_fft key is activated.

Parameters

Parameter

Description

Type

Default Value

Data_str

Name of the field

str

mandatory

index_list

List of indices to take from a components axis

[int]

[0]

t

Slice at time=t

float

None

t_index

Slice at time[t_index]

int

0

is_deg

Angle in degrees or radians

bool

True

is_fft

Add the fft as a subplot

bool

False

is_spaceorder

x-axis of the fft in space orders

bool

False

r_max

Maximum value for the wavenumbers to display

int

100

fund_harm

Frequency of the fundamental harmonic

float

None

is_norm

Normalize the field

bool

False

unit

Unit of the field

str

"SI"

data_list

List of Data objects to compare

[Data]

[]

component_list

List of component names to plot in separate figures (in case of VectorField object (flux, force)

[str]

None

legend_list

List of legends to use for each Data object (including reference one) instead of Data.name

[str]

[]

color_list

List of colors to use for each Data object

list

colors from config_dict

save_path

Path and name of the png file to save

str

None

y_min

Minimum value for the y-axis (automatically set if not provided): useful to keep frozen axes in an animation

float

None

y_max

Maximum value for the y-axis (automatically set if not provided): useful to keep frozen axes in an animation

float

None

mag_max

Maximum value for the fft y-axis (automatically set if not provided): useful to keep frozen axes in an animation

float

None

is_auto_ticks

In fft plot, adjust ticks to wavenumbers

bool

True

Example

[ ]:
#------------------------------------------------------
# Plot the flux density as a function of space with the spatial fft
out.plot_2D_Data("mag.B","angle")
out.plot_2D_Data("mag.B","wavenumber=[0,78]")
#------------------------------------------------------

plot_A_time

Description

This plot command plots a field as a function of time, at a given angle (first angle by default, or value prescribed by the user). It can also add a subplot with the Fourier Transform if the is_fft key is activated.

Parameters

Parameter

Description

Type

Default Value

Data_str

Name of the field

str

mandatory

index_list

List of indices to take from a components axis

[int]

[0]

alpha

Slice at angle=alpha

float

None

alpha_index

Slice at angle[alpha_index]

int

0

is_fft

Add the fft as a subplot

bool

False

is_elecorder

x-axis of the fft in electrical orders

bool

False

freq_max

Maximum value for the frequencies to display

float

20000

is_norm

Normalize the field

bool

False

unit

Unit of the field

str

"SI"

data_list

List of Data objects to compare

[Data]

[]

component_list

List of component names to plot in separate figures (in case of VectorField object (flux, force)

[str]

None

legend_list

List of legends to use for each Data object (including reference one) instead of Data.name

[str]

[]

color_list

List of colors to use for each Data object

list

colors from gen_color.py

save_path

Path and name of the png file to save

str

None

y_min

Minimum value for the y-axis (automatically set if not provided): useful to keep frozen axes in an animation

float

None

y_max

Maximum value for the y-axis (automatically set if not provided): useful to keep frozen axes in an animation

float

None

mag_max

Maximum value for the fft y-axis (automatically set if not provided): useful to keep frozen axes in an animation

float

None

is_auto_ticks

In fft plot, adjust ticks to wavenumbers

bool

True

Example

[ ]:
from numpy import pi

#------------------------------------------------------
# Plot the flux density as a function of time with the time fft
out.plot_2D_Data("mag.B","time", "angle[10]")
out.plot_2D_Data("mag.B","freqs=[0,2000]", "angle[10]")
#------------------------------------------------------

plot_A_fft_space

Description

This plot command plots the Fourier Transform of a field as a function of the wavenumber, at a given time step (first time step by default, or value prescribed by the user). It gives the same graph as the bottom one in plot_A_space.

Parameters

Parameter

Description

Type

Default Value

Data_str

Name of the field

str

mandatory

t

Slice at time=t

float

None

t_index

Slice at time[t_index]

int

0

is_spaceorder

x-axis of the fft in space orders

bool

False

r_max

Maximum value for the wavenumbers to display

int

100

fund_harm

Frequency of the fundamental harmonic

float

None

is_norm

Normalize the field

bool

False

unit

Unit of the field

str

"SI"

data_list

List of Data objects to compare

[Data]

[]

component_list

List of component names to plot in separate figures (in case of VectorField object (flux, force)

[str]

None

legend_list

List of component names to plot in separate figures

[str]

[]

color_list

List of colors to use for each Data object

list

colors from config_dict

save_path

Path and name of the png file to save

str

None

mag_max

Maximum value for the fft y-axis (automatically set if not provided): useful to keep frozen axes in an animation

float

None

is_auto_ticks

In fft plot, adjust ticks to wavenumbers

bool

True

Example

[ ]:
#------------------------------------------------------
# Plot the fft of the radial component
out.plot_2D_Data("mag.B","wavenumber=[0,78]", component_list=["radial"])
#------------------------------------------------------

plot_A_fft_time

Description

This plot command plots the Fourier Transform of a field as a function of the frequency, at a given angle (first angle by default, or value prescribed by the user). It gives the same graph as the bottom one in plot_A_time.

Parameters

Parameter

Description

Type

Default Value

Data_str

Name of the field

str

mandatory

alpha

Slice at angle=alpha

float

None

alpha_index

Slice at angle[alpha_index]

int

0

is_elecorder

x-axis of the fft in electrical orders

bool

False

freq_max

Maximum value for the frequencies to display

float

20000

is_norm

Normalize the field

bool

False

unit

Unit of the field

str

"SI"

data_list

List of Data objects to compare

[Data]

[]

component_list

List of component names to plot in separate figures (in case of VectorField object (flux, force)

[str]

None

legend_list

List of legends to use for each Data object (including reference one) instead of Data.name

[str]

[]

color_list

List of colors to use for each Data object

list

colors from gen_color.py

save_path

Path and name of the png file to save

str

None

mag_max

Maximum value for the fft y-axis (automatically set if not provided): useful to keep frozen axes in an animation

float

None

is_auto_ticks

In fft plot, adjust ticks to wavenumbers

bool

True

Example

[ ]:
#------------------------------------------------------
# Plot the fft of the tangential component
out.plot_2D_Data("mag.B","freqs=[0,2000]", "angle[10]", component_list=["tangential"])
#------------------------------------------------------

plot_A_surf

Description

This plot command plots a field as a 3D surface in the time/space domain.

Parameters

Parameter

Description

Type

Default Value

Data_str

Name of the field

str

mandatory

is_deg

Angle in degrees or radians

bool

True

t_max

Maximum value for time

float

max(time)

a_max

Maximum value for space

float

400

z_max

Maximum value for the field

float

max(data)

is_norm

Normalize the field

bool

False

unit

Unit of the field

str

"SI"

colormap

Colormap

cmp

"RdBu_r"

save_path

Path and name of the png file to save

str

None

component_list

List of component names to plot in separate figures (in case of VectorField object (flux, force)

[str]

None

Example

[ ]:
#------------------------------------------------------
# Plot the flux density as a surface over time and space
out.plot_3D_Data("mag.B","time","angle")
#------------------------------------------------------

plot_A_time_space

Description

This plot command gives a multiplot of the field as a function of time, space, with time and space fft, and a contour plot in the time/space domain.

Parameters

Parameter

Description

Type

Default Value

Data_str

Name of the field

str

mandatory

is_deg

Angle in degrees or radians

bool

True

is_elecorder

x-axis of the time fft in electrical orders

bool

False

is_spaceorder

x-axis of the space fft in space orders

bool

False

freq_max

Maximum value for the frequencies to display

float

20000

r_max

Maximum value for the wavenumbers to display

int

100

mag_max

Maximum value for the magnitude

float

max(data)

is_norm

Normalize the field

bool

False

unit

Unit of the field

str

"SI"

colormap

Colormap

cmp

"RdBu_r"

save_path

Path and name of the png file to save

str

None

component_list

List of component names to plot in separate figures (in case of VectorField object (flux, force)

[str]

None

Example

[ ]:
#------------------------------------------------------
# Plot the flux density as a function of time and space
out.plot_A_time_space("mag.B", freq_max=freq_max, r_max=r_max)
#------------------------------------------------------

plot_A_cfft2

Description

This plot commands plots the FFT2D as a 3D stem plot.

Parameters

Parameter

Description

Type

Default Value

Data_str

Name of the field

str

mandatory

is_elecorder

x-axis of the time fft in electrical orders

bool

False

is_spaceorder

x-axis of the space fft in space orders

bool

False

freq_max

Maximum value for the frequencies to display

float

20000

r_max

Maximum value for the wavenumbers to display

int

100

mag_max

Maximum value for the magnitude

float

max(data)

N_stem

Number of harmonic stems to display

int

100

disp_negative

Plot negative frequencies

bool

False

is_norm

Normalize the field

bool

False

unit

Unit of the field

str

"SI"

save_path

Path and name of the png file to save

str

None

component_list

List of component names to plot in separate figures (in case of VectorField object (flux, force)

[str]

None

Example

[ ]:
#------------------------------------------------------
# Plot the fft2 with stems
out.plot_3D_Data("mag.B","freqs=[0,"+str(freq_max)+"]","wavenumber=[-"+ str(r_max) + "," +str(r_max)+"]", is_2D_view = False)
#------------------------------------------------------

plot_A_fft2

Description

This plot command plots the FFT2D as a colored scatter figure.

Parameters

Parameter

Description

Type

Default Value

Data_str

Name of the field

str

mandatory

is_phase

Add the phase as a subplot

bool

False

is_deg

Phase in degrees or radians

bool

True

is_elecorder

x-axis of the time fft in electrical orders

bool

False

is_spaceorder

x-axis of the space fft in space orders

bool

False

freq_max

Maximum value for the frequencies to display

float

20000

r_max

Maximum value for the wavenumbers to display

int

100

mag_max

Maximum value for the magnitude

float

max(data)

is_norm

Normalize the field

bool

False

unit

Unit of the field

str

"SI"

colormap

Colormap

cmp

"RdBu_r"

save_path

Path and name of the png file to save

str

None

component_list

List of component names to plot in separate figures (in case of VectorField object (flux, force)

[str]

None

Example

[ ]:
#------------------------------------------------------
# Plot the fft2 as a colored scatter
freq_max=2000
r_max=20
out.plot_3D_Data("mag.B", "freqs=[0,"+str(freq_max)+"]","wavenumber=[-"+ str(r_max) + "," +str(r_max)+"]", is_2D_view = True)
#------------------------------------------------------

animate_as_gif

This function allows to animate any of the former plot commands as a gif. The animation can be along time or angle, through the use of t_index or alpha_index, or along a list of Data objects. The range of indices to be animated can be specified, as well as the step. Temporary png files will be created and removed at the end of the script. Note also that it is useful to froze the axes using y_min, y_max, etc.

Parameters

Parameter

Description

Type

Default Value

func

Plot function

function

mandatory

data_list

List of Data objects to animate, or single Data object

[Data] or Data

mandatory

save_path

Path where to save the gif

str

"./"

file_name

Name of the gif file

str

"animated_plot.gif"

index_var

Name of the plot parameter along which to animate

str

t_index

index_max

Maximum value of the index

int

50

index_step

Step for the index (number of frames = index_max / index_step)

int

1

kwargs

Parameters of the plot function

dict

Example

from pyleecan.Functions.Plot.animate_as_gif import animate_as_gif
from pyleecan.Functions.Plot.plot_A_space import plot_A_space

#------------------------------------------------------
# Generate the gif
animate_as_gif(
    plot_A_space,
    out.mag.B,
    index_max=2048,
    index_step=16,
    y_min=-2,
    y_max=2,
)
#------------------------------------------------------