Plotting a horizontal line on multiple subplots in python using pyplot

前端 未结 2 1888
梦谈多话
梦谈多话 2020-12-14 02:23

I am plotting three subplots on the same page. I want to draw a horiZontal line through all the subplots. Following is my code and the resultant graph: (You can notice I can

相关标签:
2条回答
  • 2020-12-14 02:53

    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.

    0 讨论(0)
  • 2020-12-14 02:56

    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:

    enter image description here

    0 讨论(0)
提交回复
热议问题