0

So I found a custom script to make a waterfall plot from a dataframe, which I customized for my purposes. When I run the function for small number of columns, each layer plots accordingly with the subsequent plot properly placed behind the previous plot. When I keep including a larger number of the columns, it starts plotting subsequent plots over the previous ones.

I tried using PolyCollections, and LineCollections and I can't seem to fix this. Is it a bug? Is there a work-around?

def waterfall(X, numTrials):

  # Function to generate formats for facecolors
  cc = lambda arg: colorConverter.to_rgba(arg, alpha=1)
  # This is just wrong. There must be some way to use the meshgrid or why bother.
  verts = []
  for i in np.arange(numTrials)+1:
    verts.append(list(zip(X['Time'].values, X['Trial #'+str(i)].values)))

  xmin = np.floor(np.min(X['Time'].values))
  xmax = np.ceil(np.max(X['Time'].values))
  ymin = -1
  ymax = numTrials+1
  zmin = np.floor(np.min(X.drop(columns=['Time']).values))
  zmax = np.ceil(np.max(X.drop(columns=['Time']).values))

  fig=plt.figure()
  fig.set_size_inches(15,10)
  ax = Axes3D(fig)
 
  poly = PolyCollection(verts,closed='True',edgecolors=[cc('k')],facecolors=[cc('w')])
  ax.add_collection3d(poly, zs=np.arange(numTrials)+1, zdir='y')
  ax.set_xlim(xmin,xmax)
  ax.set_ylim(ymin,ymax)
  ax.set_zlim(zmin,zmax)
  ax.view_init(25, -110)
  plt.show()

for x in np.arange(5,100,20):
  waterfall(CRresponse, x)

This is an example of a proper output, for low number of columns: enter image description here

This is an example of a messed up output for higher number of columns: enter image description here

Thank you for any help on the matter, and I appreciate your patience for a first time poster

  • Can you please provide a simple example of `CRresponse` to reproduce the error in question? – frederick-douglas-pearce Jan 11 '23 at 02:19
  • https://stackoverflow.com/questions/52923540/matplotlib-3d-workaround-for-plot-order – frederick-douglas-pearce Jan 11 '23 at 02:23
  • 1
    Looks like the answer by @jri in this SO post may solve your problem: https://stackoverflow.com/questions/23188561/matplotlib-3d-plot-zorder-issue?noredirect=1&lq=1. Try using `computed_zorder=False` and setting `zorder` with Matplotlib 3.5.0 or higher – frederick-douglas-pearce Jan 11 '23 at 02:28
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Jan 11 '23 at 04:46
  • 1
    Matplotlib only has limited 3D capabilities. It draws everything into layers (the [painter's algorithm](https://en.wikipedia.org/wiki/Painter%27s_algorithm)). For each polygon, a single depth (a "z-order") is estimated, but that can often go wrong. Manually setting the z-order, as suggested by @frederick-douglas-pearce, should make things a lot better. – JohanC Jan 11 '23 at 07:24

0 Answers0