简体   繁体   English

在 matplotlib 中缩放轴 3d

[英]Scale axes 3d in matplotlib

I'm facing issues in scaling axes 3d in matplotlib.我在 matplotlib 中缩放轴 3d 时遇到问题。 I have found another questions but somehow the answer it does not seems to work.我发现了另一个问题,但不知何故它似乎不起作用。 Here is a sample code:这是一个示例代码:

import matplotlib as mpl
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

data=np.array([[0,0,0],[10,1,1],[2,2,2]])

fig=plt.figure()
ax=Axes3D(fig)
ax.set_xlim3d(0,15)
ax.set_ylim3d(0,15)
ax.set_zlim3d(0,15)
ax.scatter(data[:,0],data[:,1],data[:,2])
plt.show()

It seems it just ignore the ax.set commands...似乎只是忽略了 ax.set 命令......

In my experience, you have to set your axis limits after plotting the data, otherwise it will look at your data and adjust whatever axes settings you entered before to fit it all in-frame out to the next convenient increment along the axes in question.根据我的经验,您必须在绘制数据后设置您的轴限制,否则它会查看您的数据并调整您之前输入的任何轴设置,以使其在框架内适合沿相关轴的下一个方便的增量。 If, for instance, you set your x-axis limits to +/-400 but your data go out to about +/-1700 and matplotlib decides to label the x-axis in increments of 500, it's going to display the data relative to an x-axis that goes out to +/-2000.例如,如果您将 x 轴限制设置为 +/-400,但您的数据超出 +/-1700 并且 matplotlib 决定以 500 为增量标记 x 轴,它将显示相对于x 轴超出 +/-2000。

So in your case, you just want to rearrange that last block of text as:因此,在您的情况下,您只想将最后一个文本块重新排列为:

fig=plt.figure()
ax=Axes3D(fig)
ax.scatter(data[:,0],data[:,1],data[:,2])
ax.set_xlim3d(0,15)
ax.set_ylim3d(0,15)
ax.set_zlim3d(0,15)
plt.show()

The way of ColorOutOfSpace is good. ColorOutOfSpace 的方式很好。 But if you want to automate the scaling you have to search for the maximum and minimum number in the data and scale with those values.但是,如果您想自动缩放,则必须搜索数据中的最大和最小数字并使用这些值进行缩放。

min = np.amin(data)  # lowest number in the array
max = np.amax(data)  # highest number in the array

ax.set_xlim3d(min, max)
ax.set_ylim3d(min, max)
ax.set_zlim3d(min, max)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM