Introduction
This step-by-step tutorial will guide you through the process of creating plots with logarithmic axes using Python Matplotlib. This tutorial will cover the following topics:
- Semilogy Plot
- Semilogx Plot
- Loglog Plot
- Errorbars Plot
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.
Semilogy Plot
The semilogy plot is a plot with a logarithmic scale on the y-axis. It is useful for visualizing data that has a large range of values.
import matplotlib.pyplot as plt
import numpy as np
## Data for plotting
t = np.arange(0.01, 20.0, 0.01)
## Create figure
fig, ax1 = plt.subplots()
## Plot data on semilogy plot
ax1.semilogy(t, np.exp(-t / 5.0))
## Add title and grid to plot
ax1.set(title='Semilogy Plot')
ax1.grid()
## Display plot
plt.show()
Semilogx Plot
The semilogx plot is a plot with a logarithmic scale on the x-axis. It is useful for visualizing data that has a large range of values on the x-axis.
import matplotlib.pyplot as plt
import numpy as np
## Data for plotting
t = np.arange(0.01, 20.0, 0.01)
## Create figure
fig, ax2 = plt.subplots()
## Plot data on semilogx plot
ax2.semilogx(t, np.sin(2 * np.pi * t))
## Add title and grid to plot
ax2.set(title='Semilogx Plot')
ax2.grid()
## Display plot
plt.show()
Loglog Plot
The loglog plot is a plot with a logarithmic scale on both the x-axis and y-axis. It is useful for visualizing data that has a large range of values on both axes.
import matplotlib.pyplot as plt
import numpy as np
## Data for plotting
t = np.arange(0.01, 20.0, 0.01)
## Create figure
fig, ax3 = plt.subplots()
## Plot data on loglog plot
ax3.loglog(t, 20 * np.exp(-t / 10.0))
## Set x-axis scale to base 2
ax3.set_xscale('log', base=2)
## Add title and grid to plot
ax3.set(title='Loglog Plot')
ax3.grid()
## Display plot
plt.show()
Errorbars Plot
The errorbars plot is a plot that shows error bars for each data point. If a data point has a negative value, it will be clipped to 0.1.
import matplotlib.pyplot as plt
import numpy as np
## Data for plotting
x = 10.0**np.linspace(0.0, 2.0, 20)
y = x**2.0
## Create figure
fig, ax4 = plt.subplots()
## Set x-axis and y-axis to logarithmic scale
ax4.set_xscale("log", nonpositive='clip')
ax4.set_yscale("log", nonpositive='clip')
## Plot data with error bars
ax4.errorbar(x, y, xerr=0.1 * x, yerr=5.0 + 0.75 * y)
## Set title and y-axis limit
ax4.set(title='Errorbars Plot')
ax4.set_ylim(bottom=0.1)
## Display plot
plt.show()
Summary
Python Matplotlib is a powerful tool for creating data visualizations. This tutorial covered how to create plots with logarithmic axes using semilogy, semilogx, loglog, and errorbars plots. By using these types of plots, you can effectively visualize data that has a large range of values.