Matplotlib 设置坐标
Matplotlib set coordinate
我想设置坐标从1到N,而不是0到N
我曾尝试使用 set_ylim()
或 set_ybound
,但失败了。
# Plot the pic.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title("Distribution of sequence order correlation values")
ax.axes.set_xlabel("Column index")
ax.axes.set_ylabel("Row index")
cax = ax.imshow(tar_data, interpolation='nearest')
cbar = fig.colorbar(cax)
这是一个解决方案。它有两层。
首先,您可以使用 imshow
函数的 extent
关键字来指定轴的范围。如果您希望第一个像素的中心位于位置 1,则意味着像素的开头位于位置 0.5。同样,如果最后一个像素的中心位于位置 8,则像素的末尾位于 8.5。这就是为什么您在我的代码中看到范围从 0.5 到 nx+0.5
,其中 nx
是 x 方向上的点数。
完成此操作后,您的坐标轴将在 0.5 到 8.5 之间变化。所以,你的蜱虫会。那不是很漂亮。要更改它,您可以使用 ax.set_xticks()
和 ax.set_yticks()
.
将您的刻度重新定义为从 1 到 8
import numpy as np
import matplotlib.pyplot as plt
data = np.array([[1,23,12],[24,12,7],[14,9,4] ])
ny, nx = data.shape
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(data, interpolation='nearest', extent=[0.5, nx+0.5, ny+0.5, 0.5])
xticks = np.arange(nx)+1
yticks = np.arange(ny)+1
ax.set_xticks(xticks)
ax.set_yticks(yticks)
plt.show()
我想设置坐标从1到N,而不是0到N
我曾尝试使用 set_ylim()
或 set_ybound
,但失败了。
# Plot the pic.
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title("Distribution of sequence order correlation values")
ax.axes.set_xlabel("Column index")
ax.axes.set_ylabel("Row index")
cax = ax.imshow(tar_data, interpolation='nearest')
cbar = fig.colorbar(cax)
这是一个解决方案。它有两层。
首先,您可以使用 imshow
函数的 extent
关键字来指定轴的范围。如果您希望第一个像素的中心位于位置 1,则意味着像素的开头位于位置 0.5。同样,如果最后一个像素的中心位于位置 8,则像素的末尾位于 8.5。这就是为什么您在我的代码中看到范围从 0.5 到 nx+0.5
,其中 nx
是 x 方向上的点数。
完成此操作后,您的坐标轴将在 0.5 到 8.5 之间变化。所以,你的蜱虫会。那不是很漂亮。要更改它,您可以使用 ax.set_xticks()
和 ax.set_yticks()
.
import numpy as np
import matplotlib.pyplot as plt
data = np.array([[1,23,12],[24,12,7],[14,9,4] ])
ny, nx = data.shape
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(data, interpolation='nearest', extent=[0.5, nx+0.5, ny+0.5, 0.5])
xticks = np.arange(nx)+1
yticks = np.arange(ny)+1
ax.set_xticks(xticks)
ax.set_yticks(yticks)
plt.show()