Search code examples
python-2.7matplotlibz-orderpolar-coordinatescartesian-coordinates

matplotlib zorder of elements in polar plot superimposed on cartesian plot


I'm having a difficulty controlling the zorder of the elements of a polar plot superimposed on a cartesian plot.

Consider this example:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(1, 1, marker='*', s=2000, c='r', zorder=2)
ax2 = fig.add_axes(ax.get_position(), frameon=False, polar=True)
ax2.scatter(1., 0.1, marker='*', s=1000, c='b', zorder=1)
plt.xlim(0, 2)
plt.ylim(0, 2)
plt.show()

The result is: enter image description here

It looks like matplotlib ignored the zorder of scatter plots. I would expect the red star to be on top of the blue one.

Could you please explain what I'm doing wrong here?

I found one question, which is kind of similar to mine, but concerns ticklines and grids instead. Maybe it's the same bug?

P.S. I'm running Linux x86_64 with Python 2.7.6 and matplotlib 1.3.1.


Solution

  • The problem is that you are setting the z-order of the marks on different axes ax and ax2 but since ax2 has a greater z-order all the plots in it will be on top of ax. One solution could be to set a higher z-order to ax but then you need to make the background transparent or set frameon=False (and that's maybe not desirable for your case), this is a demonstration of what I'm saying:

    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.scatter(1, 1, marker='*', s=2000, c='r', zorder=2)
    
    ax2 = fig.add_axes(ax.get_position(), frameon=False, polar=True)
    ax2.scatter(1., 0.1, marker='*', s=1000, c='b', zorder=1)
    
    ax.set_zorder(3)
    ax.patch.set_facecolor('none')
    #ax.patch.set_visible(False)
    
    plt.xlim(0, 2)
    plt.ylim(0, 2)
    plt.show()
    

    Plot:

    enter image description here