2016-04-13 15 views
8

Sono nuovo a pandas e matplotlib. non poteva in grado di ottenere esatto riferimento per tracciare la mia DataFrame cui schema è il seguenteCome tracciare la trama/trama 3d dal dataframe?

schema = StructType([ 
StructField("x", IntegerType(), True), 
StructField("y", IntegerType(), True), 
StructField("z", IntegerType(), True)]) 

Come per tracciare grafico 3d w.r.t. x, yez

Ecco il codice di esempio ho usato

import matplotlib.pyplot as pltt 

dfSpark = sqlContext.createDataFrame(tupleRangeRDD, schema) // reading as spark df 
df = dfSpark.toPandas() 
fig = pltt.figure(); 
ax = fig.add_subplot(111, projection='3d') 
ax.plot_surface(df['x'], df['y'], df['z']) 

sto ottenendo un diagramma grafico vuoto. sicuramente manca qualcosa. Qualche indicazione?

-Thx

Richiesta-1: Stampa df

def print_full(x): 
pd.set_option('display.max_rows', len(x)) 
print(x) 
pd.reset_option('display.max_rows') 


print_full(df) 

Risultato della top 10

  x y  z 
0  301 301  10 
1  300 301  16 
2  300 300  6 
3  299 301  30 
4  299 300  20 
5  299 299  14 
6  298 301  40 
7  298 300  30 
8  298 299  24 
9  298 298  10 
10  297 301  48 
+0

Does df contenere qualsiasi cosa? In tal caso, puoi stampare df.head (n = 10) nella tua domanda? – giosans

+0

aggiorna la mia domanda con la stampa df – mohan

risposta

13

.plot_surface() prende 2Darrays come ingressi, non 1DDataFrame colonne. Questo è stato spiegato abbastanza bene here, insieme al codice seguente che illustra come si potrebbe arrivare al formato richiesto usando l'input DataFrame. Riprodotto di seguito con modifiche minori come commenti aggiuntivi.

In alternativa, tuttavia, c'è .plot_trisurf() che utilizza gli input 1D. Ho aggiunto un esempio nel mezzo del codice.

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import cm 
from matplotlib.ticker import LinearLocator, FormatStrFormatter 
from mpl_toolkits.mplot3d import Axes3D 

## Matplotlib Sample Code using 2D arrays via meshgrid 
X = np.arange(-5, 5, 0.25) 
Y = np.arange(-5, 5, 0.25) 
X, Y = np.meshgrid(X, Y) 
R = np.sqrt(X ** 2 + Y ** 2) 
Z = np.sin(R) 
fig = plt.figure() 
ax = Axes3D(fig) 
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, 
         linewidth=0, antialiased=False) 
ax.set_zlim(-1.01, 1.01) 

ax.zaxis.set_major_locator(LinearLocator(10)) 
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) 

fig.colorbar(surf, shrink=0.5, aspect=5) 
plt.title('Original Code') 
plt.show() 

Original Matlab example

## DataFrame from 2D-arrays 
x = X.reshape(1600) 
y = Y.reshape(1600) 
z = Z.reshape(1600) 
df = pd.DataFrame({'x': x, 'y': y, 'z': z}, index=range(len(x))) 

# Plot using `.trisurf()`: 

ax.plot_trisurf(df.x, df.y, df.z, cmap=cm.jet, linewidth=0.2) 
plt.show() 

Using trisurf with only 1D input

# 2D-arrays from DataFrame 
x1 = np.linspace(df['x'].min(), df['x'].max(), len(df['x'].unique())) 
y1 = np.linspace(df['y'].min(), df['y'].max(), len(df['y'].unique())) 

""" 
x, y via meshgrid for vectorized evaluation of 
2 scalar/vector fields over 2-D grids, given 
one-dimensional coordinate arrays x1, x2,..., xn. 
""" 

x2, y2 = np.meshgrid(x1, y1) 

# Interpolate unstructured D-dimensional data. 
z2 = griddata((df['x'], df['y']), df['z'], (x2, y2), method='cubic') 

# Ready to plot 
fig = plt.figure() 
ax = fig.gca(projection='3d') 
surf = ax.plot_surface(x2, y2, z2, rstride=1, cstride=1, cmap=cm.coolwarm, 
         linewidth=0, antialiased=False) 
ax.set_zlim(-1.01, 1.01) 

ax.zaxis.set_major_locator(LinearLocator(10)) 
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) 

fig.colorbar(surf, shrink=0.5, aspect=5) 
plt.title('Meshgrid Created from 3 1D Arrays') 

plt.show() 

Modified example using <code>DataFrame</code> input