Matplotlib control which plot is on top

后端 未结 3 1577
长发绾君心
长发绾君心 2020-12-15 03:52

I am wondering if there is a way to control which plot lies on top of other plots if one makes multiple plots on one axis. An example:

As you can see, the g

3条回答
  •  一整个雨季
    2020-12-15 04:21

    Yes, you can. Just use zorder parameter. The higher the value, more on top the plot shall be.

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(series1_x, series1_y, zorder=3)
    
    ax2 = fig.add_subplot(111)
    ax2.plot(series2_x, series2_y, zorder=4)
    
    ax3 = fig.add_subplot(111)
    ax3.scatter(series2_x, series2_y, zorder=5)
    

    Alternatively, you can do line and marker plot at the same time. You can even set different colors for line and marker face.

    fig = plt.figure()
    ax1 = fig.add_subplot(111)
    ax1.plot(series1_x, series1_y)
    
    ax2 = fig.add_subplot(111)
    ax2.plot(series2_x, series2_y, '-o', color='b', mfc='k')
    

    The '-o' sets plot style to line and circle markers, color='b' sets line color to blue and mfc='k' sets the marker face color to black.

提交回复
热议问题