Skip to content

Subplots

Subplots are a powerful feature in Matplotlib that allow you to create multiple plots within a single figure. This is particularly useful when you need to compare different datasets or visualize multiple aspects of a dataset simultaneously. In this section, we'll explore two examples: the first demonstrates how to create multiple plots side by side, and the second shows how to combine data from multiple DataFrames into a single plot for comparison.

Example 1: Creating Side-by-Side Plots

In the first example, we use subplots to visualize the evolution of different phase fractions over time. The A001_Delta_Gamma.TabF tabular file contains data on phase fractions at various simulation times and temperatures. By creating separate plots for each phase – Liquid, Ferrite (BBC), and Austenite (FCC) – we can compare how these phases evolve over time within a single figure.

Below is a sample structure of the dataset:

Simulation time [s] Temperature [K] Fraction Phase 0 LIQUID Fraction Phase 1 BCC_A2 Fraction Phase 2 FCC_A1
0.0000 1786.00000 1.000000 0.000000 0.000000e+00
1.0000 1785.00000 0.998751 0.001249 0.000000e+00
2.5000 1783.50000 0.992867 0.007133 0.000000e+00
5.0000 1781.00000 0.976858 0.023142 0.000000e+00
7.5000 1778.50000 0.959798 0.040202 0.000000e+00
... ... ... ... ...

Step 1: Importing the Required Modules

Start by importing the necessary modules, including tab from MicPy for reading the tabular data and pyplot from Matplotlib for plotting.

from micpy import tab
from matplotlib import pyplot as plt

Step 2: Setting up the Subplots

We create a figure with three subplots arranged in a single row. The subplots() function returns the figure and an array of axes.

fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8, 4))

Step 3: Plotting the Data

We plot the Liquid, Ferrite, and Austenite phase fractions against temperature on their respective axes. Each plot is customized with a title and color, and the legend is disabled for clarity.

4
5
6
ax1 = df.plot(x=1, y=2, ax=ax1, title="Liquid", color="blue", legend=False)
ax2 = df.plot(x=1, y=3, ax=ax2, title="Ferrite", color="green", legend=False)
ax3 = df.plot(x=1, y=4, ax=ax3, title="Austenite", color="red", legend=False)

Step 4: Adding Labels and Gridlines

We set y-axis labels and enable gridlines for all subplots to improve readability.

7
8
9
for ax in (ax1, ax2, ax3):
    ax.set_ylabel("Phase fraction")
    ax.grid(ax.grid(linestyle=":")

Step 5: Adjusting the Layout

Finally, we use tight_layout() to ensure that the subplots are neatly arranged without overlapping elements.

fig.tight_layout()

The resulting figure contains three side-by-side plots, each showing the evolution of a different phase fraction. This approach is an alternative to creating separate figures for each plot or combining all data into a single axis.

The complete code for creating side-by-side plots is shown below:

# Import the required modules
from micpy import tab
from matplotlib import pyplot as plt

# Read the tabular data
df = tab.read("A001_Delta_Gamma.TabF")

# Create subplots
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(8, 4))

# Plot the data
ax1 = df.plot(x=1, y=2, ax=ax1, title="Liquid", color="blue", legend=False)
ax2 = df.plot(x=1, y=3, ax=ax2, title="Ferrite", color="green", legend=False)
ax3 = df.plot(x=1, y=4, ax=ax3, title="Austenite", color="red", legend=False)

# Add labels and gridlines
for ax in (ax1, ax2, ax3):
    ax.set_ylabel("Phase fraction")
    ax.grid(linestyle=":")

# Adjust the layout
fig.tight_layout()

Example 2: Combining Data from Multiple Tabular Files

In the second example, we demonstrate how to use subplots to compare results from multiple tabular files. Specifically, we look at the average grain radius over time for two different simulations – one without considering misorientations and one with. This is a common requirement, where comparing the effects of different simulation parameters is crucial for understanding the behavior of the material.

We will use the T10_01_GrainGrowth_2D.TabK and T10_02_GrainGrowthMisorientation_2D.TabK tabular files, which contain data on the average grain radius at different simulation times for the two scenarios. The structure of the dataset is as follows:

Simulation time [s] Avg. radius [microns] ...
0.000000 13.381 ...
0.007856 13.381 ...
3.525100 13.423 ...
5.622800 13.465 ...
6.097000 13.508 ...
... ... ...
251.830000 53.524 ...

Step 1: Importing the Required Module

As before, we start by importing the necessary module for reading the tabular data and plotting the results.

from micpy import tab
from matplotlib import pyplot as plt

Step 2: Loading the Data

We read data from two tabular files, each representing a different simulation. The first file T10_01_GrainGrowth_2D.TabK contains data without considering misorientations, while the second file T10_02_GrainGrowthMisorientation_2D.TabK takes misorientations into account.

df1 = tab.read("T10_01_GrainGrowth_2D.TabK")
df2 = tab.read("T10_02_GrainGrowthMisorientation_2D.TabK")

Step 3: Creating Subplots

We create a single subplot for comparing the two datasets. This subplot will contain both sets of data, plotted on the same axes for direct comparison.

fig, ax = plt.subplots(figsize=(6, 4))

Step 4: Plotting the Data

We plot the average grain radius against time for both simulations. Each plot is labeled and colored appropriately to distinguish between the two datasets.

ax = df1.plot(x=0, y=1, ax=ax, label="Without misorientations", color="red")
ax = df2.plot(x=0, y=1, ax=ax, label="With misorientations", color="green")

Step 5: Customizing the Plot

We add a title, and axis labels, and ensure that the legend is displayed to differentiate the data series.

ax.set_title("Grain growth")
ax.set_xlabel("Time [s]")
ax.set_ylabel("Average grain radius [µm]")

The resulting plot effectively compares the grain growth behavior under different simulation conditions, with both datasets clearly distinguished on the same plot.

The complete code for combining data from multiple sources is shown below:

# Import the required modules
from micpy import tab
from matplotlib import pyplot as plt

# Read the tabular data
df1 = tab.read("T10_01_GrainGrowth_2D.TabK")
df2 = tab.read("T10_02_GrainGrowthMisorientation_2D.TabK")

# Create subplots
fig, ax = plt.subplots(figsize=(6, 4))

# Plot the data
ax = df1.plot(x=0, y=1, ax=ax, label="Without misorientations", color="red")
ax = df2.plot(x=0, y=1, ax=ax, label="With misorientations", color="green")

# Customize the plot
ax.set_title("Grain growth")
ax.set_xlabel("Time [s]")
ax.set_ylabel("Average grain radius [µm]")