Python – seaborn.jointplot() 方法
Seaborn是一个基于 matplotlib 的Python数据可视化库。它提供了一个用于绘制有吸引力和信息丰富的统计图形的高级界面。 Seaborn 帮助解决了 Matplotlib 面临的两大问题;问题是什么?
- 默认 Matplotlib 参数
- 使用数据框
随着 Seaborn 对 Matplotlib 的补充和扩展,学习曲线非常缓慢。如果您了解 Matplotlib,那么您已经完成了 Seaborn 的一半。
seaborn.jointplot() :
用双变量和单变量图绘制两个变量的图。这个函数为“JointGrid”类提供了一个方便的接口,有几种固定的绘图类型。这旨在成为一个相当轻量级的包装器;如果你需要更多的灵活性,你应该直接使用 :class:'JointGrid' 。
Syntax: seaborn.jointplot(x, y, data=None, kind=’scatter’, stat_func=None, color=None, height=6, ratio=5, space=0.2, dropna=True, xlim=None, ylim=None, joint_kws=None, marginal_kws=None, annot_kws=None, **kwargs)
Parameters: The description of some main parameters are given below:
x, y: These parameters take Data or names of variables in “data”.
data: (optional) This parameter take DataFrame when “x” and “y” are variable names.
kind: (optional) This parameter take Kind of plot to draw.
color: (optional) This parameter take Color used for the plot elements.
dropna: (optional) This parameter take boolean value, If True, remove observations that are missing from “x” and “y”.
Return: jointgrid object with the plot on it.
下面是上述方法的实现:
示例 1:
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("attention")
# draw jointplot with
# hex kind
sns.jointplot(x = "solutions", y = "score",
kind = "hex", data = data)
# show the plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("mpg")
# draw jointplot with
# scatter kind
sns.jointplot(x = "mpg", y = "acceleration",
kind = "scatter", data = data)
# show the plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("exercise")
# draw jointplot with
# kde kind
sns.jointplot(x = "id", y = "pulse",
kind = "kde", data = data)
# Show the plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("titanic")
# draw jointplot with
# reg kind
sns.jointplot(x = "age", y = "fare",
kind = "reg", data = data,
dropna = True)
# show the plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
输出:
示例 2:
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("mpg")
# draw jointplot with
# scatter kind
sns.jointplot(x = "mpg", y = "acceleration",
kind = "scatter", data = data)
# show the plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
输出:
示例 3:
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("exercise")
# draw jointplot with
# kde kind
sns.jointplot(x = "id", y = "pulse",
kind = "kde", data = data)
# Show the plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
输出:
示例 4:
Python3
# importing required packages
import seaborn as sns
import matplotlib.pyplot as plt
# loading dataset
data = sns.load_dataset("titanic")
# draw jointplot with
# reg kind
sns.jointplot(x = "age", y = "fare",
kind = "reg", data = data,
dropna = True)
# show the plot
plt.show()
# This code is contributed
# by Deepanshu Rustagi.
输出: