📜  Seaborn 中的关系图 – 第二部分

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

Seaborn 中的关系图 – 第二部分

先决条件:Seaborn 中的关系图 - 第一部分
在本文的前一部分中,我们了解了 relplot()。现在,我们将阅读 seaborn 库中提供的另外两个关系图,即 scatterplot() 和 lineplot()。这两个图也可以在 relplot() 中的 kind 参数的帮助下绘制。基本上 relplot(),默认情况下,只给我们 scatterplot(),如果我们传递参数kind = “line” ,它给我们 lineplot()。

示例 1:使用 relplot() 可视化提示数据集

Python3
import seaborn as sns
sns.set(style ="ticks")
  
tips = sns.load_dataset('tips')
sns.relplot(x ="total_bill", y ="tip", data = tips)


Python3
import seaborn as sns
 
 
sns.set(style ="ticks")
tips = sns.load_dataset('tips')
 
sns.relplot(x ="total_bill",
            y ="tip",
            kind ="scatter",
            data = tips)


Python3
import seaborn as sns
 
 
sns.set(style ="ticks")
tips = sns.load_dataset('tips')
 
sns.relplot(x ="total_bill",
            y ="tip",
            kind ="line",
            data = tips)


Python3
import seaborn as sns
 
 
sns.set(style ="ticks")
tips = sns.load_dataset('tips')
markers = {"Lunch": "s", "Dinner": "X"}
 
ax = sns.scatterplot(x ="total_bill",
                     y ="tip",
                     style ="time",
                     markers = markers,
                     data = tips)


Python3
import seaborn as sns
 
 
iris = sns.load_dataset("iris")
 
sns.scatterplot(x = iris.sepal_length,
                y = iris.sepal_width,
                hue = iris.species,
                style = iris.species)


Python3
import seaborn as sns
 
 
sns.set(style = 'whitegrid')
fmri = sns.load_dataset("fmri")
 
sns.lineplot(x ="timepoint",
             y ="signal",
             data = fmri)


Python3
import seaborn as sns
 
 
sns.set(style = 'whitegrid')
fmri = sns.load_dataset("fmri")
 
sns.lineplot(x ="timepoint",
             y ="signal",
             hue ="region",
             style ="event",
             data = fmri)


Python3
import seaborn as sns
 
 
sns.set(style = 'whitegrid')
dots = sns.load_dataset("dots").query("align == 'dots'")
 
sns.lineplot(x ="time",
             y ="firing_rate",
             hue ="coherence",
             style ="choice",
             data = dots)


输出 :

示例 2:将 relplot() 与 kind=”scatter” 一起使用。

Python3

import seaborn as sns
 
 
sns.set(style ="ticks")
tips = sns.load_dataset('tips')
 
sns.relplot(x ="total_bill",
            y ="tip",
            kind ="scatter",
            data = tips)

输出 :

示例 3:将 relplot() 与 kind=”line” 一起使用。

Python3

import seaborn as sns
 
 
sns.set(style ="ticks")
tips = sns.load_dataset('tips')
 
sns.relplot(x ="total_bill",
            y ="tip",
            kind ="line",
            data = tips)

输出 :

虽然这两个图都可以使用 relplot() 绘制,但 seaborn 也有单独的函数来可视化这些图。与 relplot() 相比,这些函数还提供了一些其他功能。让我们更详细地讨论这些函数:

Seaborn.scatterplot()

散点图是统计可视化的支柱。它使用点云描述了两个变量的联合分布,其中每个点代表数据集中的一个观察值。这种描述允许眼睛推断出大量关于它们之间是否存在任何有意义关系的信息。

句法 :

seaborn.scatterplot(x=None, y=None, data=None, **kwargs)

参数 :

ParameterValueUse
x, ynumericInput data variables
dataDataframeDataset that is being used.
hue, size, stylename in data; optionalGrouping variable that will produce elements with different colors.
palettename, list, or dict; optionalColors to use for the different levels of the hue variable.
hue_orderlist; optionalSpecified order for the appearance of the hue variable levels.
hue_normtuple or Normalize object; optionalNormalization in data units for colormap applied to the hue variable when it is numeric.
sizeslist, dict, or tuple; optionaldetermines the size of each point in the plot.
size_orderlist; optionalSpecified order for appearance of the size variable levels
size_normtuple or Normalize object; optionalNormalization in data units for scaling plot objects when the size variable is numeric.
markersboolean, list, or dictionary; optionalobject determining the shape of marker for each data points.
style_orderlist; optionalSpecified order for appearance of the style variable levels
alphafloatproportional opacity of the points.
legend“brief”, “full”, or False; optionalIf “brief”, numeric hue and size variables will be represented with a sample of evenly spaced values. If “full”, every group will get an entry in the legend. If False, no legend data is added and no legend is drawn.
axmatplotlib axes; optionalAxes object in which the plot is to be drawn.
kwargskey, value pairingsOther keyword arguments are passed through to the underlying plotting function.

