2016-04-13 25 views
8

मैं pandas और matplotlib पर नया हूं। प्लॉट करने के लिए सही संदर्भ प्राप्त करने में सक्षम नहीं किया जा सका मेरी DataFrame जिसका स्कीमा के रूप मेंडेटाफ्रेम से प्लॉट/3 डी प्लॉट कैसे सतह पर रखें?

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

की तरह 3 डी ग्राफ w.r.t. प्लॉट करने के लिए इस प्रकार है एक्स, वाई और जेड

यहाँ नमूना कोड मैं इस्तेमाल किया

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']) 

मैं एक खाली ग्राफ साजिश हो रही है है। निश्चित रूप से कुछ याद आ रही है। कोई संकेतक?

-Thx

अनुरोध-1: प्रिंट df शीर्ष 10 की

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


print_full(df) 

परिणाम

  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

df कुछ भी बताया गया है? यदि हां, तो क्या आप अपने प्रश्न में df.head (n = 10) प्रिंट कर सकते हैं? – giosans

+0

प्रिंटिंग डीएफ के साथ मेरा प्रश्न अपडेट करें – mohan

उत्तर

13

.plot_surface() इनपुट के रूप में 2Darrays, नहीं 1DDataFrame कॉलम लेता है। DataFrame इनपुट का उपयोग करके आवश्यक प्रारूप में कोई कैसे पहुंच सकता है, यह दर्शाता है कि यह नीचे दिए गए कोड के साथ here काफी अच्छी तरह से समझाया गया है। अतिरिक्त टिप्पणियों जैसे मामूली संशोधन के साथ नीचे पुन: उत्पादित।

वैकल्पिक रूप से, .plot_trisurf() है जो 1D इनपुट का उपयोग करता है। मैंने कोड के बीच में एक उदाहरण जोड़ा है।

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

संबंधित मुद्दे