使用seaborn,我如何在散布图上划定一条线? [英] Using seaborn, how can I draw a line of my choice across my scatterplot?
问题描述
我希望能够在 seaborn 中生成的图中绘制一条我的规范线.我选择的图是 JointGrid,但任何散点图都可以.我怀疑seaborn可能不容易做到这一点?
I want to be able to draw a line of my specification across a plot generated in seaborn. The plot I chose was JointGrid, but any scatterplot will do. I suspect that seaborn maybe doesn't make it easy to do this?
这是绘制数据的代码(来自花瓣长度和花瓣宽度的鸢尾花数据集的数据框):
Here is the code plotting the data (dataframes from the Iris dataset of petal length and petal width):
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")
如果您从 iris 数据集中获取此图,我该如何在其上画一条我选择的线?例如,一条负斜率线可能将集群分开,而正斜率线可能会穿过它们.
If you take this graph from the iris dataset, how can I draw a line of my choice across it? For example, a line of negative slope might separate the clusters, and positive slope might run across them.
推荐答案
看来您已经将 matplotlib.pyplot
导入为 plt
以获得 plt.scatter
.您可以使用 matplotlib 函数来绘制线条:
It appears that you have imported matplotlib.pyplot
as plt
to obtain plt.scatter
in your code. You can just use the matplotlib functions to plot the line:
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)
这篇关于使用seaborn,我如何在散布图上划定一条线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!