示例 1:使用标记绘制散点图以区分人们访问餐厅的时间。

Python3

import seaborn as sns
 
 
sns.set(style ="ticks")
tips = sns.load_dataset('tips')
markers = {"Lunch": "s", "Dinner": "X"}
 
ax = sns.scatterplot(x ="total_bill",
                     y ="tip",
                     style ="time",
                     markers = markers,
                     data = tips)

输出:

示例 2:在数据框中传递数据向量而不是名称。

Python3

import seaborn as sns
 
 
iris = sns.load_dataset("iris")
 
sns.scatterplot(x = iris.sepal_length,
                y = iris.sepal_width,
                hue = iris.species,
                style = iris.species)

输出:

Seaborn.lineplot()

散点图非常有效,但没有普遍最优的可视化类型。对于某些数据集,您可能希望将变化视为一个变量的时间函数,或类似的连续变量。在这种情况下,绘制线图是更好的选择。

句法 :

seaborn.lineplot(x=None, y=None, data=None, **kwargs)

参数 :

ParameterValueUse
x, ynumericInput data variables
dataDataframeDataset that is being used.
hue, size, stylename in data; optionalGrouping variable that will produce elements with different colors.
palettename, list, or dict; optionalColors to use for the different levels of the hue variable.
hue_orderlist; optionalSpecified order for the appearance of the hue variable levels.
hue_normtuple or Normalize object; optionalNormalization in data units for colormap applied to the hue variable when it is numeric.
sizeslist, dict, or tuple; optionaldetermines the size of each point in the plot.
size_orderlist; optionalSpecified order for appearance of the size variable levels
size_normtuple or Normalize object; optionalNormalization in data units for scaling plot objects when the size variable is numeric.
markers, dashesboolean, list, or dictionary; optionalobject determining the shape of marker for each data points.
style_orderlist; optionalSpecified order for appearance of the style variable levels
unitslong_form_varGrouping variable identifying sampling units. When used, a separate line with correct terminology will be drawn for each unit but no legend entry will be inserted. Useful for displaying experimental replicate distribution when exact identities are not necessary.
estimatorname of pandas method or callable or None; optionalMethod for aggregating the vector y at the same x point through multiple observations. If None, all observations will be drawn.
ciint or “sd” or None; optionalSize of the confidence interval to be drawn when aggregating with an estimator. “sd” means drawing a standard deviation.
n_bootint; optional>Number of bootstraps to use for confidence interval measurement.
seedint, numpy.random.Generator, or numpy.random.RandomState; optionalSeed or random number generator for reproducible bootstrapping.
sortbool; optionalis True, sorts the data.
err_style“band” or “bars”; optionalEither using translucent error bands to display the confidence intervals or discrete error bars.
err_kwsdict of keyword argumentsAdditional parameters to control the aesthetics of the error bars.
legend“brief”, “full”, or False; optionalIf “brief”, numeric hue and size variables will be represented with a sample of evenly spaced values. If “full”, every group will get an entry in the legend. If False, no legend data is added and no legend is drawn.
axmatplotlib axes; optionalAxes object in which the plot is to be drawn.
kwargskey, value pairingsOther keyword arguments are passed through to the underlying plotting function.

示例 1:使用 lineplot() 对“fmri”数据集进行基本可视化

Python3

import seaborn as sns
 
 
sns.set(style = 'whitegrid')
fmri = sns.load_dataset("fmri")
 
sns.lineplot(x ="timepoint",
             y ="signal",
             data = fmri)

输出 :

示例 2:根据类别对数据点进行分组,此处为区域和事件。

Python3

import seaborn as sns
 
 
sns.set(style = 'whitegrid')
fmri = sns.load_dataset("fmri")
 
sns.lineplot(x ="timepoint",
             y ="signal",
             hue ="region",
             style ="event",
             data = fmri)

输出 :

示例 3:可视化“点”数据集的复杂绘图,以展示 seaborn 的强大功能。在此,在此示例中,使用了定量颜色映射。

Python3

import seaborn as sns
 
 
sns.set(style = 'whitegrid')
dots = sns.load_dataset("dots").query("align == 'dots'")
 
sns.lineplot(x ="time",
             y ="firing_rate",
             hue ="coherence",
             style ="choice",
             data = dots)

输出 :