我没有成功浏览网页以获得以下简单问题的解决方案:
如何使用顶点值绘制3D多边形(比如填充的矩形或三角形)?我尝试过很多想法,但都失败了,请看:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [0,1,1,0]
y = [0,0,1,1]
z = [0,1,0,1]
verts = [zip(x, y,z)]
ax.add_collection3d(PolyCollection(verts),zs=z)
plt.show()
Run Code Online (Sandbox Code Playgroud)
我提前感谢任何想法/评论.
根据接受的答案更新:
import mpl_toolkits.mplot3d as a3
import matplotlib.colors as colors
import pylab as pl
import scipy as sp
ax = a3.Axes3D(pl.figure())
for i in range(10000):
vtx = sp.rand(3,3)
tri = a3.art3d.Poly3DCollection([vtx])
tri.set_color(colors.rgb2hex(sp.rand(3)))
tri.set_edgecolor('k')
ax.add_collection3d(tri)
pl.show()
Run Code Online (Sandbox Code Playgroud)
结果如下:

我有一些由外部程序生成的表面数据作为XYZ值.我想使用matplotlib创建以下图形:
我已经看了几个用于在matplotlib中绘制曲面和轮廓的示例 - 但是,Z值似乎是X和Y的函数,即Y~f(X,Y).
我假设我将以某种方式需要转换我的Y变量,但我还没有看到任何示例,它显示了如何执行此操作.
所以,我的问题是:给定一组(X,Y,Z)点,如何从该数据生成表面和等高线图?
顺便说一句,为了澄清,我不想创建散点图.虽然我在标题中提到了matplotlib,但我并不反对使用rpy(2),如果这样我可以创建这些图表.
这个问题是与此相关的一个.
我想知道的是如何将建议的解决方案应用于一堆数据(4列),例如:
0.1 0 0.1 2.0
0.1 0 1.1 -0.498121712998
0.1 0 2.1 -0.49973005075
0.1 0 3.1 -0.499916082038
0.1 0 4.1 -0.499963726586
0.1 1 0.1 -0.0181405895692
0.1 1 1.1 -0.490774988618
0.1 1 2.1 -0.498653742846
0.1 1 3.1 -0.499580747953
0.1 1 4.1 -0.499818696063
0.1 2 0.1 -0.0107079119572
0.1 2 1.1 -0.483641823093
0.1 2 2.1 -0.497582061233
0.1 2 3.1 -0.499245863438
0.1 2 4.1 -0.499673749657
0.1 3 0.1 -0.0075248589089
0.1 3 1.1 -0.476713038166
0.1 3 2.1 -0.49651497615
0.1 3 3.1 -0.498911427589 …Run Code Online (Sandbox Code Playgroud) 我试图用plot_surface命令在matplotlib中的相同轴上绘制两个3D表面.
fig = plt.figure()
fig.figsize = fig_size
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X, Y, Exp_Fric_map, alpha = 1, rstride=1, cstride=1, cmap=cm.winter, linewidth=0.5, antialiased=True)
surf = ax.plot_surface(X, Y, Fric_map, alpha = 1, rstride=1, cstride=1, cmap=cm.autumn,linewidth=0.5, antialiased=True)
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,在查看绘图时,并不总是正确的表面是"在顶部",例如在图中:

在后角(200N,轴上为2.5Hz),当实际上黄色 - 红色更靠近观察者时,蓝绿色表面位于"顶部".如果我旋转图:

那么事情看起来还不错,蓝绿色表面位于200N和2.5Hz(现在在左侧)的黄红色表面下方.我尝试过搜索stackoverflow和Google,但找不到解决方案的任何类似问题.
我在Linux上使用Python 2.7.3,Numpy 1.6.1和Matplotlib 1.1.1rc.
我有很多(289)3d点与xyz坐标看起来像:

