Matplotlib control which plot is on top

后端 未结 3 1576
长发绾君心
长发绾君心 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.

    0 讨论(0)
  • 2020-12-15 04:29

    Another solution besides using zorder, and worth knowing: You can simply plot a scatter of points using the plot command. Something like plot(series2_x, series2_y, ' o'). Note the ' o' with a space means no lines but circle points. This way the order of plotting them on the axes does put them on top.

    0 讨论(0)
  • 2020-12-15 04:43

    Use the zorder kwarg where the lower the zorder the further back the plot, e.g.

    plt.plot(series1_x, series1_y, zorder=1)
    plt.plot(series2_x, series2_y, zorder=2)
    plt.scatter(series2_x, series2_y, zorder=3)
    
    0 讨论(0)
提交回复
热议问题