Skip to content

Visualization

In this section, we'll explore the different ways to visualize field data from MICRESS binary files using MicPy.

Built-In Visualization

MicPy provides a built-in method to visualize 2D slices of Field objects. The bin.plot() method expects a Field object as input and returns a Matplotlib Figure and Axes object.

from micpy import bin

# Read a time series from a binary file
with bin.File("A001_Delta_Gamma.conc1") as file:
    series = file.read()

# Plot the last field in the series
field = series.get_field(-1)
fig, ax = bin.plot(field)

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 import bin

# Read a time series from a binary file
with bin.File("A005_Grain_Growth_Misorientation_3D.korn") as file:
    series = file.read()

# Plot the last field in the series along the x-axis
field = series.get_field(-1)
fig, ax = bin.plot(field, axis="x", index=0)

from micpy import bin

# Read a time series from a binary file
with bin.File("A005_Grain_Growth_Misorientation_3D.korn") as file:
    series = file.read()

# Plot the last field in the series along the y-axis
field = series.get_field(-1)
fig, ax = bin.plot(field, axis="y", index=0)

from micpy import bin

# Read a time series from a binary file
with bin.File("A005_Grain_Growth_Misorientation_3D.korn") as file:
    series = file.read()

# Plot the last field in the series along the z-axis
field = series.get_field(-1)
fig, ax = bin.plot(field, axis="z", index=0)

Customizing the Visualization

The plot() method accepts an option argument args of type PlotArgs. This dataclass provides a set of optional parameters to customize the visualization. For example, you can set the title, resolution (dpi), and color map (cmap).

from micpy import bin

# Read a time series from a binary file
with bin.File("A001_Delta_Gamma.conc1") as file:
    series = file.read()

# Plot the last field in the series with custom arguments
field = series.get_field(-1)
args = bin.PlotArgs(title="Carbon", dpi=150, cmap="coolwarm")
fig, ax = bin.plot(field, args=args)

You can customize the visualization even further by directly modifying the Matplotlib Figure and Axes 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 import bin
from matplotlib import pyplot as plt

# Read a time series from a binary file
with bin.File("A001_Delta_Gamma.conc1") as file:
    series = file.read()

# Extract two fields from the series
field1 = series.get_field(5)
field2 = series.get_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 = bin.plot(field1, args=bin.PlotArgs(ax=ax1))
fig2, ax2 = bin.plot(field2, args=bin.PlotArgs(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 PlotArgs object.

# 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 = bin.plot(field1, args=bin.PlotArgs(ax=ax1, vmin=vmin, vmax=vmax))
fig2, ax2 = bin.plot(field2, args=bin.PlotArgs(ax=ax2, vmin=vmin, vmax=vmax))

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

Reference

micpy.bin

The micpy.bin module provides methods to read and write binary files.

PlotArgs dataclass

Arguments for plotting a field.

Parameters:

Name Type Description Default
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'

plot(field, axis='y', index=0, args=None)

Plot a slice of the field.

Parameters:

Name Type Description Default
field Field

Field to plot.

required
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
args PlotArgs

Arguments for plotting. Defaults to None.

None

Returns:

Type Description
Tuple[Figure, Axes]

Matplotlib figure and axes of the plot.