用对数刻度绘制mplot3d/axes3D xyz表面图吗? [英] Plotting mplot3d / axes3D xyz surface plot with log scale?

查看:74
本文介绍了用对数刻度绘制mplot3d/axes3D xyz表面图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找解决这个简单问题的方法,但在任何地方都找不到!有大量的帖子详细介绍了2D中数据的半日志/对数日志绘制,例如plt.setxscale('log')但是我对在3d图(mplot3d)上使用对数刻度感兴趣.

I've been looking high and low for a solution to this simple problem but I can't find it anywhere! There are a loads of posts detailing semilog / loglog plotting of data in 2D e.g. plt.setxscale('log') however I'm interested in using log scales on a 3d plot(mplot3d).

我没有确切的代码,因此无法在此处发布,但是下面的简单示例足以说明情况.我目前正在使用Matplotlib 0.99.1,但不久应该将其更新为1.0.0-我知道我必须为mplot3d实现更新代码.

I don't have the exact code to hand and so can't post it here, however the simple example below should be enough to explain the situation. I'm currently using Matplotlib 0.99.1 but should shortly be updating to 1.0.0 - I know I'll have to update my code for the mplot3d implementation.

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = Axes3D(fig)
X = np.arange(-5, 5, 0.025)
Y = np.arange(-5, 5, 0.025)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.jet, extend3d=True)
ax.set_zlim3d(-1.01, 1.01)

ax.w_zaxis.set_major_locator(LinearLocator(10))
ax.w_zaxis.set_major_formatter(FormatStrFormatter('%.03f'))

fig.colorbar(surf)

plt.show()

上面的代码可以很好地绘制3D图像,但是三个比例(X,Y,Z)都是线性的.我的"Y"数据跨几个数量级(例如9!),因此将其以对数刻度绘制非常有用.我可以通过获取'Y'的日志,重新创建numpy数组并以线性比例绘制log(Y)来解决此问题,但是在真正的python风格中,我正在寻找更智能的解决方案,将数据绘制在对数刻度.

The above code will plot fine in 3D, however the three scales (X, Y, Z) are all linear. My 'Y' data spans several orders of magnitude (like 9!), so it would be very useful to plot it on a log scale. I can work around this by taking the log of the 'Y', recreating the numpy array and plotting the log(Y) on a linear scale, but in true python style I'm looking for smarter solution which will plot the data on a log scale.

是否可以使用对数刻度生成XYZ数据的3D表面图,理想情况下,我希望X& Z是线性标度,Y是对数标度?

Is it possible to produce a 3D surface plot of my XYZ data using log scales, ideally I'd like X & Z on linear scales and Y on a log scale?

任何帮助将不胜感激.请原谅上面示例中的任何明显错误,如前所述,我没有确切的代码,因此从我的记忆中更改了matplotlib画廊示例.

Any help would be greatly appreciated. Please forgive any obvious mistakes in the above example, as mentioned I don't have my exact code to have and so have altered a matplotlib gallery example from my memory.

谢谢

推荐答案

由于我遇到了同样的问题,而Alejandros的回答并未产生预期的结果,这是我到目前为止发现的结果.

Since I encountered the same question and Alejandros answer did not produced the desired Results here is what i found out so far.

3D轴的日志缩放在matplotlib中是一个持续存在的问题.目前,您只能使用以下标签重新标记轴:

The log scaling for Axes in 3D is an ongoing issue in matplotlib. Currently you can only relabel the axes with:

ax.yaxis.set_scale('log')

但是,这不会导致轴按比例对数缩放,而是标记为对数. ax.set_yscale('log')将导致3D异常

This will however not cause the axes to be scaled logarithmic but labeled logarithmic. ax.set_yscale('log') will cause an exception in 3D

在github上查看问题209

See on github issue 209

因此,您仍然必须重新创建numpy数组

Therefore you still have to recreate the numpy array

这篇关于用对数刻度绘制mplot3d/axes3D xyz表面图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