Thursday, December 26, 2024
Google search engine
HomeLanguagesHow to change axes limits in Seaborn?

How to change axes limits in Seaborn?

Seaborn is a Python data visualization library based on matplotlib. It provides a high-level interface for drawing attractive and informative statistical graphics. Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. There are two methods available in the Axes module to change the limits:

  1. matplotlib.axes.Axes.set_xlim(): Axes module of matplotlib library is used to set the x-axis view limits.
  2. matplotlib.axes.Axes.set_ylim(): Axes module of matplotlib library is used to set the y-axis view limits.

Syntax:

Axes.set_xlim(self, left=None, right=None, emit=True, auto=False, *, xmin=None, xmax=None)

Axes.set_ylim(self, bottom=None, top=None, emit=True, auto=False, *, ymin=None, ymax=None)

Parameters:

  • bottom: This parameter is the bottom xlim/ylim in data coordinates
  • top: This parameter is the top xlim/ylim in data coordinates
  • emit: This parameter is used to notify observers of limit change.
  • auto: This parameter is used to turn on autoscaling of the x-axis/y-axis.
  • xmin,xmax,ymin, ymax: These parametersthe are equivalent to bottom and top and it is an error to pass both xmin/ymin and bottom or xmax/ymax and top.

Returns:-

   bottom, top: This returns the new x-axis/y-axis limits in data coordinates.

Example 1:

Python3




# Import module
import matplotlib.pyplot as plt
import seaborn as sns
 
# assign data
data = [3, 7, 9, 11, 12, 14,
        15, 16, 18, 19, 20,
        23, 25, 28]
 
# depict visualization
fig, ax = plt.subplots()
sns.distplot(data, ax=ax)
ax.set_xlim(1, 70)
plt.show()


Output:

Example 2:

Python3




# import module
import seaborn as sns
sns.set_style("whitegrid")
 
# assign dataset
tips = sns.load_dataset("tips")
 
# depict visualization
gfg = sns.boxplot(x="day", y="total_bill",
                  data=tips)
gfg.set_ylim(0, 80)


Output:

RELATED ARTICLES

Most Popular

Recent Comments