Introduction
Matplotlib is a data visualization library in Python. In this tutorial, we will discuss the differences between \dfrac and \frac TeX macros when using Mathtex in Matplotlib.
VM Tips
After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.
Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.
If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.
Import Matplotlib
To use Matplotlib, we first need to import it. We will also import numpy to generate some sample data for visualization.
import matplotlib.pyplot as plt
import numpy as np
Create Sample Data
We will create two arrays of sample data to plot.
x = np.linspace(0, 10, 100)
y = np.sin(x)
Create a Figure and Axis
We will create a figure and axis object to plot our data on.
fig, ax = plt.subplots()
Plot the Data with \frac
We will plot the data with the \frac TeX macro and display the resulting plot.
ax.plot(x, y, label=r'$\frac{sin(x)}{x}$')
ax.legend()
plt.show()
Plot the Data with \dfrac
We will plot the data with the \dfrac TeX macro and display the resulting plot.
fig, ax = plt.subplots()
ax.plot(x, y, label=r'$\dfrac{sin(x)}{x}$')
ax.legend()
plt.show()
Summary
In this tutorial, we discussed the differences between \dfrac and \frac TeX macros when using Mathtex in Matplotlib. We demonstrated how to plot data with both macros and display the resulting plots. By default, \frac produces a smaller, in-line style fraction, while \dfrac produces a larger, display style fraction.