1

I am trying to make a 3d plot from a Pandas.DataFrame object.

Requirements

  • The number of columns to be plotted for z may vary and hence I am using a loop for the z values with a fixed x and y values. The code is shown in Code 1.

Code 1

import matplotlib.pyplot as plt
import urllib, base64
from mpl_toolkits.mplot3d import axes3d
import numpy as np
import pandas as pd

column_names = ['A', 'B', 'C', 'D', 'E']
df = pd.DataFrame(columns=column_names)

fig2 = plt.figure(figsize=(15,15))
ax2 = fig2.add_subplot(111, projection='3d')

for x in df.columns:
    if(x!='A' and x!='B'):
      ax2.plot_surface(df['A'].values, df['B'].values, df[x].values, linewidth=0, antialiased=False)
      ax2.legend()

Problem:

When I execute Code 1, I get an error -

Argument Z must be 2-dimensional. 

I have solved it when i used - plot_trisurf, as shown in Code 2.

Code 2

for x in df.columns:
    if(x!='A' and x!='B'):
      ax2.plot_trisurf(df['A'].values, df['B'].values, df[x].values, linewidth=0, antialiased=False)
      ax2.legend()

But now I am getting a different error -

Error in qhull Delaunay triangulation calculation: singular input data (exitcode=2); use python verbose option (-v) to see original qhull error.

Question

  • How can I make 3d plots for a Pandas.DataFrame with different number of columns for Z with Legend

Note

The data provided above is just for experimentation and may not be uniform and can have decimals.

Michael
  • 2,167
  • 5
  • 23
  • 38
Arjun G
  • 53
  • 7

1 Answers1

0

The qhull error you're getting is probably because your 'A' and 'B' columns contain data that is linearly dependent, i.e., the points are geometrically on a line (you did not post the data so I cannot verify this). The plot_trisurf function tries to construct a Delaunay triangulation from the X, Y parameters you pass it (the df['A'].values, df['B'].values in your code). A singular/degenerate configuration invokes the error from Qhull, which is the underlying library that is used to construct the Delaunay triangulation (see also my answer here).

If your data is singular/degenerate you can use scatter plots or line plots instead.

If you insist on a surface plot, and your data is singular, you might try to "joggle" the X, Y data so the underlying triangulation will not fail.

Iddo Hanniel
  • 1,636
  • 10
  • 18