Issue
When using matplotlib in jupyter notebook in VS code the Z-label of my 3D plot is cut off. I also used the example code from the official matplotlib documentation:
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
def randrange(n, vmin, vmax):
"""
Helper function to make an array of random numbers having shape (n, )
with each number distributed Uniform(vmin, vmax).
"""
return (vmax - vmin)*np.random.rand(n) + vmin
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
n = 100
# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zlow, zhigh)
ax.scatter(xs, ys, zs, marker=m)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
plt.show()
This code returns the following image:
I already tried to change the figure size and ax.dist as suggested in this Answer, both did not resolve the issue. My python version is 3.11.6 and matplotlib 3.8.1.
Solution
The answer you reference works with Matplotlib 3.7.1, which shows the same issue as in your post image. (ax.dist=13
added before plt.show()
line doesn't show Z label' cut off.) However, I get a deprecated warning when I use that approach.
That lead me to here that suggests using ax.set_box_aspect()
to set zoom
. Setting of value one is default according to the documentation.
This addition of ax.set_box_aspect(None, zoom=0.85)
worked for Matplotlib 3.7.1:
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
def randrange(n, vmin, vmax):
"""
Helper function to make an array of random numbers having shape (n, )
with each number distributed Uniform(vmin, vmax).
"""
return (vmax - vmin)*np.random.rand(n) + vmin
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
n = 100
# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for m, zlow, zhigh in [('o', -50, -25), ('^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zlow, zhigh)
ax.scatter(xs, ys, zs, marker=m)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
#ax.dist = 13
ax.set_box_aspect(None, zoom=0.85)
plt.show()
Answered By - Wayne
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.