Matplotlib - Symmetrical Logarithmic and Logit Scales



Symmetrical Logarithmic Scale

The Symmetrical Logarithmic scale is similar to the logarithmic scale. It often abbreviated as symlog which is a type of scale used to represent data on an axis where the values are distributed symmetrically around zero using logarithmic intervals. It provides a logarithmic-like scale for both positive and negative values while accommodating zero.

To apply the Symmetrical Logarithmic scale on x-axis and y-axis, we have to use plt.xscale(‘symlog’) and plt.yscale(‘symlog’) respectively.

Characteristics of Symmetrical Logarithmic Scale

The symmetrical logarithmic scale has the following characteristics.

  • Symmetrical Behavior − Represents both positive and negative values logarithmically while handling zero.
  • Linear Near Zero − The scale is linear around zero within a specified range (linthresh) before transitioning to logarithmic behavior.

Parameters for Symmetrical Logarithmic Scale

linthresh − Linear threshold that determines the range around zero where the scale behaves linearly before transitioning to a logarithmic scale.

When to Use Symmetrical Logarithmic Scale:

  • Data around Zero − Suitable for datasets containing values centered around zero with a wide range of positive and negative values.
  • Avoiding Symmetry Bias − When symmetric representation of positive and negative values is needed without bias towards either side.

Importance of Symmetrical Logarithmic Scale

The Symmetrical Logarithmic Scale provides a logarithmic-like scale that accommodates both positive and negative values, making it useful for visualizing datasets with a balanced distribution around zero.

It also helps in highlighting smaller variations around zero while accommodating larger values without skewing the representation.

Plot with Symmetrical Logarithmic Scale

In this plot we are creating the symmetrical Logarithmic Scale on the y-axis by using the plt.yscale('symlog', linthresh=0.01).

Example

import matplotlib.pyplot as plt
import numpy as np
# Generating data for a sine wave with values around zero
x = np.linspace(-10, 10, 500)
y = np.sin(x)
# Creating a plot with a symmetrical logarithmic scale for the y-axis
plt.plot(x, y)
# Set symmetrical logarithmic scale for the y-axis
plt.yscale('symlog', linthresh=0.01)  
plt.xlabel('X-axis')
plt.ylabel('Y-axis (symlog scale)')
plt.title('Symmetrical Logarithmic Scale')
plt.show()
Output
Symmetric Log

Using a symmetrical logarithmic scale in Matplotlib allows for the visualization of datasets containing values around zero by enabling effective representation and analysis of symmetrically distributed data. Adjusting the linear threshold (linthresh) parameter is crucial to determine the range where the scale behaves linearly around zero before transitioning to a logarithmic scale.

Logit Scale

The Logit scale is a specialized type of scale used to represent data on an axis where the values are confined between 0 and 1. It's specifically designed for data that exists within this range commonly encountered in probabilities or values representing probabilities.

Setting the Scale

The plt.xscale() and plt.yscale() functions can be used to set the scale for the x-axis and y-axis respectively.

Characteristics of Logit Scale

The below are the characteristics of Logit Scale.

  • Constrains Data − Specifically used for data bounded between 0 and 1.
  • Transformation − Utilizes the logit function to map values from the standard logistic distribution.

When to Use Logit Scale

Probability Data − Suitable for visualizing probabilities or values representing probabilities, especially when dealing with logistic regression or logistic models.

Data within 0 to 1 Range − Specifically designed for data bounded within the 0 to 1 interval.

Importance of Logit Scale

  • The Logit Scale facilitates the visualization and analysis of data that represents probabilities or has a probabilistic interpretation.
  • It also helps in understanding and visualizing transformations of probability-related data.

Plot with the Logit Scale

In this plot we are creating the Logit scale on x-axis and y-axis.

Example

import matplotlib.pyplot as plt
import numpy as np
# Generating data within the 0 to 1 range
x = np.linspace(0.001, 0.999, 100)
y = np.log(x / (1 - x))
# Creating a plot with a logit scale for the x-axis
plt.plot(x, y)
plt.xscale('logit')  # Set logit scale for the x-axis
plt.xlabel('X-axis (logit scale)')
plt.ylabel('Y-axis')
plt.title('Logit Scale')
plt.show()
Output
Logit Scale

Understanding and choosing the appropriate scale for a plot is important for accurately representing the underlying data and ensuring that patterns and trends are effectively communicated in visualizations.

Plot yscale class linear, log, logit and symlog by name in Matplotlib library.

In this plot we are plotting the yscale class linear, log, logit and symlog by name.

Example

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
y = np.random.normal(loc=0.5, scale=0.4, size=1000)
y = y[(y > 0) & (y < 1)]
y.sort()
x = np.arange(len(y))

# linear
plt.subplot(221)
plt.plot(x, y)
plt.yscale('linear')
plt.title('linear')

# log
plt.subplot(222)
plt.plot(x, y)
plt.yscale('log')
plt.title('log')

# symmetric log
plt.subplot(223)
plt.plot(x, y - y.mean())
plt.yscale('symlog', linthresh=0.01)
plt.title('symlog')

# logit
plt.subplot(224)
plt.plot(x, y)
plt.yscale('logit')
plt.title('logit')
plt.show()
Output
Logit Scale
Advertisements