Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subplot axis.set_zorder, plots dissapear

I am plotting X plots with twinx, and I want to set the zorder of these axes:

import matplotlib.pyplot as plt 
import numpy as np
from pandas import plotting


x1 = np.linspace(1,20,10) 
y1 = np.random.random_integers(1,20,10)
y2 = np.random.random_integers(1,20,10)
y3 = np.random.random_integers(1,20,10)

fig,ax = plt.subplots()
bx = ax.twinx()
cx = ax.twinx()
colors = getattr(getattr(plotting, '_style'), '_get_standard_colors') 
 (num_colors=5)

ax.plot(x1,y1, label = "1",color= colors[0])
bx.plot(x1,y2, label = "2",color= colors[1])
cx.plot(x1,y3, label = "3",color= colors[2])


cx.spines["right"].set_position(("axes", 1.1))


li,la = ax.get_legend_handles_labels()
li2,la2 = bx.get_legend_handles_labels()
li3,la3 = cx.get_legend_handles_labels()

lines= li+li2+li3
labels = la+la2+la3

ax.legend(lines, labels, loc=9, ncol=2, bbox_to_anchor=(0.5, 1.1))

ax.set_zorder(9)
bx.set_zorder(7)
cx.set_zorder(5)

plt.show()

The plots for bx and cx disappear after setting set_zorder. Without zorder, everything is fine.

Thanks for your help :)

enter image description here

like image 987
Fifo Avatar asked Sep 14 '25 00:09

Fifo


1 Answers

You need to make the background of the axes with higher zorder transparent, else you don't see the lower plots.

ax.set_facecolor("none")
bx.set_facecolor("none")

You may in addition set the facecolor for the lowest axes to white (but that is not strictly necessary),

cx.set_facecolor("white")
like image 82
ImportanceOfBeingErnest Avatar answered Sep 16 '25 15:09

ImportanceOfBeingErnest