# 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
``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's13    ax.set_aspect('equal') and ax.axis('equal') not working for 3D.1415    Input16      ax: a matplotlib axis, e.g., as output from plt.gca().17    '''1819    x_limits = ax.get_xlim3d()20    y_limits = ax.get_ylim3d()21    z_limits = ax.get_zlim3d()2223    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)2930    # The plot bounding box is a sphere in the sense of the infinity31    # norm, hence I call half the max range the plot radius.32    plot_radius = 0.5*max([x_range, y_range, z_range])3334    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])``
``20def square_axis(axes=None):21  """22  Expands the x- and y-limits on the given axes so that they are equal23  (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@staticmethod88def set_axes_equal(ax):89    """ Sets equal aspect ratio across the three axes of a 3D plot.9091    Contributed by Xuefeng Zhao.9293    :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 - radius100    upper_limits = centers + radius101    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 snaps106107    Parameters108    ----------109    X :110        space disctretization to get limits111    Y :112        solution to get limits113    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)119120    Returns121    -------122    ax123124    fig125126    time_text127        object to fill in text128129    """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) / 10135        uppery = nm.max(Y) + nm.max(Y) / 10136    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``