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 thez
values with a fixedx
andy
values. The code is shown inCode 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.