Matplotlib Symmetric Log Plots

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Matplotlib is a data visualization library that is widely used in Python. It allows users to create a wide variety of visualizations, including line plots, scatter plots, bar charts, and more. In this lab, you will learn how to use the symlog axis scaling in Matplotlib to create symmetric log plots.

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlotCustomizationGroup(["`Plot Customization`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/PlotCustomizationGroup -.-> matplotlib/grid_config("`Grid Configuration`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills matplotlib/importing_matplotlib -.-> lab-48978{{"`Matplotlib Symmetric Log Plots`"}} matplotlib/figures_axes -.-> lab-48978{{"`Matplotlib Symmetric Log Plots`"}} matplotlib/line_plots -.-> lab-48978{{"`Matplotlib Symmetric Log Plots`"}} matplotlib/grid_config -.-> lab-48978{{"`Matplotlib Symmetric Log Plots`"}} python/tuples -.-> lab-48978{{"`Matplotlib Symmetric Log Plots`"}} python/importing_modules -.-> lab-48978{{"`Matplotlib Symmetric Log Plots`"}} python/numerical_computing -.-> lab-48978{{"`Matplotlib Symmetric Log Plots`"}} python/data_visualization -.-> lab-48978{{"`Matplotlib Symmetric Log Plots`"}} end

Import Libraries

Before we can begin, we need to import the necessary libraries. In this lab, we will be using Matplotlib and NumPy.

import matplotlib.pyplot as plt
import numpy as np

Generate Data

Next, we need to generate some data to plot. In this example, we will create three arrays: one for the x-axis values, one for the y-axis values in the first plot, and one for the y-axis values in the third plot.

dt = 0.01
x = np.arange(-50.0, 50.0, dt)
y1 = np.arange(0, 100.0, dt)
y3 = np.sin(x / 3.0)

Create Plots

Now that we have our data, we can create our plots. We will create three subplots, each with a different symlog axis scaling.

fig, (ax0, ax1, ax2) = plt.subplots(nrows=3)

Create Symlog Plot on x-axis

In the first subplot, we will create a symlog plot on the x-axis. We will also add a minor grid to the x-axis.

ax0.plot(x, y1)
ax0.set_xscale('symlog')
ax0.set_ylabel('symlogx')
ax0.grid()
ax0.xaxis.grid(which='minor')

Create Symlog Plot on y-axis

In the second subplot, we will create a symlog plot on the y-axis.

ax1.plot(y1, x)
ax1.set_yscale('symlog')
ax1.set_ylabel('symlogy')

Create Symlog Plot on both x-axis and y-axis

In the third subplot, we will create a symlog plot on both the x-axis and y-axis. We will also set the linthresh parameter to 0.015.

ax2.plot(x, y3)
ax2.set_xscale('symlog')
ax2.set_yscale('symlog', linthresh=0.015)
ax2.grid()
ax2.set_ylabel('symlog both')

Display the Plots

Finally, we can display our plots using the show() method.

plt.show()

Summary

In this lab, you learned how to use the symlog axis scaling in Matplotlib to create symmetric log plots. Specifically, you learned how to create a symlog plot on the x-axis, on the y-axis, and on both the x-axis and y-axis.

Other Python Tutorials you may like