Ax axis equal python

matplotlib.pyplot.axis#

Convenience method to get or set some axis properties.

xmin, xmax, ymin, ymax = axis() xmin, xmax, ymin, ymax = axis([xmin, xmax, ymin, ymax]) xmin, xmax, ymin, ymax = axis(option) xmin, xmax, ymin, ymax = axis(**kwargs) 

The axis limits to be set. This can also be achieved using

ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax)) 

If a bool, turns axis lines and labels on or off. If a string, possible values are:

Turn on axis lines and labels. Same as True .

Turn off axis lines and labels. Same as False .

Set equal scaling (i.e., make circles circular) by changing axis limits. This is the same as ax.set_aspect(‘equal’, adjustable=’datalim’) . Explicit data limits may not be respected in this case.

Set equal scaling (i.e., make circles circular) by changing dimensions of the plot box. This is the same as ax.set_aspect(‘equal’, adjustable=’box’, anchor=’C’) . Additionally, further autoscaling will be disabled.

Set limits just large enough to show all data, then disable further autoscaling.

Automatic scaling (fill plot box with data).

‘scaled’ with axis limits equal to data limits.

Square plot; similar to ‘scaled’, but initially forcing xmax-xmin == ymax-ymin .

Whether observers are notified of the axis limit change. This option is passed on to set_xlim and set_ylim .

Returns : xmin, xmax, ymin, ymax float

Источник

Equal axis aspect ratio#

How to set and adjust plots with equal axis aspect ratios.

Читайте также:  Cookie удаление cookie php

not equal, looks like ellipse, equal, looks like circle, still a circle, even after changing limits, still a circle, auto-adjusted data limits

import matplotlib.pyplot as plt import numpy as np # Plot circle of radius 3. an = np.linspace(0, 2 * np.pi, 100) fig, axs = plt.subplots(2, 2) axs[0, 0].plot(3 * np.cos(an), 3 * np.sin(an)) axs[0, 0].set_title('not equal, looks like ellipse', fontsize=10) axs[0, 1].plot(3 * np.cos(an), 3 * np.sin(an)) axs[0, 1].axis('equal') axs[0, 1].set_title('equal, looks like circle', fontsize=10) axs[1, 0].plot(3 * np.cos(an), 3 * np.sin(an)) axs[1, 0].axis('equal') axs[1, 0].set(xlim=(-3, 3), ylim=(-3, 3)) axs[1, 0].set_title('still a circle, even after changing limits', fontsize=10) axs[1, 1].plot(3 * np.cos(an), 3 * np.sin(an)) axs[1, 1].set_aspect('equal', 'box') axs[1, 1].set_title('still a circle, auto-adjusted data limits', fontsize=10) fig.tight_layout() plt.show() 

Источник

matplotlib.axes.Axes.axis#

Convenience method to get or set some axis properties.

xmin, xmax, ymin, ymax = axis() xmin, xmax, ymin, ymax = axis([xmin, xmax, ymin, ymax]) xmin, xmax, ymin, ymax = axis(option) xmin, xmax, ymin, ymax = axis(**kwargs) 

The axis limits to be set. This can also be achieved using

ax.set(xlim=(xmin, xmax), ylim=(ymin, ymax)) 

If a bool, turns axis lines and labels on or off. If a string, possible values are:

Turn on axis lines and labels. Same as True .

Turn off axis lines and labels. Same as False .

Set equal scaling (i.e., make circles circular) by changing axis limits. This is the same as ax.set_aspect(‘equal’, adjustable=’datalim’) . Explicit data limits may not be respected in this case.

Set equal scaling (i.e., make circles circular) by changing dimensions of the plot box. This is the same as ax.set_aspect(‘equal’, adjustable=’box’, anchor=’C’) . Additionally, further autoscaling will be disabled.

Читайте также:  Php enable module debian

Set limits just large enough to show all data, then disable further autoscaling.

Automatic scaling (fill plot box with data).

‘scaled’ with axis limits equal to data limits.

Square plot; similar to ‘scaled’, but initially forcing xmax-xmin == ymax-ymin .

Whether observers are notified of the axis limit change. This option is passed on to set_xlim and set_ylim .

Returns : xmin, xmax, ymin, ymax float

Источник

Make a Square Plot With Equal Axes in Matplotlib

Make a Square Plot With Equal Axes in Matplotlib

  1. set_aspect() to Make a Square Plot With Equal Axes
  2. axis() Method to Generate Square Plot

We can set the aspect ratio of a plot using the set_aspect() method to make it a square plot and axis() method can also be used to make a square plot with equal axes in Matplotlib.

set_aspect() to Make a Square Plot With Equal Axes

We can set the aspect ratio using matplotlib.axes.Axes.set_aspect() function. If we use «equal» as an aspect ratio in the function, we get a plot with the same scaling from data points to plot units for X-axis and Y-axis.

import numpy as np import matplotlib.pyplot as plt  x = np.linspace(-3,3,100) y = np.sin(x)  fig = plt.figure() ax = fig.add_subplot(111)  plt.plot(x, y) plt.xlim(-3,3) plt.ylim(-3,3)  ax.set_aspect('equal', adjustable='box')  plt.xlabel("x") plt.ylabel("sinx")  plt.show() 

square plot with equal axes using set_aspect to equal

plt.xlim(-3,3) plt.ylim(-3,3)  ax.set_aspect('equal') 

It sets both X-axis and Y-axis to have the same range. Then ax.set_aspect(‘equal’) sets both axes to be equal.

The above method only yields a square plot when ranges for both axes are set to be the same. To generate a square plot in the general case, we have to manually set the aspect ratio using the following command:

axes.set_aspect(1./axes.get_data_ratio()) 

axes.get_data_ratio() gets the ratio of the raw plot data. The value of its reciprocal is passed to set_aspect() to make axes equal without setting the limits of axes manually.

import numpy as np import matplotlib.pyplot as plt  x = np.linspace(-3,3,100) y = np.sin(x)  fig = plt.figure() ax = fig.add_subplot(111)  plt.plot(x, y)  ax.set_aspect(1.0/ax.get_data_ratio(), adjustable='box')  plt.xlabel("x") plt.ylabel("sinx") plt.show() 

square plot with equal axes using set_aspect general

axis() Method to Generate Square Plot

If we pass «square» as an argument to matplotlib.pyplot.axis() , it creates a square plot where the ranges for both axes occupy are equal to the length in plot.

import numpy as np import matplotlib.pyplot as plt  x=np.linspace(-3,3,100) y=np.sin(x) plt.plot(x, y) plt.axis('square') plt.xlabel("x") plt.ylabel("sinx") plt.show() 

square plot with equal axes using axis methodl

The range of axes in this solution are selected automatically. It is equal to ax.set_aspect(‘equal’, adjustable=’datalim’) . The explicit axis limits set by the user are not respected.

Читайте также:  Выполнение строки кода javascript

Suraj Joshi is a backend software engineer at Matrice.ai.

Related Article — Matplotlib Axes

Copyright © 2023. All right reserved

Источник

Оцените статью