use*_*339 10 python matplotlib seaborn
我希望能够在seaborn中生成的情节中画出我的规格线.我选择的情节是JointGrid,但任何散点图都可以.我怀疑seaborn可能不容易做到这一点?
以下是绘制数据的代码(来自花瓣长度和花瓣宽度的Iris数据集的数据帧):
import seaborn as sns
iris = sns.load_dataset("iris")
grid = sns.JointGrid(iris.petal_length, iris.petal_width, space=0, size=6, ratio=50)
grid.plot_joint(plt.scatter, color="g")
Run Code Online (Sandbox Code Playgroud)
如果从虹膜数据集中获取此图形,我如何在其中绘制我选择的线条?例如,负斜率线可能会将簇分开,并且正斜率可能会跨越它们.
cht*_*mon 17
看来,您已经导入matplotlib.pyplot为plt获得plt.scatter在你的代码.您可以使用matplotlib函数绘制线条:
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")
grid = sns.JointGrid(iris.petal_length, iris.petal_width, space=0, size=6, ratio=50)
grid.plot_joint(plt.scatter, color="g")
plt.plot([0, 4], [1.5, 0], linewidth=2)
Run Code Online (Sandbox Code Playgroud)
通过创建JointGridin seaborn,您已经创建了三个轴,即主轴ax_joint和两个边缘轴。
要在关节轴上绘制其他内容,我们可以使用来访问关节网格grid.ax_joint,然后像在其他matplotlib Axes对象上一样在此处创建绘图对象。
例如:
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")
grid = sns.JointGrid(iris.petal_length, iris.petal_width, space=0, size=6, ratio=50)
# Create your scatter plot
grid.plot_joint(plt.scatter, color="g")
# Create your line plot.
grid.ax_joint.plot([0,4], [1.5,0], 'b-', linewidth = 2)
Run Code Online (Sandbox Code Playgroud)
JointGrid顺便说一句,您也可以通过类似的方式来访问a的边缘轴:
grid.ax_marg_x.plot(...)
grid.ax_marg_y.plot(...)
Run Code Online (Sandbox Code Playgroud)