2016-04-13 25 views
8

Tôi mới đến pandasmatplotlib. Không thể lấy tham chiếu chính xác để vẽ sơ đồ số DataFrame của chúng tôi có sơ đồ như sauLàm thế nào để bề mặt cốt truyện/âm mưu 3d từ dataframe?

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

Giống như đồ thị biểu đồ 3 chiều w.r.t. x, y và z

Đây là đoạn mã mẫu tôi đã từng

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

Tôi nhận được một âm mưu đồ thị rỗng. chắc chắn thiếu một cái gì đó. Bất kỳ con trỏ?

-Thx

Request-1: In df

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


print_full(df) 

Kết quả 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

Liệu df chứa bất cứ điều gì? Nếu vậy, bạn có thể in df.head (n = 10) trong câu hỏi của bạn không? – giosans

+0

cập nhật câu hỏi của tôi với in df – mohan

Trả lời

13

.plot_surface() mất 2Darrays như đầu vào, không 1DDataFrame cột. Điều này đã được giải thích khá tốt here, cùng với mã bên dưới minh họa cách người ta có thể đến được định dạng yêu cầu bằng cách sử dụng đầu vào DataFrame. Đã sao chép dưới đây với các sửa đổi nhỏ như nhận xét bổ sung.

Cách khác, tuy nhiên, có .plot_trisurf() sử dụng các đầu vào 1D. Tôi đã thêm một ví dụ ở giữa mã.

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

Các vấn đề liên quan