[英]Matplotlib how to center a 2D image at origin on XY surface in 3D plot?
I have an image whose size is 330x330 in pixel and 5cm x 5cm in reality. 我有一张图像,像素大小为330x330,现实中为5cm x 5cm。 When I tried to use the function
plot_surface
, the left top corner of the image was stuck to the origin. 当我尝试使用函数
plot_surface
,图像的左上角固定在原点上。 However, I want the center of the image to locate at the origin like this, 但是,我希望图像的中心位于这样的原点,
Is there any way I can do this? 有什么办法可以做到吗? And how can I change the unit system into
cm
instead of pixels? 怎样将单位制改为
cm
而不是像素?
EDIT: Here is my current code: 编辑:这是我当前的代码:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
xx, yy = np.meshgrid(np.linspace(0, 1, 330), np.linspace(0, 1, 330))
X = xx
Y = yy
ax.plot_surface(X, Y, 0, rstride=10, cstride=10, facecolors=plt.imread('pattern.jpg') / 255., shade=False)
plt.show()
You can set the size of a figure in matplotlib using the figsize=(width, height)
argument. 您可以使用
figsize=(width, height)
参数在matplotlib中设置图形的大小。 The figure size in pixel is determined by the dpi = 80
argument. 以像素为单位的图形大小由
dpi = 80
参数确定。 For example 例如
plt.figure(figsize=(8,6))
...
plt.savefig("out.png", dpi=100)
will result in a 8 times 6 inch figure, which has 800 x 600 pixels. 将产生8乘6英寸的图形,其尺寸为800 x 600像素。
You cannot change the units from inch to something else, but since 1 inch = 2.54 cm you can calculate your desired figure size if needed. 您无法将单位从英寸更改为其他单位,但是由于1英寸= 2.54厘米,因此可以根据需要计算所需的图形尺寸。
All of this has nothing to do with the plotting units that matplotlib uses and plotting in matplotlib is in no way related to real length units, ie you can plot the size of an atom next to the map of the solar system, all in one plot of the size of a sheet of paper. 所有这些都与matplotlib使用的绘图单位无关,并且在matplotlib中进行绘图与实际长度单位毫无关系,即,您可以在太阳系图旁边绘制一个原子的大小,全部在一个图中一张纸的大小。
The position of the origin in a plot is determined by the axis limits. 图中原点的位置由轴限制确定。 Roughly speeking if your axis goes from
0
to 2
, the origin is on the left side of the figure, if your plot goes from -1
to 1
, the origin is in the middle. 如果轴从
0
到2
粗略查看,则原点在图的左侧,如果绘图从-1
到1
则原点在中间。
When ax
is your axes object, you can set the origin using 当
ax
是轴对象时,可以使用以下命令设置原点
ax.set_xlim([-1,1])
ax.set_ylim([-1,1])
in the 2D case or, for the 3D case via 在2D情况下,或者对于3D情况,通过
ax.set_xlim3d([-1,1])
ax.set_ylim3d([-1,1])
ax.set_zlim3d([-1,1])
from mpl_toolkits.mplot3d import Axes3D import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111, projection='3d') xx, yy = np.meshgrid(np.linspace(-1, 1, 330), np.linspace(-1, 1, 330)) X = xx Y = yy c = np.random.rand(330,330,3) ax.plot_surface(X, Y, 0, rstride=30, cstride=30, facecolors=c, shade=False) plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.