【问题标题】:How to draw a correlation line in a matplotlib scatter plot?如何在 matplotlib 散点图中绘制相关线?
【发布时间】:2019-08-03 09:42:17
【问题描述】:

下面是我要绘制的散点图的代码。绘制子图后,我想添加一条相关线。尽管代码运行没有错误,但我看不到图中绘制的任何线条。任何帮助将不胜感激。

    fig, ax = plt.subplots(figsize=fs)
    for g in np.unique(group):
        ix = np.where(group == g)
        sc = ax.scatter(y[ix], dosage[ix], color = cdict[g], edgecolors='none')
    # Fit with polyfit
    b, m = polyfit(y, dosage, 1)
    ax.plot(y, b + m * y, '-')
    ax.set_xlabel('LT2', fontsize = 16)
    ax.set_ylabel('Log Dosage', fontsize = 16)
    # ax.legend(scatterpoints = 1, title='DILI Risk')

    # fig.colorbar(sc, label='Dosage/mg/day')
    path_dose = 'figure/reports/images/{}_hist_dose.png'.format(name_rep)
    data['hist_dosage'].append(path_dose)
    plt.savefig(path_dose)
    plt.close()

下面是我得到的情节。

【问题讨论】:

  • 也许matplotlib.org/api/_as_gen/matplotlib.pyplot.xcorr.html 的文档在 matplotlib 中讨论 xcorr 会很有用
  • 无法访问您的数据,也无法查看您的情节,我们无法告诉您出了什么问题
  • 最好在您的问题中添加一个小型(虚构)数据集。

标签: python matplotlib correlation scatter-plot


【解决方案1】:

对不起,我的数据中有 nans。 np.polyfit() 一开始就不起作用,因此没有什么可绘制的。

【讨论】:

    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多