为了绘制一个数据集中二元变量的分布,可以使用jointplot(),这个函数能够产生一个多面板的图像,在图像上包括两个变量之间的关系,在单独的坐标中还绘制出了各个变量的分布。

    绘制分布图的函数用法如下:

    1. seaborn.jointplot(x, y, data=None, kind='scatter',
    2. stat_func=<function pearsonr>,
    3. color=None, size=6, ratio=5,
    4. space=0.2, dropna=True, xlim=None,
    5. ylim=None, joint_kws=None,
    6. marginal_kws=None,
    7. annot_kws=None, **kwargs)

    (1)特殊参数:
    kind : 图类型,值为{ “scatter” | “reg” | “resid” | “kde” | “hex” }之一,分别对应为散点图、回归图、残差图、核密度估计、六角图。
    (2)基本参数
    (1)
    color : 颜色。参数类型: matplotlib 颜色
    (2)
    size : 图的尺度大小(正方形),默认为 6。参数类型:数值型。
    (3)
    ratio : 中心图与侧边图的比例,越大、中心图占比越大。参数类型:数值型。
    (4)
    space : 中心图与侧边图的间隔大小。参数类型:数值型。
    (5) s:散点的大小(只针对散点图scatter),参数类型:数值型。
    (6) linewidth:散点边缘线的宽度(只针对散点图scatter),参数类型:数值型。
    (7) edgecolor:散点的边界颜色,默认无色,可以重叠(只针对散点图scatter),参数类型:matplotlib 颜色。
    (8) {x, y}lim:x、y轴的范围。参数类型:二元组
    (9) {joint, marginal, annot}_kws:中心图、侧边图与统计注释的信息。例如:joint_kws=dict(s=80,edgecolor=’g’), marginal_kws=dict(bins=15,rug=True,color=’g’), annot_kws=dict(stat=’r’,fontsize=15)。
    plot()是Matplotlib画图的最主要方法,Series和DataFrame都有plot方法。plot()默认生成是曲线图,可以通过kind参数生成其它的图形,可选的值为:line、bar、 barh、kde、scatter、等。对于坐标类数据,可以用散点图来查看它们的分布趋势和是否有离群点的存在。
    使用Matplotlib库的plot()方法做散点图,数据为萼片的长和宽

    1. import pandas as pd # 导入pandas库
    2. import warnings # 载入当前版本seaborn库时会有警告出现,先载入warnings,忽略警告
    3. import seaborn as sns # 导入seaborn库
    4. import matplotlib.pyplot as plt # 导入matplotlib库
    5. warnings.filterwarnings("ignore") # 忽略警告
    6. sns.set(style="white", color_codes=True) # 确定主题为white
    7. iris = pd.read_csv("iris.csv") # 读取csv数据并转为Pandas的 DataFrame格式
    8. iris.plot(kind="scatter",
    9. x="SepalLengthCm", y="SepalWidthCm")
    10. plt.show()

    image.png

    利用matplotlib作散点图

    使用seaborn库的FacetGrid()方法做分类散点图,可以将不同分类数据点用不同颜色进行标注,数据为萼片的长和宽。

    1. import pandas as pd # 导入pandas库
    2. import warnings # 载入当前版本seaborn库时会有警告出现,先载入warnings,忽略警告
    3. import seaborn as sns # 导入seaborn库
    4. import matplotlib.pyplot as plt # 导入matplotlib库
    5. warnings.filterwarnings("ignore") # 忽略警告
    6. sns.set(style="white", color_codes=True) # 确定主题为white
    7. iris = pd.read_csv("iris.csv") # 读取csv数据并转为Pandas的 DataFrame格式
    8. sns.FacetGrid(iris, hue="Species", size=5).map(plt.scatter,
    9. "SepalLengthCm",
    10. "SepalWidthCm").add_legend()
    11. plt.show()

    Figure_1.png
    用seaborn做分类散点图

    使用seaborn库的jointplot ()方法做散点图,数据为萼片的长和宽。
    Figure_2.pngFigure_3.png
    seaborn库的jointplot ()方法做散点图

    使用seaborn库的jointplot ()方法做回归图和六角图(如图10.40所示),数据为萼片的长和宽。

    1. import pandas as pd # 导入pandas库
    2. import warnings # 载入当前版本seaborn库时会有警告出现,先载入warnings,忽略警告
    3. import seaborn as sns # 导入seaborn库
    4. import matplotlib.pyplot as plt # 导入matplotlib库
    5. warnings.filterwarnings("ignore") # 忽略警告
    6. sns.set(style="white", color_codes=True) # 确定主题为white
    7. iris = pd.read_csv("iris.csv") # 读取csv数据并转为Pandas的 DataFrame格式
    8. sns.jointplot(x="SepalLengthCm", y="SepalWidthCm",
    9. data=iris, size=5, kind="reg") #回归图
    10. plt.show()
    11. sns.jointplot("SepalLengthCm", y="SepalWidthCm",
    12. data=iris, size=5, kind="hex") #六角图
    13. plt.show()

    Figure_4.pngFigure_5.png
    回归图和六角图

    使用Seaborn库的jointplot ()方法做KDE 图和散点图+KDE 图,数据为萼片的长和宽

    1. import pandas as pd # 导入pandas库
    2. import warnings # 载入当前版本seaborn库时会有警告出现,先载入warnings,忽略警告
    3. import seaborn as sns # 导入seaborn库
    4. import matplotlib.pyplot as plt # 导入matplotlib库
    5. warnings.filterwarnings("ignore") # 忽略警告
    6. sns.set(style="white", color_codes=True) # 确定主题为white
    7. iris = pd.read_csv("iris.csv") # 读取csv数据并转为Pandas的 DataFrame格式
    8. sns.jointplot("SepalLengthCm", y="SepalWidthCm",
    9. data=iris, size=5, kind="kde")
    10. plt.show()
    11. sns.jointplot("SepalLengthCm", y="SepalWidthCm",
    12. data=iris, size=5).plot_joint(sns.kdeplot, zorder=0, n_levels=6)
    13. plt.show()

    Figure_6.pngFigure_7.png
    KDE 图和散点图+KDE 图