首页 > 解决方案 > Matplotlib.pyplot:在 subplo 上绘制对角线

问题描述

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, sharey=True)
ax1.plot([-1, -1], [1, 1], linewidth=10, c="red")
sns.regplot(x=early_mean_zscore_untreated, y=late_mean_zscore_untreated, data=combo_untreated, ax=ax1, fit_reg=False)
sns.regplot(x=early_mean_zscore_treated, y=late_mean_zscore_treated, data=combo_treated, ax=ax2, fit_reg=False)
ax1.set(xlabel="Z-score for early PAs", ylabel="Z-score for late PAs")
ax2.set(xlabel="Z-score for early PAs", ylabel="Z-score for late PAs")
ax1.set(title="Resubmitted <= %d times" % resub_cutoff, aspect='equal')
ax2.set(title="Resubmitted > %d times" % resub_cutoff, aspect='equal')
fig.suptitle("Comparing improvement over the semester\nZ-scores")
ax1.plot([-1, -1], [1, 1], 'red', linewidth=10, )
plt.savefig("graphm.png")
plt.show()

我想在两个图的每一个上显示一条对角线,实际上是一条 45 度线。我找到了如何在轴上绘制水平线和垂直线,但我找不到如何绘制任意线。

标签: matplotlibseaborn

解决方案


老鹰眼的贡献者@ImportanceOfBeingEarnest 发现我创建这条线的参数不正确:

ax1.plot([-1, -1], [1, 1], 'red', linewidth=10)

我把它们写成第一对是起点的坐标,第二对是终点的坐标。实际上,第一个参数是 x 坐标列表,第二个参数是 y 坐标列表,所以我的线实际上是定义一个点。解决我的问题的正确方法是:

ax1.plot([-1,1],[-1,1], 'red', linewidth=10)

推荐阅读