logo_learn_stats

Methods to Get Axis Limits in Matplotlib (With Instance)

Posted on
banner 336x280

You’ll be able to significance please see syntax to get the axis limits for each the x-axis and y-axis of a plot in Matplotlib:

import matplotlib.pyplot as plt

#get x-axis and y-axis limits
xmin, xmax, ymin, ymax = plt.axis()

#print axis limits
print(xmin, xmax, ymin, ymax)

Refer to instance presentations how one can significance this syntax in follow.

banner 468x60

Instance: Methods to Get Axis Limits in Matplotlib

Assume we assemble please see scatterplot in Matplotlib:

import matplotlib.pyplot as plt

#outline x and y
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 5, 9, 15, 24, 39, 35, 35, 40, 41]

#assemble spray plot of x vs. y
plt.spray(x, y)

We will be able to significance please see syntax to get the axis limits for each the x-axis and y-axis of the scatterplot:

import matplotlib.pyplot as plt

#outline x and y
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 5, 9, 15, 24, 39, 35, 35, 40, 41]

#assemble spray plot of x vs. y
plt.spray(x, y)

#get x-axis and y-axis limits
xmin, xmax, ymin, ymax = plt.axis()

#print axis limits
print(xmin, xmax, ymin, ymax)

0.55 10.45 -1.0 43.0

From the output we will be able to see:

  • x-axis minimal: 0.55
  • x-axis most: 10.45
  • y-axis minimal: -1.0
  • y-axis most: 43.0

Those values fit the axis limits that may be detectable within the scatterplot above.

We will be able to additionally significance the annotate() serve as so as to add those axis limits as textual content values to the plot if we’d like:

import matplotlib.pyplot as plt

#outline x and y
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = [1, 5, 9, 15, 24, 39, 35, 35, 40, 41]

#assemble spray plot of x vs. y
plt.spray(x, y)

#get x-axis and y-axis limits
xmin, xmax, ymin, ymax = plt.axis()

#print axis limits
lims="xmin: " + str(spherical(xmin, 2)) + '\n' + \
       'xmax: ' + str(spherical(xmax, 2)) + '\n' + \
       'ymin: ' + str(spherical(ymin, 2)) + '\n' + \
       'ymax: ' + str(spherical(ymax, 2))

#upload axis limits to plan at (x,y) coordinate (1,35)
plt.annotate(lims, (1, 35))

Matplotlib get axis limits

Spare Assets

Refer to tutorials give an explanation for how one can carry out alternative habitual duties in Matplotlib:

Methods to All set Axis Ticks in Matplotlib
Methods to Building up Plot Measurement in Matplotlib
Methods to Upload Textual content to Matplotlib Plots

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *