avatarRizky Maulana Nurhidayat

Summary

The website content provides a tutorial on advanced Matplotlib techniques for data visualization in Python, focusing on adjusting colors, axes limits, and creating complex plots with grids.

Abstract

The provided text serves as a guide for users with beginner to intermediate knowledge of Matplotlib, a popular data visualization library in Python. It builds upon basic plotting by demonstrating how to enhance visualizations through color customization, setting axes limits to focus on relevant data segments, and utilizing grids for more complex, multi-plot figures. The tutorial emphasizes practical code examples, accompanied by explanatory figures, to illustrate the effects of different Matplotlib functions and parameters. It also encourages readers to explore further by offering to provide tutorials for even more complex plots upon request.

Opinions

  • The author believes that customizing colors is a fundamental aspect of creating visually appealing plots, offering a list of named colors provided by Matplotlib.
  • Adjusting axes limits is presented as a crucial step in highlighting the most pertinent parts of the data.
  • The use of grids is advocated for organizing complex data into multiple plots within a single figure, enhancing readability and comparison between datasets.
  • The author values the importance of interactive learning by suggesting readers to compare the effects of code changes on the resulting plots.
  • There is an underlying assumption that readers are interested in progressing from basic to more complex visualizations and that they are using Jupyter Notebook as their development environment.
  • The inclusion of a "Bonus" section with complex plots indicates the author's enthusiasm for showcasing the capabilities of Matplotlib and inspiring readers to experiment with advanced visualizations.

DESIGNING WITH MATPLOTLIB

Python Data Visualization with Matplotlib — For Absolute Beginner Part II

A guideline to adjust your plots in Matplotlib

Photo by Adeolu Eletu on Unsplash

We will learn about adjusting colors, axes limit, and making grids in matplotlib with Jupyter Notebook. If you have not read the last part, you can read via this link.

1. Adjusting colors

Talking about colors used in matplotlib, if you did not adjust the colors, matplotlib would generate your blue colors. You can use the following code to adjust the colors for the line plot we create in Part I.

plt.plot(x, np.sin(x), label = 'sin(x)', c = 'red')
plt.plot(x, np.cos(x), label = 'cos(x)', c = 'darkorchid')

Matplotlib will give you a plot like this.

The red line plot represents sin(x), whereas cos(x) is presented by the ‘dark orchid’ line. If you did not know the colors in matplotlib, here I attach it.

List of named colors from matplotlib

If you want to create scatter plots, you can use

plt.scatter(x, np.sin(x), label = 'sin(x)', c = 'red')
plt.scatter(x, np.cos(x), label = 'cos(x)', c = 'darkorchid', s = 10)

where the code ‘s’ means the size of the scatter plot. Here is the plot

The number of dots is 100, the same number as x. You can check it in Part I.

2. Adjusting axes limits

You can use this code to limit your axes.

plt.xlim(-1, 11)
plt.ylim(-1.5, 1.5)

Here is the result. Try to compare it with the previous plot.

3. Complex plot using grid

I will show the example of grid utilization in matplotlib

The code to create the grids is shown below

plt.figure(figsize=(9, 6))
plt.suptitle('Data Visualization with Matplotlib and Jupyter Notebook') # title for all plots
plt.subplot(2, 1, 1) # number of rows, columns, and the panel number
plt.plot(x, np.sin(x), label = 'sin(x)', c = 'red')
plt.legend(loc = 'best')
plt.xlim(-1, 12)
plt.ylim(-1.5, 1.5)
plt.subplot(2, 1, 2)
plt.scatter(x, np.cos(x), label = 'cos(x)', c = 'royalblue', s = 5)
plt.legend(loc = 'best')
plt.xlabel('Phase Angle (rad)')
plt.xlim(-1, 11)
plt.ylim(-1.5, 1.5)
plt.savefig('grids_matplotlib.png', dpi = 300)

First, as a standard procedure, we need to create a container. Then, we need to add the title for all the plots, as shown by line 2. After that, we should define the number of rows, columns, and the panel number for each plot. We will create two plots in similar rows. So, we define the number of rows is 2 and 1 for the columns. The sin plot will be plotted in panel 1 and cos plot in panel 2. Check the codes!

That’s all. If you need a tutorial for a more complex plot, please inform me.

Bonus

I will give you the complex plots I have generated.

Thanks!

Data Visualization
Python
Matplotlib
Jupyter Notebook
Beginner
Recommended from ReadMedium