Matplotlib - Symlog



What is Symlog?

symlog is a scale in Matplotlib that combines both linear and logarithmic scaling by providing a way to plot data that includes both positive and negative values while accommodating a wide range of magnitudes.

Characteristics of Symlog Scale

The below are the characteristics of Symlog scale. Let’s go through one by one in detail.

Linear Near Zero

In the context of the symlog scale in Matplotlib library linear near zero refers to the behavior of the scale around the zero point. The symlog scale combines linear and logarithmic scaling particularly in the vicinity of zero to allow for a more nuanced representation of data.

Linear Behavior Near Zero

Close to Zero − For values close to zero i.e. within a defined range around zero the symlog scale behaves linearly and similar to a linear scale.

Linear Threshold (linthresh) − The linthresh parameter defines the range around zero where the scale behaves linearly. Values within this threshold are represented linearly.

Linear Region − Within the specified threshold around zero the symlog scale behaves like a typical linear scale preserving the direct relationship between data values and their representation on the plot.

Example

In this example we are setting the linthresh parameter as 0.1 to range around zero (-0.1 to 0.1 in this case) behaves linearly. Values within this range are represented linearly on the plot by allowing for a more focused and precise visualization of data close to zero while accommodating larger values away from zero logarithmically.

import matplotlib.pyplot as plt
import numpy as np
# Sample data with positive and negative values
x = np.linspace(-10, 10, 100)
y = np.sinh(x)  # Hyperbolic sine function for demonstration
# Creating a plot with symlog scale on y-axis
plt.plot(x, y)
plt.yscale('symlog', linthresh=0.1)  # Set y-axis to symlog scale with a linear threshold of 0.1
plt.xlabel('X-axis')
plt.ylabel('Y-axis (Symlog Scale)')
plt.title('Plot with Symlog Scale (Linear Near Zero)')
plt.show()
Output
Linear Near Zero

The use cases of Linear near zero

Focus on Small Changes near Zero − Allows better representation of small changes or variations near zero without losing information about larger values.

Handling Data with Zero-Centered Patterns − Useful for datasets where patterns or changes are centered around zero.

Symmetric logarithmic scaling

Symmetric logarithmic scaling is often referred to as symlog scaling in Matplotlib which is a scaling method that combines both linear and logarithmic scales to represent data symmetrically around zero. This scale is particularly useful when dealing with datasets that contain values spanning positive and negative ranges and require a nuanced representation across a wide range of values.

Characteristics of Symlog Scaling

Symmetric Behavior − Symlog scaling maintains symmetry around zero,accommodating both positive and negative values.

Linear Region Near Zero − Close to zero within a defined range around zero the scale behaves linearly by preserving the direct proportionality of values and their representation on the plot.

Logarithmic Scaling Away from Zero − As values move away from zero i.e. both positive and negative the scale transitions into a logarithmic scale allowing representation of larger absolute values logarithmically.

Linear Threshold (linthresh) − The linthresh parameter defines the range around zero where the scale behaves linearly.

Example

In this example the symlog scale with a linthresh of 0.1 specifies that values within the range of -0.1 to 0.1 (around zero) are represented linearly on the x-axis.

import matplotlib.pyplot as plt
import numpy as np
# Sample data with positive and negative values
x = np.linspace(-10, 10, 100)
y = np.sinh(x)  # Hyperbolic sine function for demonstration
# Creating a plot with symlog scale on y-axis
plt.plot(x, y)
plt.xscale('symlog', linthresh=0.1)  # Set x-axis to symlog scale with a linear threshold of 0.1
plt.xlabel('X-axis')
plt.ylabel('Y-axis (Symlog Scale)')
plt.title('Plot with Symlog Scale')
plt.show()

Output

Symmetric Log Scale

Use Cases

Handling Data with Both Positive and Negative Values − Useful for datasets spanning positive and negative ranges.

Focused Visualization − This allows to focus on small changes near zero while accommodating larger values away from zero logarithmically.

Logarithmic away from zero

Logarithmic away from zero refers to the behavior of a logarithmic scale in a plot particularly in the context of the symlog scale in Matplotlib.

The below are the characteristics of the Logarithmic away from zero.

Near Zero − Close to zero within a specified range around zero which is defined by the linthresh parameter. The scale behaves linearly by preserving a direct relationship between the plotted values and their representation on the plot.

Away from Zero − As the values move further away from zero both positively and negatively the scale transitions into a logarithmic scaling behavior.

Logarithmic Behavior − The scale represents larger absolute values logarithmically using powers of a base value i.e. usually 10 or e to visually compress the range of these larger values.

Compression of Values − Larger absolute values away from zero are compressed on the plot due to the logarithmic scaling making it easier to visualize a wide range of values efficiently.

Example

In this example we are setting the linthresh parameters as 0.2 and 0.1 for xscale and yscale repectively.

import matplotlib.pyplot as plt
import numpy as np
# Sample data with positive and negative values
x = np.linspace(-10, 10, 100)
y = np.sinh(x)  # Hyperbolic sine function for demonstration
# Creating a plot with symlog scale on y-axis
plt.plot(x, y)
plt.xscale('symlog', linthresh=0.2)  # Set x-axis to symlog scale with a linear threshold of 0.2
plt.yscale('symlog', linthresh=0.1)  # Set x-axis to symlog scale with a linear threshold of 0.1
plt.xlabel('X-axis')
plt.ylabel('Y-axis (Symlog Scale)')
plt.title('Plot with Symlog Scale')
plt.show()

Output

Logarthmic Away Zero
Advertisements