简体   繁体   English

1 轴在 3d plot 中无法正常工作

[英]1 axis does not work correctly in 3d plot

the value on y-axis does not change in my plot if I define my function outside ax.plot_wireframe() .如果我在ax.plot_wireframe()之外定义我的 function ,则 y 轴上的值不会在我的 plot 中改变。 It is the problem of my real function which longer.这是我真正的 function 更长的问题。

import pandas
import numpy
from matplotlib import cm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # <--- This is important for 3d plotting 

a = numpy.linspace(1,10,10)  # x axis
b = numpy.linspace(1,10,10)  # y axis
R = a+b   #model function, real function is longer
z = R
    
fig = plt.figure()
ax = plt.axes(projection='3d')
a,b = numpy.meshgrid(a,b)
#ax.plot_wireframe(a,b,a+b, color='b') #correct
#ax.plot_wireframe(a,b,z, color='b') #wrong
ax.plot_wireframe(a,b,R, color='b') #wrong
ax.set_title('surface');
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');

Here is the result这是结果在此处输入图像描述

Take a look at the matplotlib documentation regarding 3D wireframe plots .查看有关3D 线框图的 matplotlib 文档。 The x, y and z values need to be 2-dimensional. x、y 和 z 值需要是二维的。 Explanations for this are given here or here . 此处此处给出了对此的解释。 This is also the reason for the line这也是线的原因

a,b = numpy.meshgrid(a,b)

in your code.在你的代码中。 It creates a 10x10 2D array for both 1D inputs.它为两个 1D 输入创建一个 10x10 2D 数组。 In the next lines you call the wireframe method with a+b for the z values.在接下来的几行中,您使用a+b作为 z 值调用线框方法。 Hence, the z values are calculated in place and the result is again a 10x10 2D array.因此,z 值是在适当位置计算的,结果又是一个 10x10 2D 数组。 The reason why you get the "wrong" graph with the variables R or z is that they are calculated before a and b are turned into their respective 2D meshgrids.使用变量R or z得到“错误”图表的原因是它们是在a and b转换为各自的 2D 网格之前计算的。 If you define R or z after the line containing numpy.meshgrid it works fine.如果在包含numpy.meshgrid的行之后定义R or z ,它可以正常工作。

import pandas
import numpy
from matplotlib import cm
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D # <--- This is important for 3d plotting 

a = numpy.linspace(1,10,10)  # x axis
b = numpy.linspace(1,10,10)  # y axis

R = a+b   #not working
z = R     #not working

def f(x,y): 
    return x+y

fig = plt.figure()
ax = plt.axes(projection='3d')
a,b = numpy.meshgrid(a,b)

Z = f(a,b)#working
R = a+b   #working
z = R     #working

#ax.plot_wireframe(a,b,a+b, color='b') #
#ax.plot_wireframe(a,b,Z, color='b') #
ax.plot_wireframe(a,b,R, color='b') #
ax.set_title('surface');
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');

So the short answer is: numpy.meshgrid changes your variables a and b and your are basically doing your calculations with different a s and b s所以简短的回答是: numpy.meshgrid改变你的变量 a 和 b 并且你基本上是用不同a s 和b s 进行计算

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM