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")
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.
It appears that you have imported
matplotlib.pyplot
asplt
to obtainplt.scatter
in your code. You can just use the matplotlib functions to plot the line:By creating a
JointGrid
in seaborn, you have created three axes, the mainax_joint
, and the two marginal axes.To plot something else on the joint axes, we can access the joint grid using
grid.ax_joint
, and then create plot objects on there as you would with any othermatplotlib
Axes
object.For example:
As an aside, you can also access the marginal axes of a
JointGrid
in a similar way: