0

When z-values become small the z-axis gives wrong magnitude.

Example:

import numpy as np

import matplotlib.pyplot as plt

import matplotlib

print('matplotlib version : ', matplotlib.__version__)

X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
r = 0.0001 # scale z values 

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

# Plot the surface.
surf = ax.plot_surface(X, Y, r*Z, cmap=matplotlib.cm.coolwarm,
                       linewidth=0, antialiased=False)
                       
plt.show()

when taking e.g. r=0.0001 the z-axis should be labelled with 1e-5 or something like that to be correct, but it uses the same exact z-ticks as plotting with r=1.

Edit: added plot. The ticks on the z-axis should be adjusted but aren't. (Just want a an overall 1e-5, for the axis not on each tick)

enter image description here

Expected image for ir = 0.000000000000000000000001 # scale z values ????

enter image description here

8
  • Can you add the output plot that you get that shows the problem? When I generate the plot, the z-axis has an overall 1e-5 factor next to the axis. Do you want this 1e-5 factor to be applied on each tick label? Commented Feb 5 at 14:32
  • Added plot now. Commented Feb 5 at 15:00
  • Thanks. Do you know what version of matplotlib you are using? Commented Feb 5 at 15:02
  • running code on python-fiddle.com ---> matplotlib version : 3.5.2 I get an 1e-5 (1e-5 printed parallel to z axis only once) along z axis Commented Feb 5 at 15:04
  • added @MattPitkin suggested pic please delete it if not the expected output Commented Feb 5 at 15:08

1 Answer 1

0

This is caused by a bug that is present in Matplotlib v3.7 (all minor versions). It was fixed in Matplotlib release 3.8. So, I recommend upgrading your Matplotlib version, e.g.,

pip install matplotlib==3.8

The problem also does not seem to occur in releases prior to 3.7, so you could also downgrade to version 3.6.3, e.g.,

pip install matplotlib==3.6.3

if upgrading is not possible.

If upgrading or downgrading are not possible, then you can manually apply the fix from the pull request, i.e.,

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

X = np.arange(-5, 5, 0.25)
Y = np.arange(-5, 5, 0.25)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
r = 0.0001 # scale z values 

fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

# Plot the surface.
surf = ax.plot_surface(X, Y, r*Z, cmap=matplotlib.cm.coolwarm,
                       linewidth=0, antialiased=False)

# fix the z-axis offset positioning 
ax.zaxis.offsetText._transform = ax.zaxis.axes.transData

plt.show()

The above answer does not fix plots rendered in a Juptyer notebook as standard (i.e., with or without %matplotlib inline magic), i.e., even with Matplotlib v3.8, the offset exponent value on the axis will not show up. Instead, to make things work as expected, you must use interactive plotting (via ipympl) by starting your notebook cell with %matplotlib widget or %matplotlib ipympl.

Sign up to request clarification or add additional context in comments.

6 Comments

Tried versions 3.63, 3.8.0/4 and 3.9.x with same results. Using Conda version 24.3.0 on Ubuntu if it matters.
I was also using a conda environment in Ubuntu, so I'm not sure why you're getting different results. Did you try the manual fix too?
My file /home/miniconda3/envs/torch/lib/python3.11/site-packages/mpl_toolkits/mplot3d/axis3d.py does have the draw() method with the line "self.offsetText._transform = self.axes.transData" as suggested in that PR. Is that what you mean to add manually?
Are you 100% sure you're using the conda environment that you think you are when generating the plot? Are you running the example code directly within a python/ipython terminal within the environment, or is is within a script that your passing to Python, e.g., python myscript.py? Or, in a Jupyter notebook?
I am running in a jupyter notebook yes. Got the axis 1e-6 label right when just running a script at the same location. So this is a jupyter notebook issue then. I can make the figure outside the notebook so problem is solved, but if you have an explanation I'd be curious to know, of course.
|

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.