Syntax error How to visualize values on logarithmic scale on matplotalib?

How to visualize values on logarithmic scale on matplotalib?



To visualize values on logarithmic scale on matplotlib, we can use yscale('log').

Steps

  • Import matplotlib nd numpy.

  • Set the figure size and adjust the padding between and around the subplots.

  • Create x and y data points using numpy.

  • Use yscale('log') to visualize the values on a logarithmic scale.

  • Plot x and y data points using plot method.

  • Place a legend on the figure.

  • To display the figure, use show() method.

Example

import numpy as np
from matplotlib import pyplot as plt

# Set the figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# x and y data points
x = np.linspace(1, 100, 1000)
y = np.log(x)

# logarithmic scale
plt.yscale('log')

# Plot the x and y data points
plt.plot(x, y, c="red", lw=3, linestyle="dashdot", label="y=log(x)")

# Place the legend
plt.legend()

# Display the plot
plt.show()

Output

It will produce the following output −

Updated on: 2022-02-01T11:20:03+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements