AToe
AToe

Reputation: 329

Plotting quadrants with axvspan

I am trying to plot four colored quadrants on a plot with a range of [-0.2,1.4] and [-0.2,1.4]. The issue is I'm having trouble trying to line up the y axis coordinates of the quadrants using plt.axvspan:

roc_t = 0.43652219
roc_v = 0.82251961

plt.figure()
plt.figure(figsize=(15,12))
plt.xlim(-0.2,1.4)
plt.ylim(-0.2,1.4)
plt.scatter([nTPredict], [nVPredict], color = 'red', marker='X', s=500)

plt.plot([T_Cutoff,T_Cutoff],[-0.2,1.4],color = 'black',linestyle='dashed',lw=2)
plt.plot([1.4,-0.2],[(V_Cutoff),(V_Cutoff)],color = 'black',linestyle='dashed',lw=2)

plt.axvspan(-0.2, roc_t, 0, roc_v, alpha=0.3, color='#1F98D0')#blue
plt.axvspan(roc_t, 1.4, 0, roc_v, alpha=0.3, color='#F9D307')#yellow
plt.axvspan(-0.2, roc_t, roc_v, 1, alpha=0.3, color='#F38D25')#orange
plt.axvspan(roc_t, 1.4, roc_v, 1, alpha=0.3, color='#DA383D')#red

plt.show()
plt.close()

Which produced this:

So the top and bottom rows should end/start at roc_v (indicated by the horizontal dotted line). I understand that in plt.axvspan the ymin and ymax are a relative scale of [0,1] but I can't figure out how to get the quadrants to line up with the horizontal dotted line.

How do I figure out the value of roc_v for the ymin and ymax. I've tried (roc_v*1.6) which I though was the logical answer, but this still doesn't line it up. Otherwise, is there another way I can plot these background quadrants?

Upvotes: 3

Views: 3184

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40697

Although it's probably doable messing around with transforms, I think you'll probably have more luck using something else than axvspan. I would suggest fill_between, where the limits are simply provided in Data coordinates.

PS: for your dashed line, you might want to have a look at axvline() and axhline(), which were specially made for this purpose.

roc_t = 0.43652219
roc_v = 0.82251961

T_Cutoff = roc_t
V_Cutoff = roc_v

fig,ax = plt.subplots(figsize=(15,12))
ax.set_xlim(-0.2,1.4)
ax.set_ylim(-0.2,1.4)


ax.axvline(T_Cutoff,color = 'black',linestyle='dashed',lw=2)
ax.axhline(V_Cutoff,color = 'black',linestyle='dashed',lw=2)

ax.fill_between([-0.2, roc_t],-0.2,roc_v,alpha=0.3, color='#1F98D0')  # blue
ax.fill_between([roc_t, 1.4], -0.2, roc_v, alpha=0.3, color='#F9D307')  # yellow
ax.fill_between([-0.2, roc_t], roc_v, 1.4, alpha=0.3, color='#F38D25')  # orange
ax.fill_between([roc_t, 1.4], roc_v, 1.4, alpha=0.3, color='#DA383D')  # red

plt.show()

enter image description here

Upvotes: 2

Related Questions