用积分简单地绘制三维空间就可以了,但我有表面问题有一些要点:
for i in range(30):
output.write(str(X[i])+' '+str(Y[i])+' '+str(Z[i])+'\n')
-0.807237702464 0.904373229492 111.428744443
-0.802470821517 0.832159465335 98.572957317
-0.801052795982 0.744231916692 86.485869328
-0.802505546206 0.642324228721 75.279804677
-0.804158144115 0.52882485495 65.112895758
-0.806418040943 0.405733109371 56.1627277595
-0.808515314192 0.275100227689 48.508994388
-0.809879521648 0.139140394575 42.1027499025
-0.810645106092 -7.48279012695e-06 36.8668106345
-0.810676720161 -0.139773175337 32.714580273
-0.811308686707 -0.277276065449 29.5977405865
-0.812331692291 -0.40975978382 27.6210856615
-0.816075037319 -0.535615685086 27.2420699235
-0.823691366944 -0.654350489595 29.1823292975
-0.836688691603 -0.765630198427 34.2275056775
-0.854984518665 -0.86845932028 43.029581434
-0.879261949054 -0.961799684483 55.9594146815
-0.740499820944 0.901631050387 97.0261463995
-0.735011699497 0.82881933383 84.971061395
-0.733021568161 0.740454485354 73.733621269
-0.732821755233 0.638770044767 63.3815970475
-0.733876941678 0.525818698874 54.0655910105
-0.735055978521 0.403303715698 45.90859502
-0.736448900325 0.273425879041 …Run Code Online (Sandbox Code Playgroud) 我是新来的pandas和matplotlib.无法准确引用绘制我DataFrame的模式如下
schema = StructType([
StructField("x", IntegerType(), True),
StructField("y", IntegerType(), True),
StructField("z", IntegerType(), True)])
Run Code Online (Sandbox Code Playgroud)
喜欢绘制3d图形wrt x,y和z
这是我使用的示例代码
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'])
Run Code Online (Sandbox Code Playgroud)
我得到一个空图形图.肯定遗漏了什么.有什么指针吗?
-谢谢
请求-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)
Run Code Online (Sandbox Code Playgroud)
前十名的结果
x y z
0 301 301 10
1 300 301 16
2 300 300 6
3 299 301 30
4 299 …Run Code Online (Sandbox Code Playgroud) 我试图了解如何构建用于plot_surface的数组(在Axes3d中).我试图建立一个简单的表面来操纵那些数组的数据:
In [106]: x
Out[106]:
array([[0, 0],
[0, 1],
[0, 0]])
In [107]: y
Out[107]:
array([[0, 0],
[1, 1],
[0, 0]])
In [108]: z
Out[108]:
array([[0, 0],
[1, 1],
[2, 2]])
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚它们是如何被解释的 - 例如我的情节中z = 2中没有任何东西.任何人都可以准确地解释哪些值将被用来制作点,哪一个用于行,最后用于表面.
例如,我想建立一个与线点连接的表面:[0,0,0] - > [1,1,1] - > [0,0,2] [0,0,0] - > [1,-1,1] - > [0,0,2]和这些线之间的表面.数组plot_surface看起来应该是什么样的?
我试图从元组列表中绘制一个三维表面,数据如下:
[(60,5,'121'),(61,5,'103'),(62,5,'14 .8'),(63,5,'48 .5'),(64,5,'57 .5') ,(65,5,'75 .7'),(66,5,'89 .6'),(67,5,'55 .3'),(68,5,'63 .3'),(69,5,'118') ,(70,5,'128'),(71,5,'105'),(72,5,'115'),(73,5,'104'),(74,5,'134') ,(75,5,'123'),(76,5,'66 .3'),(77,5,'132'),(78,5,'145'),(79,5,'115') ,(80,5,'38 .2'),(81,5,'10 .4'),(82,5,'18 .4'),(83,5,'87'),(84,5,'86 .7') ,(85,5,'78 .9'),(86,5,'89 .9'),(87,5,'108'),(88,5,'57 .1'),(89,5,'51 .1') ,(90,5,'69 .1'),(91,5,'59 .8'),(60,6,'48 .9'),(61,6,'33 .3'),(62,6,' - 19.2' ),(63,6,' - 17.5'),(64,6,' - 6.5'),(65,6,'75 .7'),(66,6,'89 .6'),(67,6,' 55.3'),(68,6,'99 .8'),(69,6,'156'),(70,6,'141'),(71,6,'54 .1'),(72,6,' 66.1'),(73,6,'98 .9'),(74,6,'155'),(75,6,'146'),(76,6,'111'),(77,6,' 132'),(78,6,'145'),(79,6,'97 .3'),(80,6,'101'),(81,6,'59 .4'),(82,6,' 70.4'),(83,6,'142'),(84,6,'145'),(85,6,'140'),(86,6,'56 .9'),(87,6,' 77.8'),(88 ,6,'21 .1'),(89,6,'27 .1'),(90,6,'48 .1'),(91,6,'41 .8')]
数据的格式为:(X,Y,Z)
理想情况下,我希望它看起来像这样:

我不确定如何将我的数据提供给matplotlib.
提前致谢
如何在 matplotlib 中做这样的事情,但不是用点而是用表面?(我有点坐标)
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
#X = ...
#Y = ... Some coordinates points from file it is list
#Z = ...
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X, Y, Z, c='r')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 matplotlib 制作一个简单的 3D 曲面图,但该图最终没有显示;我只得到空的 3D 轴。
这是我所做的:
from mpl_toolkits.mplot3d import Axes3D
x = np.arange(1, 100, 1)
y = np.arange(1, 100, 1)
z = np.arange(1, 100, 1)
fig = figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x, y, z, rstride=5, cstride=5)
show()
Run Code Online (Sandbox Code Playgroud)
...我明白了:
有什么建议么?
我正在尝试使用 matplotlib 可视化多元正态分布。我想生产这样的东西:
我使用以下代码:
from mpl_toolkits import mplot3d
x = np.linspace(-1, 3, 100)
y = np.linspace(0, 4, 100)
X, Y = np.meshgrid(x, y)
Z = np.random.multivariate_normal(mean = [1, 2], cov = np.array([[0.5, 0.25],[0.25, 0.50]]), size = 100000)
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap='viridis', edgecolor='none')
ax.set_title('surface');
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误消息:
...
7 ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
----> 8 cmap='viridis', edgecolor='none')
...
ValueError: shape mismatch: objects cannot be broadcast to a single shape
Run Code Online (Sandbox Code Playgroud)
错误的原因是什么以及如何更正我的代码?