Skip to content

Instantly share code, notes, and snippets.

@phobson
Created March 4, 2013 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phobson/5086152 to your computer and use it in GitHub Desktop.
Save phobson/5086152 to your computer and use it in GitHub Desktop.
Script to show weird rendering bug with multiple `fill_between` areas on many `twinx` axes
# setup the environment
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, np.pi*4, 137)
y = (2*np.random.normal(size=137) + x**2)
# reflect everything around the origin
xx = np.hstack([-1*x[::-1], x])
yy = np.hstack([-1*y[::-1], y])
# function to do the plot
def horizonPlot(ax, x, y, nfolds=3, inverty=False, color='CornflowerBlue'):
axes = [ax]
if inverty:
ylims = np.linspace(y.min(), y.max(), nfolds + 1)[::-1]
else:
ylims = np.linspace(y.min(), y.max(), nfolds + 1)
for n in range(1, nfolds):
newax = axes[-1].twinx()
axes.append(newax)
for n, ax in enumerate(axes):
ax.fill_between(x, y, y2=ylims[n], facecolor=color, alpha=1.0/nfolds, zorder=n)
ax.set_ylim([ylims[n], ylims[n+1]])
ax.set_yticklabels([])
ax.set_yticks([])
if inverty:
ax.invert_yaxis()
ax.set_xlim([x.min(), x.max()])
return fig
fig, baseax = plt.subplots(figsize=(6.5,1.5))
posax = baseax.twinx()
negax = posax.twinx()
fig = horizonPlot(posax, xx, np.ma.masked_less(yy, 0), inverty=False, color='DarkGreen')
fig = horizonPlot(negax, xx, np.ma.masked_greater(yy, 0), inverty=True, color='CornflowerBlue')
for ax in fig.get_axes():
ax.set_yticklabels([])
fig.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment