【问题标题】:Draw a vertical line on seaborn jointplot在 seaborn 联合图上画一条垂直线
【发布时间】:2020-03-31 16:23:01
【问题描述】:

我正在尝试在 Seaborn 联合图上绘制一条垂直线并获得两个图,或者错误说明 ax is not iterable。逻辑如下:

a4_dims = (12, 4)
fig, ax = plt.subplots(figsize=a4_dims)
ax.set_xlim(-.75, 1.25)
ax.set_ylim(-.75,1.25)
plt.axvline(0)
sns.jointplot(x='1_3Movement',y='1_2Movement',data=dfm,kind='kde', xlim=(-.75, 1.25), ylim=(-.75,1.25))

这就是我得到的。

【问题讨论】:

    标签: matplotlib seaborn


    【解决方案1】:

    Seaborn 的 jointplot 创建了自己的图形和 3 个轴。 jointplot 返回一个 JointGrid 对象。您可以通过.ax_joint.ax_marg_x.ax_marg_y 获取各个轴。要在等高线图部分上画一条线,请使用.ax_joint

    联合图始终是二次图。 figsize 可以通过height= 设置(宽度相等)。

    from matplotlib import pyplot as plt
    import numpy as np
    import seaborn as sns
    
    kdeplot = sns.jointplot(x=np.random.normal(0.25, 0.5, 10), y=np.random.normal(0.25, 0.5, 10),
                              kind='kde', xlim=(-.75, 1.25), ylim=(-.75, 1.25), height=4)
    # draw a vertical line on the joint plot, optionally also on the x margin plot
    for ax in (kdeplot.ax_joint, kdeplot.ax_marg_x):
        ax.axvline(0, color='crimson', ls='--', lw=3)
    plt.show()
    

    【讨论】: