Skip to content

Visualization

In this section, we'll explore the different ways to visualize field data from MICRESS binary files using MicPy's built-in plotting capabilities based on the Matplotlib library.

VTK Export & ParaView Integration

For advanced 3D visualization and in-depth analysis, MicPy can export data to the Visualization Toolkit (VTK) format, enabling seamless use with external tools such as ParaView. See the VTK Export and ParaView Plugin sections for details on exporting data and visualizing it in ParaView.

Built-In Visualization

MicPy provides a built-in method to visualize 2D slices of Field objects. The plot() method returns a Matplotlib Figure, Axes and Colorbar objects.

from micpy.bin import read

# Read the last field from a binary file
field = read("A001_Delta_Gamma.conc1", -1)

# Plot the field using the default settings
fig, ax, cbar = field.plot()

The plot() method also accepts optional arguments axis and index. The axis argument specifies the axis along which the slice is taken, and can be one of the following: x, y, or z. The index argument specifies the slice index, and has to be an integer.

from micpy.bin import read

# Read the last field from a binary file
field = read("A005_Grain_Growth_Misorientation_3D.korn", -1)

# Plot the field along the x-axis
fig, ax, cbar = field.plot(axis="x", index=0)

from micpy.bin import read

# Read the last field from a binary file
field = read("A005_Grain_Growth_Misorientation_3D.korn", -1)

# Plot the field along the y-axis
fig, ax, cbar = field.plot(axis="y", index=0)

from micpy.bin import read

# Read the last field from a binary file
field = read("A005_Grain_Growth_Misorientation_3D.korn", -1)

# Plot the field along the z-axis
fig, ax, cbar = field.plot(axis="z", index=0)

Customizing the Visualization

The plot() method accepts various Matplotlib parameters such as the title, resolution (dpi), and color map (cmap).

1
2
3
4
5
6
7
from micpy.bin import read

# Read the last field from a binary file
field = read("A001_Delta_Gamma.conc1", -1)

# Plot the field with custom arguments
fig, ax, cbar = field.plot(title="Carbon", dpi=150, cmap="coolwarm")

You can customize the visualization even further by directly modifying the Matplotlib Figure, Axes and Colorbar objects returned by the plot() method. This allows you to perform any kind of advanced customizations using Matplotlib's extensive functionality.

# Limit the x-axis and y-axis
ax.set_xlim(300, 500)
ax.set_ylim(0, 1300)

# Add text annotations
delta = ax.text(370, 850, "$\\delta$", color="white", fontsize=14)
gamma = ax.text(370, 200, "$\\gamma$", color="black", fontsize=14)

Combining Multiple Plots

You can combine multiple plots into a single figure using Matplotlib's subplots functionality. This allows you to visualize two or more slices of the field data side by side. In the following example, we plot two different fields from the same series in a single figure.

from micpy.bin import read
from matplotlib import pyplot as plt

# Read a time series from a binary file
series = file.read("A001_Delta_Gamma.conc1")

# Extract two fields from the series
field1 = series.field(5)
field2 = series.field(10)

# Create a figure with two subplots on a grid with 1 row and 2 columns
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 4))

# Plot the two fields using the subplots' axes
fig1, ax1, cbar1 = field1.plot(ax=ax1)
fig2, ax2, cbar2 = field2.plot(ax=ax2)

# Optionally tighten the layout to prevent overlapping
fig.tight_layout()

In the example above, each subplot's color map is determined by the minimum and maximum values of the field data. If you want to use identical color maps for both subplots, you can pass the vmin and vmax arguments to the plot() method.

# Create a figure with two subplots on a grid with 1 row and 2 columns
fig_norm, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 4))

# Determine the minimum and maximum values of both fields
vmin = min(field1.min(), field2.min())
vmax = max(field1.max(), field2.max())

# Plot the two fields explicitly setting the color map range
fig1, ax1, cbar1 = field1.plot(ax=ax1, vmin=vmin, vmax=vmax)
fig2, ax2, cbar2 = field2.plot(ax=ax2, vmin=vmin, vmax=vmax)

# Optionally tighten the layout to prevent overlapping
fig_norm.tight_layout()

Reference

micpy.bin.Field.plot(axis='y', index=0, title=None, xlabel=None, ylabel=None, figsize=None, dpi=None, aspect='equal', ax=None, cax=None, vmin=None, vmax=None, cmap='micpy', alpha=1.0, interpolation='none', extent=None)

Plot a slice of the field using Matplotlib.

Parameters:

Name Type Description Default
axis str

Axis to plot. Possible values are x, y, and z. Defaults to y.

'y'
index int

Index of the slice. Defaults to 0.

0
title str

Title of the plot. Defaults to None.

None
xlabel str

Label of the x-axis. Defaults to None.

None
ylabel str

Label of the y-axis. Defaults to None.

None
figsize Tuple[float, float]

Figure size. Defaults to None.

None
dpi int

Figure DPI. Defaults to None.

None
aspect str

Aspect ratio. Defaults to equal.

'equal'
ax Axes

Axes of the plot. Defaults to None.

None
cax Axes

Axes of the color bar. Defaults to None.

None
vmin float

Minimum value of the color bar. Defaults to None.

None
vmax float

Maximum value of the color bar. Defaults to None.

None
cmap str

Colormap. Defaults to micpy.

'micpy'
alpha float

Transparency of the plot. Defaults to 1.0.

1.0
interpolation str

Interpolation method. Defaults to none.

'none'
extent Tuple[float, float, float, float]

Extent of the plot.

None

Returns:

Type Description
Tuple[Figure, Axes, Colorbar]

Matplotlib figure, axes, and color bar.