- Matplotlib Basics
- Matplotlib - Home
- Matplotlib - Introduction
- Matplotlib - Vs Seaborn
- Matplotlib - Environment Setup
- Matplotlib - Anaconda distribution
- Matplotlib - Jupyter Notebook
- Matplotlib - Pyplot API
- Matplotlib - Simple Plot
- Matplotlib - Saving Figures
- Matplotlib - Markers
- Matplotlib - Figures
- Matplotlib - Styles
- Matplotlib - Legends
- Matplotlib - Colors
- Matplotlib - Colormaps
- Matplotlib - Colormap Normalization
- Matplotlib - Choosing Colormaps
- Matplotlib - Colorbars
- Matplotlib - Text
- Matplotlib - Text properties
- Matplotlib - Subplot Titles
- Matplotlib - Images
- Matplotlib - Image Masking
- Matplotlib - Annotations
- Matplotlib - Arrows
- Matplotlib - Fonts
- Matplotlib - What are Fonts?
- Setting Font Properties Globally
- Matplotlib - Font Indexing
- Matplotlib - Font Properties
- Matplotlib - Scales
- Matplotlib - Linear and Logarthmic Scales
- Matplotlib - Symmetrical Logarithmic and Logit Scales
- Matplotlib - LaTeX
- Matplotlib - What is LaTeX?
- Matplotlib - LaTeX for Mathematical Expressions
- Matplotlib - LaTeX Text Formatting in Annotations
- Matplotlib - PostScript
- Enabling LaTex Rendering in Annotations
- Matplotlib - Mathematical Expressions
- Matplotlib - Animations
- Matplotlib - Artists
- Matplotlib - Styling with Cycler
- Matplotlib - Paths
- Matplotlib - Path Effects
- Matplotlib - Transforms
- Matplotlib - Ticks and Tick Labels
- Matplotlib - Radian Ticks
- Matplotlib - Dateticks
- Matplotlib - Tick Formatters
- Matplotlib - Tick Locators
- Matplotlib - Basic Units
- Matplotlib - Autoscaling
- Matplotlib - Reverse Axes
- Matplotlib - Logarithmic Axes
- Matplotlib - Symlog
- Matplotlib - Unit Handling
- Matplotlib - Ellipse with Units
- Matplotlib - Spines
- Matplotlib - Axis Ranges
- Matplotlib - Axis Scales
- Matplotlib - Axis Ticks
- Matplotlib - Formatting Axes
- Matplotlib - Axes Class
- Matplotlib - Twin Axes
- Matplotlib - Figure Class
- Matplotlib - Multiplots
- Matplotlib - Grids
- Matplotlib - Object-oriented Interface
- Matplotlib - PyLab module
- Matplotlib - Subplots() Function
- Matplotlib - Subplot2grid() Function
- Matplotlib - Anchored Artists
- Matplotlib - Manual Contour
- Matplotlib - Coords Report
- Matplotlib - AGG filter
- Matplotlib - Ribbon Box
- Matplotlib - Fill Spiral
- Matplotlib - Findobj Demo
- Matplotlib - Hyperlinks
- Matplotlib - Image Thumbnail
- Matplotlib - Plotting with Keywords
- Matplotlib - Create Logo
- Matplotlib - Multipage PDF
- Matplotlib - Multiprocessing
- Matplotlib - Print Stdout
- Matplotlib - Compound Path
- Matplotlib - Sankey Class
- Matplotlib - MRI with EEG
- Matplotlib - Stylesheets
- Matplotlib - Background Colors
- Matplotlib - Basemap
- Matplotlib Event Handling
- Matplotlib - Event Handling
- Matplotlib - Close Event
- Matplotlib - Mouse Move
- Matplotlib - Click Events
- Matplotlib - Scroll Event
- Matplotlib - Keypress Event
- Matplotlib - Pick Event
- Matplotlib - Looking Glass
- Matplotlib - Path Editor
- Matplotlib - Poly Editor
- Matplotlib - Timers
- Matplotlib - Viewlims
- Matplotlib - Zoom Window
- Matplotlib Plotting
- Matplotlib - Bar Graphs
- Matplotlib - Histogram
- Matplotlib - Pie Chart
- Matplotlib - Scatter Plot
- Matplotlib - Box Plot
- Matplotlib - Violin Plot
- Matplotlib - Contour Plot
- Matplotlib - 3D Plotting
- Matplotlib - 3D Contours
- Matplotlib - 3D Wireframe Plot
- Matplotlib - 3D Surface Plot
- Matplotlib - Quiver Plot
- Matplotlib Useful Resources
- Matplotlib - Quick Guide
- Matplotlib - Useful Resources
- Matplotlib - Discussion
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
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
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()