Plotting a horizontal line on multiple subplots in python using pyplot

陌路散爱 提交于 2019-11-28 20:31:30

I found a way to do it for anyone who stumbles on this anyways.

We need to replace the following line from the OP:

plt.axhline(y=0.002, xmin=0, xmax=1, hold=None)

We replace it with:

ax1.axhline(y=0.002,xmin=0,xmax=3,c="blue",linewidth=0.5,zorder=0)
ax2.axhline(y=0.002,xmin=0,xmax=3,c="blue",linewidth=0.5,zorder=0)
ax3.axhline(y=0.002,xmin=0,xmax=3,c="blue",linewidth=0.5,zorder=0)

This produces:

Since you have defined ax1, ax2 and ax3, it is easy to draw horizontal lines on them. You need to do it separately for them. But your code could be simplified:

for ax in [ax1, ax2, ax3]:
    ax.axhline(y=0.002, c="blue",linewidth=0.5,zorder=0)

According to axhline documentation, xmin and xmax should be in the range (0,1). There is no chance that xmax=3.0. Since your intent is to draw horizontal line across the axes (which is the default behavior of axhline method ), you can just omit the xmin and xmax parameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!