Matplotlib 3d Wire Frame绘图未按预期绘图

时间:2014-08-18 19:36:48

标签: python matplotlib plot

我正在使用2个变量运行模拟:P和Q.

P和Q均为[0.2,0.4,0.6,0.8]

P和Q的每个组合产生一个我称之为NB_Means的输出。 nb_means是通过运行P = 0.2的模拟器并使用[.2,.4,.6,.8]改变Q来生成的,之后我继续下一个P(.4)并重复相同的过程。 / p>

EX:在nb_means中如下:p = .2& q = .2 - > 32和p = .2& q = .4 - > 159 ......等等

我试图将线框绘制为:

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


x=[.2,.2,.2,.2,.4,.4,.4,.4,.6,.6,.6,.6,.8,.8,.8,.8]
y=[.2,.4,.6,.8,.2,.4,.6,.8,.2,.4,.6,.8,.2,.4,.6,.8]
nb_means = [32, 159, 216, 327, 206, 282, 295, 225, 308, 252, 226, 229, 301, 276, 262, 273]

fig = plt.figure()
ax = plt.axes(projection='3d')
X,Y = np.meshgrid(x,y)

ax.set_title('Name Based Routing')
ax.set_xlabel('Prob of Request')
ax.set_ylabel('Prob of Publish')
ax.set_zlabel('RTT')
ax.plot_wireframe(X, Y, nb_means, rstride=10, cstride=10)
plt.show()

matplotlib output

但是,正如您在上面的输出中看到的那样......我预计线图会沿着Q轴增加。但事实并非如此。

我是否错误地设置了我的x和y?

1 个答案:

答案 0 :(得分:3)

XYnb_means都是问题所在。它们都应该是2D数组(您的nb_means目前是一维列表)。您也不需要使用meshgrid制作XY,您需要做的就是重塑所有内容:

X = np.reshape(x, (4,4))
Y = np.reshape(y, (4,4))
nb2 = np.reshape(nb_means, (4,4))
...
ax.plot_wireframe(X, Y, nb2)

你可能也不太想要rstride = 10和cstride = 10.