📜  Python – seaborn.FacetGrid() 方法

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

Python – seaborn.FacetGrid() 方法

先决条件: Seaborn 编程基础

Seaborn 是一个基于 matplotlib 的Python数据可视化库。它提供了一个用于绘制有吸引力和信息丰富的统计图形的高级界面。 Seaborn 帮助解决了 Matplotlib 面临的两大问题;问题是什么?

  • 默认 Matplotlib 参数
  • 使用数据框

随着 Seaborn 对 Matplotlib 的补充和扩展,学习曲线非常缓慢。如果您了解 Matplotlib,那么您已经完成了 Seaborn 的一半。

seaborn.FacetGrid() :

  • FacetGrid 类有助于可视化一个变量的分布以及使用多个面板分别在数据集子集中的多个变量之间的关系。
  • 一个 FacetGrid 最多可以绘制三个维度?行、列和色相。前两个与生成的轴数组有明显的对应关系;将色调变量视为沿深度轴的第三个维度,其中不同的级别用不同的颜色绘制。
  • FacetGrid 对象将数据框作为输入以及将形成网格的行、列或色调维度的变量的名称。变量应该是分类的,变量每个级别的数据将用于沿该轴的一个方面。
seaborn.FacetGrid( data, \*\*kwargs)



Seaborn.FacetGrid 使用许多参数作为输入,下面以表格的形式描述其中的主要参数:

Argument                              DescriptionValue                                                                                                  
dataTidy (“long-form”) dataframe where each column is a variable and each row is an observation.    DataFrame
row, col, hueVariables that define subsets of the data, which will be drawn on separate facets in the grid. See the “*_order“ parameters to control the order of levels of this variable.strings
paletteColors to use for the different levels of the “hue“ variable.palette name, list, or dict, optional

下面是上述方法的实现:

示例 1:

Python3
# importing packages
import seaborn
import matplotlib.pyplot as plt
  
# loading of a dataframe from seaborn
df = seaborn.load_dataset('tips')
  
############# Main Section         #############
# Form a facetgrid using columns with a hue
graph = seaborn.FacetGrid(df, col ="sex",  hue ="day")
# map the above form facetgrid with some attributes
graph.map(plt.scatter, "total_bill", "tip", edgecolor ="w").add_legend()
# show the object
plt.show()
  
# This code is contributed by Deepanshu Rustagi.


Python3
# importing packages
import seaborn
import matplotlib.pyplot as plt
  
# loading of a dataframe from seaborn
df = seaborn.load_dataset('tips')
  
############# Main Section         #############
# Form a facetgrid using columns with a hue
graph = seaborn.FacetGrid(df, row ='smoker', col ='time')
# map the above form facetgrid with some attributes
graph.map(plt.hist, 'total_bill', bins = 15, color ='orange')
# show the object
plt.show()
  
# This code is contributed by Deepanshu Rustagi.


Python3
# importing packages
import seaborn
import matplotlib.pyplot as plt
  
# loading of a dataframe from seaborn
df = seaborn.load_dataset('tips')
  
############# Main Section         #############
# Form a facetgrid using columns with a hue
graph = seaborn.FacetGrid(df, col ='time', hue ='smoker')
# map the above form facetgrid with some attributes
graph.map(seaborn.regplot, "total_bill", "tip").add_legend()
# show the object
plt.show()
  
# This code is contributed by Deepanshu Rustagi.



输出 :

示例 2:

Python3

# importing packages
import seaborn
import matplotlib.pyplot as plt
  
# loading of a dataframe from seaborn
df = seaborn.load_dataset('tips')
  
############# Main Section         #############
# Form a facetgrid using columns with a hue
graph = seaborn.FacetGrid(df, row ='smoker', col ='time')
# map the above form facetgrid with some attributes
graph.map(plt.hist, 'total_bill', bins = 15, color ='orange')
# show the object
plt.show()
  
# This code is contributed by Deepanshu Rustagi.


输出 :

示例 3:

Python3

# importing packages
import seaborn
import matplotlib.pyplot as plt
  
# loading of a dataframe from seaborn
df = seaborn.load_dataset('tips')
  
############# Main Section         #############
# Form a facetgrid using columns with a hue
graph = seaborn.FacetGrid(df, col ='time', hue ='smoker')
# map the above form facetgrid with some attributes
graph.map(seaborn.regplot, "total_bill", "tip").add_legend()
# show the object
plt.show()
  
# This code is contributed by Deepanshu Rustagi.


输出 :