Matplotlib: keep grid lines behind the graph but the y and x axis above

前端 未结 4 682
孤街浪徒
孤街浪徒 2021-01-02 01:18

I am having a hard time plotting grid lines under my graphs without messing with the main x and y axis zorder:

import matplotlib.pyplot as plt
import numpy a         


        
4条回答
  •  悲哀的现实
    2021-01-02 02:01

    I have tried matplotlib 1.2.1, 1.3.1rc2 and master (commit 06d014469fc5c79504a1b40e7d45bc33acc00773)

    To get the axis spines on top of the the bars you can do the following:

    for k, spine in ax.spines.items():  #ax.spines is a dictionary
        spine.set_zorder(10)
    

    EDIT

    It seems that I can't make the tick lines to go on top of the bars. I've tried

    1. ax.tick_params(direction='in', length=10, color='k', zorder=10)
       #This increases the size of the lines to 10 points, 
       #but the lines stays hidden behind  the bars
    2. for l in ax.yaxis.get_ticklines():
           l.set_zorder(10)
    

    and some other way with no results. It seems that when drawing the bars they are put on top and the zorder is ignored

    A workaround could be to draw the tick lines outwards

    ax.tick_params(direction='out', length=4, color='k', zorder=10)
    

    or both in and outwards, using direction='inout'

    EDIT2

    I've done some test after @tcaswell comments.

    If zorder in the ax.bar function is set to be <=2, the axis, ticklines and grid lines are drawn above the bars. If the valus is >2.01 (the default value for axis) the bars are drawn on top of the axis, ticklines and grid. Then it possible to set larger values to the spines (as above) but any attempt to change the zorder of the ticklines is simply ignored (although the values are updated on the corresponding artists).

    I've tried the to use zorder=1 for the bar and zorder=0 for the grid and the grid is drawn on top of the bars. So zorder is ignored.

    recap

    It seems to me that ticklines and grid zorder are just ignored and kept to the default values. For me this is a bug somehow related with bar or some patches.

    BTW, I do remember changing successfully the zorder in ticklines when using imshow

提交回复
热议问题