Using seaborn, how can I draw a line of my choice across my scatterplot?

后端 未结 2 1790
臣服心动
臣服心动 2021-02-19 03:40

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 ma

相关标签:
2条回答
  • 2021-02-19 04:02

    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)
    

    0 讨论(0)
  • 2021-02-19 04:04

    By creating a JointGrid in seaborn, you have created three axes, the main ax_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 other matplotlib Axes object.

    For example:

    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)
    

    As an aside, you can also access the marginal axes of a JointGrid in a similar way:

    grid.ax_marg_x.plot(...)
    grid.ax_marg_y.plot(...)
    
    0 讨论(0)
提交回复
热议问题