
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How can I get the length of a single unit on an axis in Matplotlib?
To get the length of a single unit on an axis in Matplotlib, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create x and y data points using numpy.
- Create a new figure or activate an existing figure using figure() method.
- Add an '~.axes.Axes' to the figure as part of a subplot arrangement.
- Plot x and y data points using plot() method.
- To get the single unit length, use transData transform.
- Print the horizontal and vertical lengths.
- To display the figure, use show() method.
Example
import numpy as np import matplotlib.pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.arange(0, 10, 0.005) y = np.exp(-x / 2.) * np.sin(2 * np.pi * x) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x, y) xy = ax.transData.transform([(0, 1), (1, 0)])\ -ax.transData.transform((0, 0)) print("Vertical length:", xy[0][1]) print("Horizontal length: ", xy[1][0]) plt.show()
Output
In addition to the plot, we will get the following output on the console −
Vertical length: 269.5 Horizontal length: 581.25
- Related Articles
- How to set the unit length of an axis in Matplotlib?
- How can I get the output of a Matplotlib plot as an SVG?
- How can I plot a single point in Matplotlib Python?
- How can I get pyplot images to show on a console app? (Matplotlib)
- How can I get the color of the last figure in Matplotlib?
- How I can get a Cartesian coordinate system in Matplotlib?
- How can I write unit tests against code that uses Matplotlib?
- Changing the color of a single X-axis tick label in Matplotlib
- How can I get the output of multiple MySQL tables from a single query?
- How can I place a table on a plot in Matplotlib?
- How can I get maximum and minimum values in a single MySQL query?
- How do I change matplotlib's subplot projection of an existing axis?
- How can I generate more colors on a pie chart in Matplotlib?
- How to append a single labeled tick to X-axis using matplotlib?
- Changing the color of an axis in Matplotlib

Advertisements