Basic Plotting with Matplotlib

Data visualization is an essential skill for analyzing and presenting scientific data effectively. Python itself doesn’t include plotting capabilities in its core language, but Matplotlib provides powerful and flexible tools for creating visualizations. Matplotlib is the most widely used plotting library in Python and serves as an excellent starting point for creating basic plots.

Matplotlib works well with NumPy, Python’s numerical computing library, to create a variety of plot types including line plots, scatter plots, bar charts, and more. For this document, we’ve already imported both libraries as you can see in the code below:

We’ve also set up some default styling parameters to make our plots more readable and professional-looking:

These settings configure the appearance of our plots with appropriate font sizes, line widths, and tick marks. The get_size() function helps us convert dimensions from centimeters to inches, which is useful when specifying figure sizes. With these preparations complete, we’re ready to create various types of visualizations to effectively display our data.

Matplotlib offers multiple levels of functionality for creating plots. Throughout this section, we’ll primarily focus on using commands that leverage default settings. This approach simplifies the process, as Matplotlib automatically handles much of the graph layout. These high-level commands are ideal for quickly creating effective visualizations without delving into intricate details. Later in this course, we’ll briefly touch upon more advanced techniques that provide greater control over plot elements and layout.

Basic Plotting

To create a basic line plot, use the following command:

plt.plot(x, y)

By default, this generates a line plot. However, you can customize the appearance by adjusting various parameters within the plot() function. For instance, you can modify it to resemble a scatter plot by changing certain arguments. The versatility of this command allows for a range of visual representations beyond simple line plots.

Let’s create a simple line plot of the sine function over the interval \([0, 4\pi]\). We’ll use NumPy to generate the x-values and calculate the corresponding y-values. The following code snippet demonstrates this process:

1x = np.linspace(0, 4.*np.pi, 100)
2y = np.sin(x)

3plt.figure(figsize=get_size(8,6))
4plt.plot(x, y)
5plt.tight_layout()
6plt.show()
1
Create an array of 100 values between 0 and 4\(\pi\).
2
Calculate the sine of each value in the array.
3
create a new figure with a size of (8,6) cm
4
plot the data
5
automatically adjust the layout
6
show the figure

Here is the code in a Python cell:

Try to change the values of the x and y arrays and see how the plot changes.

plt.tight_layout() is a very useful function in Matplotlib that automatically adjusts the spacing between plot elements to prevent overlapping and ensure that all elements fit within the figure area. Here’s what it does:

  1. Padding Adjustment: It adjusts the padding between and around subplots to prevent overlapping of axis labels, titles, and other elements.

  2. Subplot Spacing: It optimizes the space between multiple subplots in a figure.

  3. Text Accommodation: It ensures that all text elements (like titles, labels, and legends) fit within the figure without being cut off.

  4. Margin Adjustment: It adjusts the margins around the entire figure to make sure everything fits neatly.

  5. Automatic Resizing: If necessary, it can slightly resize subplot areas to accommodate all elements.

  6. Legend Positioning: It takes into account the presence and position of legends when adjusting layouts.

Key benefits of using plt.tight_layout():

  • It saves time in manual adjustment of plot elements.
  • It helps create more professional-looking and readable plots.
  • It’s particularly useful when creating figures with multiple subplots or when saving figures to files.

You typically call plt.tight_layout() just before plt.show() or plt.savefig(). For example:

plt.figure()
# ... (your plotting code here)
plt.tight_layout()
plt.show()

Customizing Plots

To enhance the clarity and interpretability of our plots, it’s crucial to provide context through proper labeling. The following commands add descriptive axis labels to our diagram:

plt.xlabel('x-label')
plt.ylabel('y-label')

Here’s an example of adding labels to our sine plot:

When plotting multiple datasets, it’s important to include a legend to identify each line. Use these commands:

plt.plot(..., label='Label name')
plt.legend(loc='lower left')

Here’s an example with a legend:

You can add multiple lines to the same plot:

You can customize the appearance of lines with additional parameters:

When plotting experimental data, it’s customary to include error bars that graphically indicate measurement uncertainty. The errorbar function can be used to display both vertical and horizontal error bars:

plt.errorbar(x, y, xerr=x_errors, yerr=y_errors, fmt='format', label='label')

Here’s an example of a plot with error bars:

We can visualize 2D arrays created with NumPy:

Saving Figures

To save a figure to a file, use the savefig method. Matplotlib supports multiple formats including PNG, JPG, EPS, SVG, PGF and PDF:

plt.savefig('filename.extension')

Here’s an example of creating and saving a figure:

For scientific papers, PDF format is recommended whenever possible. LaTeX documents compiled with pdflatex can include PDFs using the includegraphics command. PGF can also be a good alternative in some cases.

NumPy with Visualization

The arrays and calculations we’ve learned in NumPy form the foundation for scientific data visualization. In the next section, we’ll explore how to use Matplotlib to create visual representations of NumPy arrays, allowing us to interpret and communicate our physics results more effectively.

For example, we can visualize the planetary force calculations from our broadcasting example: