matplotlib 在三维图上设置轴限制

sc4hvdpw  于 2023-05-18  发布在  其他
关注(0)|答案(2)|浏览(177)

我想在matplotlib 3D图中设置轴限制,以摆脱超过15,000的值。
我使用了'set_zlim',但在我的结果上发生了一些错误。
我该怎么办?

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(10, 5))
ax = fig.gca( fc='w', projection='3d')

for hz, freq, z in zip(all_hz, all_freq,all_amp):
    x = hz
    y = freq
    z = z
    
    ax.plot3D(x, y, z)
    ax.set_ylim(-10,15000)
    ax.set_zlim(0,0.1)

plt.show()
a1o7rhls

a1o7rhls1#

这似乎是工具包中的一个缺陷,由于透视图。绘制数据时未将其裁剪到正确的限值。您始终可以将数据切片为正确的值:

import numpy as np
# define limits
ylim = (-10,15000)
zlim = (0,0.1)

x = hz 
# slicing with logical indexing
y = freq[ np.logical_and(freq >= ylim[0],freq <= ylim[1] ) ]
# slicing with logical indexing
z = z[ np.logical_and(z >= zlim[0],z <= zlim[1] ) ]
    
ax.plot3D(x, y, z)
ax.set_ylim(ylim) # this shouldn't be necessary but the limits are usually enlarged per defailt
ax.set_zlim(zlim) # this shouldn't be necessary but the limits are usually enlarged per defailt
afdcj2ne

afdcj2ne2#

set_ylim()和set_zlim()方法简单地定义轴的上下边界。他们不会为你修剪数据。要做到这一点,你必须添加一个如下的条件语句来修剪你的数据:

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(10, 5))
ax = fig.gca(fc='w', projection='3d')

for hz, freq, z in zip(all_hz, all_freq, all_amp):
    if freq < 15000 and z < 0.1:
        x = hz
        y = freq
        z = z

        ax.plot3D(x, y, z)
        ax.set_ylim(-10, 15000)
        ax.set_zlim(0, 0.1)

plt.show()

相关问题