Line plots¶
Line plots are an excellent tool for visualizing trends, especially when tracking changes over time or any continuous variable. In this section, we'll demonstrate how to create a line plot using Pandas by analyzing phase fraction data from a solidification simulation.
Overview of the data¶
For this example, we will be working with data stored in the A001_Delta_Gamma.TabF
file. This dataset tracks the evolution of phase fractions during solidification, with columns including:
Simulation time [s]
: The elapsed time since the simulation started.Temperature [K]
: The temperature at each time point.Fraction Phase 0 LIQUID
: The fraction of the liquid phase present.Fraction Phase 1 BCC_A2
: The fraction of the BCC_A2 phase.Fraction Phase 2 FCC_A1
: The fraction of the FCC_A1 phase.
Here's a snapshot of the dataset structure:
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 module¶
We start by importing the tab module from MicPy, which will enable us to load the tabular data into a Pandas DataFrame.
Step 2: Loading the data¶
Next, we load the data from the A001_Delta_Gamma.TabF
file into a Pandas DataFrame for further processing.
Step 3: Plotting the data¶
To visualize the phase fractions against temperature, we use the plot()
method from Pandas. We specify the x
parameter as the temperature column index and the y
parameter as a list containing the phase fraction columns.
Step 4: Customizing the plot¶
For a more detailed visualization, you can add various elements such as titles, axis labels, and markers. Here's how you can do that:
Step 5: Advanced customization¶
To fine-tune the plot further, you can access the Matplotlib Axes object returned by the plot()
method. This allows you to modify specific aspects of the plot. For example, here's how you can add custom y-ticks, gridlines, and a text annotation:
Conclusion¶
The steps outlined above demonstrate how to create and customize a line plot using data from a simulation. By following these steps, you can effectively visualize trends in your data, making it easier to draw meaningful conclusions.
Below is the complete code used to generate the customized line plot:
from micpy import tab
# Load the data
df = tab.read("A001_Delta_Gamma.TabF")
# Plot the data
ax = df.plot(
x=1,
y=[2, 3, 4],
figsize=(7, 4),
title="Solidification of Carbon-Manganese Steel",
xlabel="Temperature [K]",
ylabel="Volume fraction",
label=["Liquid", "Ferrite", "Austenite"],
marker="o"
)
# Advanced customization
ax.set_yticks([0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1])
ax.grid(linestyle=":")
ax.annotate(
"Peritectic reaction",
(1760, 0.4),
(1760, 0.5),
ha="center",
arrowprops=dict(arrowstyle='->'),
fontsize=10
)