调整散点标记大小以匹配imshow单元格大小
假设我用 imshow
画了一些数据,生成了类似这样的图:
X = 10*np.random.rand(5,3)
fig, ax = plt.subplots()
ax.imshow(X, cmap=cm.jet, interpolation='nearest')
我想在这个图上加一些散点,我知道怎么做。
不过,我希望散点的大小能完全适应用 imshow
画出的每个单元格。也就是说,在上面的例子中,数据是 5 x 3。如果我在 [0, 0] 这个位置画一个 X 标记,我希望它能填满那个橙色的单元格区域。
ax.scatter(0,0, marker='x')
我知道 scatter
有一个大小参数,但我不知道该怎么计算这个大小。
提前谢谢你。
编辑 - 尝试了 @Hyry 在下面回答中提供的代码。
这是我用来画一些点的代码:
def plot_singularities(x_dim, y_dim, x_steps, y_steps, min_points,
max_points, scalar_field, file_path):
"""
Plot the singularities of the rotational free scalar field
:param x_dim : the x dimension of the scalar field
:param y_dim : the y dimension of the scalar field
:param x_steps : the discretization in x of the vector field
:param y_steps : the discretization in y of the vector field
:param scalar_field : the scalar_field to be plot
:param min_points : a set (x, y) of min points of the scalar field
:param max_points : a set (x, y) of max points of the scalar field
"""
plt.figure()
## Plot the scalar field level curves
x, y = numpy.mgrid[-x_dim/2:x_dim/2:x_steps*1j, -y_dim/2:y_dim/2:y_steps*1j]
plt.contour(x, y, scalar_field, colors='white', zorder=1, extent=[-x_dim/2.0, x_dim/2.0, -y_dim/2.0, y_dim/2.0])
## Plot the critical points
x = numpy.linspace(-x_dim/2, x_dim/2, x_steps)
y = numpy.linspace(-y_dim/2, y_dim/2, y_steps)
# Draw the min points
x_indices = numpy.nonzero(min_points)[0]
y_indices = numpy.nonzero(min_points)[1]
rgba_colors = numpy.zeros((len(x_indices), 4))
rgba_colors[:, 1] = 0.5
rgba_colors[:, 3] = min_points[numpy.nonzero(min_points)]
scatter = plt.scatter(x[x_indices], y[y_indices], color=rgba_colors, marker='$\\otimes$', s=1, zorder=2)
scatter.__class__ = DataPathCollection
我得到的标记(绿色的)比预期的大:
我的数据是 100 x 100。难道标记不应该变小吗?
编辑 2:
经过一些测试,我发现问题和 imshow
函数中的 extent
参数有关:
X = 10*np.random.rand(5,5)
fig, ax = plt.subplots()
plt.imshow(X, cmap="jet", interpolation='nearest', extent=[-5, 5, -5, 5])
scatter = plt.scatter(0,0, marker='x', s=1)
scatter.__class__ = DataPathCollection
plt.show()
我该如何调整代码来解决这个问题呢?
谢谢。
1 个回答
2
PathCollection
的大小是根据屏幕坐标系统来决定的,不能直接在数据坐标系统中设置。下面有一个例子,展示了如何在数据坐标系统中缩放路径。你还可以把PathCollection
对象的__class__
改成DataPathCollection
。
交叉标记的原始路径范围是从-0.5到0.5。
from matplotlib.collections import PathCollection
import pylab as pl
import numpy as np
class DataPathCollection(PathCollection):
def draw(self, renderer):
if self._sizes is not None:
affine = self.axes.transData.get_affine()
m = affine.get_matrix()
m[:, 2] = 0
m[2, :] = 0
self._transforms = [affine.scale(x, x) for x in self._sizes]
return Collection.draw(self, renderer)
X = 10*np.random.rand(5,3)
fig, ax = pl.subplots()
ax.imshow(X, cmap="jet", interpolation='nearest')
scatter = ax.scatter(0,0, marker='x', s=1)
scatter.__class__ = DataPathCollection
这是输出结果: