📜  如何设置 Seaborn 图表的标题和字体?

📅  最后修改于: 2022-05-13 01:54:40.357000             🧑  作者: Mango

如何设置 Seaborn 图表的标题和字体?

在本文中,我们将看到如何在 seaborn 图表中设置标题和字体。数据可视化是以图形格式呈现数据。它对于数据分析非常重要,主要是因为以数据为中心的Python包的奇妙生态系统和 seaborn 是一个惊人的可视化库,用于在Python绘制统计图形。

安装:

对于Python环境:

pip install seaborn

对于 conda 环境:

conda install seaborn

让我们使用 seaborn 创建一些基本图:

Python3
# Importing libraries  
import numpy as np  
import seaborn as sns  
      
# Selecting style as white,  
# dark, whitegrid, darkgrid   
# or ticks  
sns.set( style = "white" )  
      
# Generate a random univariate   
# dataset  
rs = np.random.RandomState( 10 )  
d = rs.normal( size = 50 )  
      
# Plot a simple histogram and kde   
# with binsize determined automatically  
sns.distplot(d, kde = True, color = "g")


Python3
# Importing libraries  
import numpy as np  
import seaborn as sns  
      
# Selecting style as white,  
# dark, whitegrid, darkgrid   
# or ticks  
sns.set( style = "white" )  
      
# Generate a random univariate   
# dataset  
rs = np.random.RandomState( 10 )  
d = rs.normal( size = 50 )  
      
# Plot a simple histogram and kde   
# with binsize determined automatically  
sns.distplot(d, kde = True, color = "g").set_title('Seaborn')


Python3
# Importing libraries  
import numpy as np  
import seaborn as sns  
      
# Selecting style as white,  
# dark, whitegrid, darkgrid   
# or ticks  
sns.set( style = "white" )  
      
# Generate a random univariate   
# dataset  
rs = np.random.RandomState( 10 )  
d = rs.normal( size = 50 )  
      
# Plot a simple histogram and kde   
# with binsize determined automatically  
sns.distplot(d, kde = True, color = "g").set_title(
  'Seaborn', fontdict = { 'fontsize': 30})


Python3
# Importing libraries  
import numpy as np  
import seaborn as sns  
      
# Selecting style as white,  
# dark, whitegrid, darkgrid   
# or ticks  
sns.set( style = "white" )  
      
# Generate a random univariate   
# dataset  
rs = np.random.RandomState( 10 )  
d = rs.normal( size = 50 )  
      
# Plot a simple histogram and kde   
# with binsize determined automatically  
sns.distplot(d, kde = True, color = "g").set_title(
  'seaborn', fontdict= { 'fontsize': 30, 'verticalalignment': 'bottom'})


Python3
# Importing libraries  
import numpy as np  
import seaborn as sns      
      
# Selecting style as white,  
# dark, whitegrid, darkgrid   
# or ticks  
sns.set( style = "white" )  
      
# Generate a random univariate   
# dataset  
rs = np.random.RandomState( 10 )  
d = rs.normal( size = 50 )  
      
# Plot a simple histogram and kde   
# with binsize determined automatically  
sns.distplot(d, kde = True, color = "g").set_title(
  'seaborn', fontdict= { 'fontsize': 24, 'horizontalalignment': 'left'})


Python3
# Importing libraries
import numpy as np
import seaborn as sns
  
# Selecting style as white,
# dark, whitegrid, darkgrid
# or ticks
sns.set(style="white")
  
# Generate a random univariate
# dataset
rs = np.random.RandomState(10)
d = rs.normal(size=50)
  
# Plot a simple histogram and kde
# with binsize determined automatically
sns.distplot(d, kde=True, color="g").set_title(
  'seaborn', fontdict={'fontsize': 24, 'horizontalalignment': 'right'})


输出:

现在我们可以使用set_title()函数添加标题。此函数能够设置标题和字体样式。

示例 1:在 seaborn 图表中添加标题。

在这个例子中,我们将使用 set_title()函数设置标题。

代码:

蟒蛇3

# Importing libraries  
import numpy as np  
import seaborn as sns  
      
# Selecting style as white,  
# dark, whitegrid, darkgrid   
# or ticks  
sns.set( style = "white" )  
      
# Generate a random univariate   
# dataset  
rs = np.random.RandomState( 10 )  
d = rs.normal( size = 50 )  
      
# Plot a simple histogram and kde   
# with binsize determined automatically  
sns.distplot(d, kde = True, color = "g").set_title('Seaborn')

输出:

示例 2:增加标题的字体大小。

这里我们使用 fontsize 属性增加字体的大小。

代码:

蟒蛇3

# Importing libraries  
import numpy as np  
import seaborn as sns  
      
# Selecting style as white,  
# dark, whitegrid, darkgrid   
# or ticks  
sns.set( style = "white" )  
      
# Generate a random univariate   
# dataset  
rs = np.random.RandomState( 10 )  
d = rs.normal( size = 50 )  
      
# Plot a simple histogram and kde   
# with binsize determined automatically  
sns.distplot(d, kde = True, color = "g").set_title(
  'Seaborn', fontdict = { 'fontsize': 30})

输出:



示例 3:设置字体的垂直对齐方式。

在这里,我们使用verticalalignment属性设置字体的对齐方式。对齐的有效垂直对齐值;支持的值为“top”、“bottom”、“center”、“baseline”、“center_baseline”。

代码:

蟒蛇3

# Importing libraries  
import numpy as np  
import seaborn as sns  
      
# Selecting style as white,  
# dark, whitegrid, darkgrid   
# or ticks  
sns.set( style = "white" )  
      
# Generate a random univariate   
# dataset  
rs = np.random.RandomState( 10 )  
d = rs.normal( size = 50 )  
      
# Plot a simple histogram and kde   
# with binsize determined automatically  
sns.distplot(d, kde = True, color = "g").set_title(
  'seaborn', fontdict= { 'fontsize': 30, 'verticalalignment': 'bottom'})

输出:

示例 4:设置水平对齐。

在这里,我们使用水平对齐属性设置字体的对齐方式。有效的水平对齐方式 对齐的值;支持的值为 center'、'right'、'left'。



代码:

蟒蛇3

# Importing libraries  
import numpy as np  
import seaborn as sns      
      
# Selecting style as white,  
# dark, whitegrid, darkgrid   
# or ticks  
sns.set( style = "white" )  
      
# Generate a random univariate   
# dataset  
rs = np.random.RandomState( 10 )  
d = rs.normal( size = 50 )  
      
# Plot a simple histogram and kde   
# with binsize determined automatically  
sns.distplot(d, kde = True, color = "g").set_title(
  'seaborn', fontdict= { 'fontsize': 24, 'horizontalalignment': 'left'})

输出:

现在使用horizontalalignment': 'right' 检查右侧对齐:

蟒蛇3

# Importing libraries
import numpy as np
import seaborn as sns
  
# Selecting style as white,
# dark, whitegrid, darkgrid
# or ticks
sns.set(style="white")
  
# Generate a random univariate
# dataset
rs = np.random.RandomState(10)
d = rs.normal(size=50)
  
# Plot a simple histogram and kde
# with binsize determined automatically
sns.distplot(d, kde=True, color="g").set_title(
  'seaborn', fontdict={'fontsize': 24, 'horizontalalignment': 'right'})

输出: