Matplotlib – Linear and Logarthmic Scales


Matplotlib – Linear and Logarthmic Scales



”;


What are Scales?

In Matplotlib library scales refer to the mapping of data values to the physical dimensions of a plot. They determine how data values are represented and visualized along the axes of a plot. Matplotlib supports various types of scales and the choice of scale can significantly impact how the data is perceived in visualization.

Common Types of Scales in Matplotlib

The below are the common types of scales available in matplotlib library.







Sr.No Scale & Usage
1

Linear Scale

Suitable for most numerical data without large variations in magnitude.

2

Logarithmic Scale

Ideal for datasets covering several orders of magnitude or exhibiting exponential growth.

3

Symmetrical Logarithmic Scale

Suitable for datasets with both positive and negative values>.

4

Logit Scale

Specifically used for data bounded between 0 and 1.

Linear Scale

The linear scale is the default scale used to represent data along axes in a plot. It”s a straightforward mapping where the data values are plotted in direct proportion to their actual numerical values. In a linear scale equal distances along the axis represent equal differences in the data.

Characteristics of Linear Scale

  • Equal Intervals − In a linear scale equal distances on the axis correspond to equal differences in data values.
  • Linear Mapping − The relationship between data values and their position on the axis is linear.

Using Linear Scale

By default the Matplotlib library uses a linear scale for both the x-axis and y-axis. To explicitly set a linear scale we don”t need to use any specific function as it”s the default behavior. However we can specify it explicitly using plt.xscale(”linear”) or plt.yscale(”linear”) for the x-axis or y-axis respectively.

The following is the example of applying the linear scale to a plot.

Example


import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.xlabel(''X-axis'')
plt.ylabel(''Y-axis'')
plt.title(''Linear Scale'')
plt.show()

Output


Linear Scale

When to Use Linear Scale

  • Linear scales are commonly used when the data doesn”t have exponential growth or when the range of values isn”t too large.
  • It”s suitable for representing most numerical data that doesn”t exhibit significant nonlinear behavior.

Logarithmic Scale

The Logarithmic scale represents data using a logarithmic mapping. This is useful when there is a wide range of values and the logarithmic scale helps to emphasize changes in smaller values.

Characteristics of Logarithmic Scale

The below are the characteristics of the logarithmic scale.

Equal Ratios

In a logarithmic scale, equal distances on the axis represent equal ratios between values rather than equal differences.

Compression of Data

It compresses a wide range of data into a more readable and interpretable visualization.

Emphasizes Smaller Values

It emphasizes changes in smaller values more than larger ones.

Using Logarithmic Scale

To use a logarithmic scale we have to specify plt.xscale(”log”) or plt.yscale(”log”) for the x-axis or y-axis respectively. Logarithmic scales are particularly useful for visualizing exponential growth or phenomena that cover several orders of magnitude.

When to Use Logarithmic Scale

  • Logarithmic scales are suitable for data with large variations in magnitude or when there”s a need to highlight changes in smaller values.
  • Commonly used in fields like finance (stock prices), scientific research (decibel levels, earthquake magnitudes) and biology (pH levels).

The following is the example plot with the logarithmic scale.

Example


import matplotlib.pyplot as plt
import numpy as np
# Generating logarithmically spaced data
x = np.linspace(1, 10, 100)
y = np.log(x)
# Creating a plot with a logarithmic scale for the x-axis
plt.plot(x, y)
plt.xscale(''log'')  # Set logarithmic scale for the x-axis
plt.xlabel(''X-axis (log scale)'')
plt.ylabel(''Y-axis'')
plt.title(''Logarithmic Scale'')
plt.show()

Output


Logarithmic Scale

Using a logarithmic scale in a plot can provide insights into data with a wide range of values making it easier to visualize patterns and trends across different scales within the same plot.

Logarithmic plot of a cumulative distribution function

This example shows the Logarithmic plot of a cumulative distribution function.

Example


import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
N = 100
data = np.random.randn(N)
X2 = np.sort(data)
F2 = np.array(range(N))/float(N)
plt.plot(X2, F2)
plt.xscale(''log'')
plt.yscale(''log'')
plt.show()

Output


cummulative_log

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *