最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Pyplot surface plot z axis scaling label - Stack Overflow

programmeradmin0浏览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)

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

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)

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

Share Improve this question edited Feb 5 at 15:07 pippo1980 3,0613 gold badges17 silver badges39 bronze badges asked Feb 5 at 14:05 twistedqbittwistedqbit 1271 gold badge1 silver badge8 bronze badges 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? – Matt Pitkin Commented Feb 5 at 14:32
  • Added plot now. – twistedqbit Commented Feb 5 at 15:00
  • Thanks. Do you know what version of matplotlib you are using? – Matt Pitkin 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 – pippo1980 Commented Feb 5 at 15:04
  • added @MattPitkin suggested pic please delete it if not the expected output – pippo1980 Commented Feb 5 at 15:08
 |  Show 3 more comments

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论