Home > Article > Backend Development > How to Plot Logarithmic Axes in Matplotlib?
When plotting graphs, it's often useful to have one axis plotted on a logarithmic scale. This is particularly relevant when dealing with data that spans several orders of magnitude. In this context, a logarithmic scale allows for a more compact and informative visualization.
To create a plot with one logarithmic axis using Matplotlib, you can utilize the Axes.set_yscale method. This method allows you to modify the scale after the Axes object has been created. It also provides you with the flexibility to create a control interface for users to select the desired scale.
To add a logarithmic scale to your plot, include the following line in your code:
<code class="python">ax.set_yscale('log')</code>
To revert to a linear scale, use 'linear' as the argument:
<code class="python">ax.set_yscale('linear')</code>
Here's a modified version of your sample code that incorporates the logarithmic scale:
<code class="python">import matplotlib.pyplot as plt a = [pow(10, i) for i in range(10)] fig = plt.figure() ax = fig.add_subplot(2, 1, 1) line, = ax.plot(a, color='blue', lw=2) ax.set_yscale('log') plt.show()</code>
This code will produce a graph with the values of the "y" axis plotted on a logarithmic scale.
The above is the detailed content of How to Plot Logarithmic Axes in Matplotlib?. For more information, please follow other related articles on the PHP Chinese website!