4 examples of 'matplotlib axis equal' in Python

Every line of 'matplotlib axis equal' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
10def set_axes_equal(ax):
11 '''Make axes of 3D plot have equal scale so that spheres appear as spheres,
12 cubes as cubes, etc.. This is one possible solution to Matplotlib's
13 ax.set_aspect('equal') and ax.axis('equal') not working for 3D.
14
15 Input
16 ax: a matplotlib axis, e.g., as output from plt.gca().
17 '''
18
19 x_limits = ax.get_xlim3d()
20 y_limits = ax.get_ylim3d()
21 z_limits = ax.get_zlim3d()
22
23 x_range = abs(x_limits[1] - x_limits[0])
24 x_middle = np.mean(x_limits)
25 y_range = abs(y_limits[1] - y_limits[0])
26 y_middle = np.mean(y_limits)
27 z_range = abs(z_limits[1] - z_limits[0])
28 z_middle = np.mean(z_limits)
29
30 # The plot bounding box is a sphere in the sense of the infinity
31 # norm, hence I call half the max range the plot radius.
32 plot_radius = 0.5*max([x_range, y_range, z_range])
33
34 ax.set_xlim3d([x_middle - plot_radius, x_middle + plot_radius])
35 ax.set_ylim3d([y_middle - plot_radius, y_middle + plot_radius])
36 ax.set_zlim3d([z_middle - plot_radius, z_middle + plot_radius])
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

20def square_axis(axes=None):
21 """
22 Expands the x- and y-limits on the given axes so that they are equal
23 (defaults to the current axes).
24 """
25 if axes is None:
26 axes = gca()
27 tmpv = axes.axis()
28 xmax = max([tmpv[1], tmpv[3]])
29 xmin = min([tmpv[0], tmpv[2]])
30 axes.axis([xmin, xmax, xmin, xmax])
87@staticmethod
88def set_axes_equal(ax):
89 """ Sets equal aspect ratio across the three axes of a 3D plot.
90
91 Contributed by Xuefeng Zhao.
92
93 :param ax: a Matplotlib axis, e.g., as output from plt.gca().
94 """
95 bounds = [ax.get_xlim3d(), ax.get_ylim3d(), ax.get_zlim3d()]
96 ranges = [abs(bound[1] - bound[0]) for bound in bounds]
97 centers = [np.mean(bound) for bound in bounds]
98 radius = 0.5 * max(ranges)
99 lower_limits = centers - radius
100 upper_limits = centers + radius
101 ax.set_xlim3d([lower_limits[0], upper_limits[0]])
102 ax.set_ylim3d([lower_limits[1], upper_limits[1]])
103 ax.set_zlim3d([lower_limits[2], upper_limits[2]])
104def setup_axis(X, Y, ax=None, fig=None, ylims=None):
105 """Setup axis, including timer for animation or snaps
106
107 Parameters
108 ----------
109 X :
110 space disctretization to get limits
111 Y :
112 solution to get limits
113 ax :
114 ax where to put everything, if None current axes are used (Default value = None)
115 fig :
116 fig where to put everything, if None current figure is used (Default value = None)
117 ylims :
118 custom ylims, if None y axis limits are calculated from Y (Default value = None)
119
120 Returns
121 -------
122 ax
123
124 fig
125
126 time_text
127 object to fill in text
128
129 """
130 if ax is None:
131 fig = plt.gcf()
132 ax = plt.gca()
133 if ylims is None:
134 lowery = nm.min(Y) - nm.min(Y) / 10
135 uppery = nm.max(Y) + nm.max(Y) / 10
136 else:
137 lowery = ylims[0]
138 uppery = ylims[1]
139 ax.set_ylim(lowery, uppery)
140 ax.set_xlim(X[0], X[-1])
141 time_text = ax.text(X[0] + nm.sign(X[0]) * X[0] / 10,
142 uppery - uppery / 10,
143 'empty', fontsize=15)
144 return ax, fig, time_text

Related snippets