如何在 Seaborn 图中设置轴标签和限制?
在本文中,我们将学习如何在 Seaborn 图中设置轴标签和限制。让我们先讨论一些概念。
- 轴是图中包含数据空间的区域。 Axes 包含两个或三个轴(在 3D 的情况下)对象,它们负责处理数据限制。
- 轴标签是根据含义、单位和方向描述轴值的标签。
- Axes Limits 是对轴值的限制,用于过滤轴上所需的值。
在这里,在本文中,内容来自一次设置轴标签、轴限制和两者。最后,您将能够学习如何在 Seaborn 图中设置轴标签和限制。
设置轴标签
方法 1:要设置 seaborn 图中的轴标签,我们使用Python matplotlib 库中的 matplotlib.axes.Axes.set()函数。
Syntax: Axes.set(self, xlabel, ylabel, fontdict=None, labelpad=None, **kwargs)
Parameters:
- xlabel : str- The label text for the x-axis.
- ylabel : str- The label text for the y-axis.
- labelpad : scalar, optional, default: None
- **kwargs : Text properties
Returns: It will change the x-axis and y-axis labels.
示例:在此示例中,我们将使用单个 matplotlib.axes.Axes.set()函数并通过一次调用该函数更改标签的标签,我们将一次性传递 xlabel 和 ylabel 参数,这将改变用户情节。
Python3
# import seaborn
import seaborn as sns
sns.set_style("whitegrid")
# import data
tips = sns.load_dataset("tips")
# plot boxplot
gfg = sns.boxplot(x ="day", y ="total_bill", data = tips)
# add label to the axis and label to the plot
gfg.set(xlabel ="GFG X", ylabel = "GFG Y", title ='some title')
Python3
# import seaborn
import seaborn as sns
sns.set_style("whitegrid")
# load data
tips = sns.load_dataset("tips")
# plot boxplot
gfg = sns.boxplot(x ="day", y ="total_bill", data = tips)
# This will add title to plot
gfg.set_title( "GFG - GFG")
# This will add label to X-axis
gfg.set_xlabel( "GFG X")
# This will add label to Y-axis
gfg.set_ylabel( "GFG Y")
Python3
# import packages
import matplotlib.pyplot as plt
import seaborn as sns
# create data
data = [3, 7, 9, 11, 12, 14, 15, 16, 18, 19, 20, 23, 25, 28]
# plot distplot
fig, ax = plt.subplots()
sns.distplot(data, ax = ax)
# change the limits of X-axis
ax.set_xlim(1, 70)
plt.show()
Python3
# import packages
import matplotlib.pyplot as plt
import seaborn as sns
# create data
data = [3, 7, 9, 11, 12, 14, 15, 16, 18, 19, 20, 23, 25, 28]
# plot distplot
fig, ax = plt.subplots()
sns.distplot(data, ax = ax)
# This will change the limits of the x-axis
ax.set_xlim(1, 70)
# This will add label to the X-axis
ax.set_xlabel( "GFG X")
# This will add label to the Y-axis
ax.set_ylabel( "GFG Y")
# This will add title to the plot
ax.set_title( "GFG - GFG")
plt.show()
输出:
方法 2:要设置 seaborn 图中的轴标签,我们使用Python matplotlib 库中的 matplotlib.axes.Axes.set_ylabel() 和 matplotlib.axes.Axes.set_xlabel()函数。
Syntax:
Axes.set_xlabel(self, xlabel, fontdict=None, labelpad=None, **kwargs)
Axes.set_ylabel(self, xlabel, fontdict=None, labelpad=None, **kwargs)
Parameters: This method accepts the following parameters.
- xlabel : This parameter is the label text.
- labelpad : This parameter is the spacing in points from the axes bounding box including ticks and tick labels.
Returns:This method does not return any value.
示例:在本示例中,我们将分别使用 matplotlib.axes.Axes.set_ylabel() 和 matplotlib.axes.Axes.set_xlabel()函数,并将标签名称作为参数传递来更改绘图的标签。
输入:
蟒蛇3
# import seaborn
import seaborn as sns
sns.set_style("whitegrid")
# load data
tips = sns.load_dataset("tips")
# plot boxplot
gfg = sns.boxplot(x ="day", y ="total_bill", data = tips)
# This will add title to plot
gfg.set_title( "GFG - GFG")
# This will add label to X-axis
gfg.set_xlabel( "GFG X")
# This will add label to Y-axis
gfg.set_ylabel( "GFG Y")
输出:
设置轴限制
更改限制的功能:-
- matplotlib.axes.Axes.set_xlim()函数:matplotlib 库的axes 模块用于设置x 轴视图限制。
- matplotlib.axes.Axes.set_ylim()函数:matplotlib 库的axes 模块用于设置y 轴视图限制。
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 parameters 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.
例子:
蟒蛇3
# import packages
import matplotlib.pyplot as plt
import seaborn as sns
# create data
data = [3, 7, 9, 11, 12, 14, 15, 16, 18, 19, 20, 23, 25, 28]
# plot distplot
fig, ax = plt.subplots()
sns.distplot(data, ax = ax)
# change the limits of X-axis
ax.set_xlim(1, 70)
plt.show()
输出:
设置轴标签和轴限制
在这个特定的例子中,我们将在Python库的合适的所需函数的帮助下,在单个代码中更改绘图的标签和限制。
蟒蛇3
# import packages
import matplotlib.pyplot as plt
import seaborn as sns
# create data
data = [3, 7, 9, 11, 12, 14, 15, 16, 18, 19, 20, 23, 25, 28]
# plot distplot
fig, ax = plt.subplots()
sns.distplot(data, ax = ax)
# This will change the limits of the x-axis
ax.set_xlim(1, 70)
# This will add label to the X-axis
ax.set_xlabel( "GFG X")
# This will add label to the Y-axis
ax.set_ylabel( "GFG Y")
# This will add title to the plot
ax.set_title( "GFG - GFG")
plt.show()
输出: