Python/matplotlib mplot3d-如何设置 z 轴的最大值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4913306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python/matplotlib mplot3d- how do I set a maximum value for the z-axis?
提问by user605243
I am trying to make a 3-dimensional surface plot for the expression: z = y^2/x, for x in the interval [-2,2] and y in the interval [-1.4,1.4]. I also want the z-values to range from -4 to 4.
我正在尝试为表达式制作一个 3 维曲面图:z = y^2/x,对于区间 [-2,2] 中的 x 和区间 [-1.4,1.4] 中的 y。我还希望 z 值的范围从 -4 到 4。
The problem is that when I'm viewing the finished surfaceplot, the z-axis values do not stop at [-4,4].
问题是当我查看完成的表面图时,z 轴值不会停在 [-4,4]。
So my question is how I can "remove" the z-axis value that range outside the intervall [-4,4] from the finished plot?
所以我的问题是如何从完成的图中“删除”区间 [-4,4] 之外的 z 轴值?
My code is:
我的代码是:
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection="3d")
x = np.arange(-2.0,2.0,0.1,float) # x in interval [-2,2]
y = np.arange(-1.4,1.4,0.1,float) # y in interval [-1.4,1.4]
x,y = np.meshgrid(x,y)
z = (y**2/x) # z = y^2/x
ax.plot_surface(x, y, z,rstride=1, cstride=1, linewidth=0.25)
ax.set_zlim3d(-4, 4) # viewrange for z-axis should be [-4,4]
ax.set_ylim3d(-2, 2) # viewrange for y-axis should be [-2,2]
ax.set_xlim3d(-2, 2) # viewrange for x-axis should be [-2,2]
plt.show()
回答by Paul
clipping your data will accomplish this, but it's not very pretty.
裁剪您的数据将实现这一点,但它不是很漂亮。
z[z>4]= np.nan
z[z<-4]= np.nan
回答by TocToc
I am having the same issue and still have not found anything better than clipping my data. Unfortunately in my case I am tied to matplotlib 1.2.1. But in case you can upgrade to version 1.3.0 you could have a solution: it seems there is a bunch of new APIrelated to axes ranges. In particular, you may be interested by the "set_zlim".
我遇到了同样的问题,但仍然没有找到比裁剪我的数据更好的方法。不幸的是,在我的情况下,我被绑定到 matplotlib 1.2.1。但是,如果您可以升级到 1.3.0 版,您可以有一个解决方案:似乎有一堆与轴范围相关的新API。特别是,您可能对“set_zlim”感兴趣。
Edit 1: Manage to migrate my environnement to use matplotlib 1.3.0; set_zlim worked like a charm :)
编辑 1:管理迁移我的环境以使用 matplotlib 1.3.0;set_zlim 就像一个魅力:)
The follwing code worked for me (By the way I am running this on OSX, I am not sure this has an impact?):
以下代码对我有用(顺便说一下,我在 OSX 上运行它,我不确定这是否有影响?):
# ----------------------------------------------------------------------------
# Make a 3d plot according to data passed as arguments
def Plot3DMap( self, LabelX, XRange, LabelY, YRange, LabelZ, data3d ) :
fig = plt.figure()
ax = fig.add_subplot( 111, projection="3d" )
xs, ys = np.meshgrid( XRange, YRange )
surf = ax.plot_surface( xs, ys, data3d )
ax.set_xlabel( LabelX )
ax.set_ylabel( LabelY )
ax.set_zlabel( LabelZ )
ax.set_zlim(0, 100)
plt.show()

