如何从数据框中绘制情节/ 3d图? [英] How to surface plot/3d plot from dataframe?
问题描述
我是新来的 pandas
和 matplotlib
。无法得到我的 DataFrame
的详细参考,其模式如下
schema = StructType([
StructField(x,IntegerType(),True),
StructField(y,IntegerType(),True),
StructField z,IntegerType(),True)])
喜欢绘制3d图wrt x,y和z
以下是我使用的示例代码
import matplotlib.pyplot as pltt
dfSpark = sqlContext.createDataFrame(tupleRangeRDD,schema)//读取为spark df
df = dfSpark.toPandas()
fig = pltt。数字();
ax = fig.add_subplot(111,projection ='3d')
ax.plot_surface(df ['x'],df ['y'],df ['z'])
我得到一个空的图形图。绝对缺少一些东西任何指针?
-Thx
请求-1:打印df
def print_full(x):
pd.set_option('display.max_rows',len(x))
print(x)
pd.reset_option('display.max_rows')
print_full(df)
前10名的结果
xyz
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
.plot_surface()
take 2D
数组
作为输入,而不是 1D
DataFrame
列。这已经很好地解释了
## 2D数组中的DataFrame
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)))
#绘制使用`.trisurf()`:
ax.plot_trisurf(df.x,df.y,df.z,cmap = cm.jet,linewidth = 0.2)
plt.show()
I am new to pandas
and matplotlib
. Couldn't able to get exact reference to plot my DataFrame
whose schema is as follows
schema = StructType([
StructField("x", IntegerType(), True),
StructField("y", IntegerType(), True),
StructField("z", IntegerType(), True)])
Like to plot 3d graph w.r.t. x, y and z
Here is the sample code i used
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'])
I am getting a empty graph plot. definitely missing something. Any pointers?
-Thx
Request-1: Print df
def print_full(x):
pd.set_option('display.max_rows', len(x))
print(x)
pd.reset_option('display.max_rows')
print_full(df)
Result of 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
.plot_surface()
takes 2D
arrays
as inputs, not 1D
DataFrame
columns. This has been explained quite well here, along with the below code that illustrates how one could arrive at the required format using DataFrame
input. Reproduced below with minor modifications like additional comments.
Alternatively, however, there is .plot_trisurf()
which uses 1D
inputs. I've added an example in the middle of the code.
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()
## 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()
# 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()
这篇关于如何从数据框中绘制情节/ 3d图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!