Matplotlib center/align 在 imshow 图中打勾
Matplotlib center/align ticks in imshow plot
我一直在尝试将 imshow
的 x 和 y 刻度居中,但没有成功。
所需的 yticks
应该是:[ 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
和 xticks
:[ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]
但 aligned/centered。例如。第 1 行的值 100 应该恰好位于行 space 的中间(黄色 box/pixel 的中间)。
import numpy as np
import matplotlib.pyplot as plt
X = np.random.rand(10,11)
plt.figure(dpi=130)
plt.imshow(X, cmap = 'jet', interpolation=None, extent=[5,55,1000,100], aspect='auto')
这里,值5
根本没有出现在x axis
.
中
我也试过下面的方法,x轴可以,但y轴不行
plt.figure(dpi=130)
X = np.random.rand(10,11)
plt.imshow(X, cmap = 'jet', interpolation=None, extent=[2.5,57.5,1000,100], aspect='auto')
ax = plt.gca()
xticks = cluster_space
yticks = space_segment
ax.set_xticks(xticks)
ax.set_yticks(yticks)
一般来说,要使像素居中,您需要将范围设置为从最低像素坐标减去像素宽度的一半到最高像素坐标加上像素宽度的一半。
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
X = np.random.rand(10,11)
plt.figure()
centers = [5,55,1000,100]
dx, = np.diff(centers[:2])/(X.shape[1]-1)
dy, = -np.diff(centers[2:])/(X.shape[0]-1)
extent = [centers[0]-dx/2, centers[1]+dx/2, centers[2]+dy/2, centers[3]-dy/2]
plt.imshow(X, cmap = 'jet', interpolation=None, extent=extent, aspect='auto')
plt.xticks(np.arange(centers[0], centers[1]+dx, dx))
plt.yticks(np.arange(centers[3], centers[2]+dy, dy))
plt.show()
我一直在尝试将 imshow
的 x 和 y 刻度居中,但没有成功。
所需的 yticks
应该是:[ 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
和 xticks
:[ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55]
但 aligned/centered。例如。第 1 行的值 100 应该恰好位于行 space 的中间(黄色 box/pixel 的中间)。
import numpy as np
import matplotlib.pyplot as plt
X = np.random.rand(10,11)
plt.figure(dpi=130)
plt.imshow(X, cmap = 'jet', interpolation=None, extent=[5,55,1000,100], aspect='auto')
这里,值5
根本没有出现在x axis
.
我也试过下面的方法,x轴可以,但y轴不行
plt.figure(dpi=130)
X = np.random.rand(10,11)
plt.imshow(X, cmap = 'jet', interpolation=None, extent=[2.5,57.5,1000,100], aspect='auto')
ax = plt.gca()
xticks = cluster_space
yticks = space_segment
ax.set_xticks(xticks)
ax.set_yticks(yticks)
一般来说,要使像素居中,您需要将范围设置为从最低像素坐标减去像素宽度的一半到最高像素坐标加上像素宽度的一半。
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
X = np.random.rand(10,11)
plt.figure()
centers = [5,55,1000,100]
dx, = np.diff(centers[:2])/(X.shape[1]-1)
dy, = -np.diff(centers[2:])/(X.shape[0]-1)
extent = [centers[0]-dx/2, centers[1]+dx/2, centers[2]+dy/2, centers[3]-dy/2]
plt.imshow(X, cmap = 'jet', interpolation=None, extent=extent, aspect='auto')
plt.xticks(np.arange(centers[0], centers[1]+dx, dx))
plt.yticks(np.arange(centers[3], centers[2]+dy, dy))
plt.show()