如何设置 Seaborn 图表图形大小?
Seaborn 是一个基于 matplotlib 的Python数据可视化库。它用于绘制有吸引力且信息丰富的统计图形。要调整 seaborn 图的图形大小,我们将使用 matplotlib.pyplot 的 subplots函数。
matplotlib.pyplot.subplots() 创建一个图窗和一组子图。它有一个名为 figsize 的参数,它接受一个元组作为参数,其中包含绘图的高度和宽度。它返回图形和轴数组。
在调用 seaborn 图时,我们将在设置所需图的尺寸后将 ax 参数设置为等于由 matplotlib.pyplot.subplots 返回的轴数组。
示例 1:我们将考虑两个学生并将他们的分数绘制在条形图中,我们将设置大小为 ( 4,5 ) 的图。
Python3
# Importing libraries
import seaborn as sns
import matplotlib.pyplot as plt
# Setting the data
x = ["Student1", "Student2"]
y = [70, 87]
# setting the dimensions of the plot
fig, ax = plt.subplots(figsize=(4, 5))
# drawing the plot
sns.barplot(x, y, ax=ax)
plt.show()
Python3
# Importing libraries
import seaborn as sns
import matplotlib.pyplot as plt
# Setting the data
x = ["Student1", "Student2"]
y = [80, 68]
# setting the dimensions of the plot
fig, ax = plt.subplots(figsize=(6, 6))
# drawing the plot
sns.barplot(x, y, ax=ax)
plt.show()
Python3
# Importing libraries
import seaborn as sns
import matplotlib.pyplot as plt
# Setting the data
x = ["Student1", "Student2"]
y = [70, 87]
# setting the dimensions of the plot
fig, ax = plt.subplots(figsize=(40, 5))
# drawing the plot
sns.boxplot(x = y)
plt.show()
输出:
示例 2:我们将绘制大小为 (6, 6) 的图。
蟒蛇3
# Importing libraries
import seaborn as sns
import matplotlib.pyplot as plt
# Setting the data
x = ["Student1", "Student2"]
y = [80, 68]
# setting the dimensions of the plot
fig, ax = plt.subplots(figsize=(6, 6))
# drawing the plot
sns.barplot(x, y, ax=ax)
plt.show()
示例 3:在此示例中,我们将创建箱线图并使用 figsize 设置图表的大小。
蟒蛇3
# Importing libraries
import seaborn as sns
import matplotlib.pyplot as plt
# Setting the data
x = ["Student1", "Student2"]
y = [70, 87]
# setting the dimensions of the plot
fig, ax = plt.subplots(figsize=(40, 5))
# drawing the plot
sns.boxplot(x = y)
plt.show()
输出: