4

I want to set axis limits in matplotlib 3D plot to get rid of the value over 15,000.

I used 'set_zlim', but happened some error on my results.

how can I do?

enter image description here


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()
2

2 Answers 2

1

This seem to be a flaw in the toolkit due to the perspective. The data is plotted without being cropped to the correct limits. You can always slice the data to the correct values:

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
Sign up to request clarification or add additional context in comments.

Comments

0

The set_ylim() and set_zlim() methods simply define the upper and lower boundries of the axes. They don't trim your data for you. To do that, you have to add a conditional statement like below to trim your data:

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.