使用matplotlib更新散点图中的标记样式
我正在做一个互动绘图的应用,用户可以从一个matplotlib散点图中选择数据点。为了让用户更清楚,我希望在点击某个点(或者通过其他方式选择时)能够改变这个点的颜色和形状。
因为matplotlib.collections.PathCollection
这个类有一个set_facecolors
的方法,所以改变点的颜色相对简单。不过,我找不到类似的方法来更新点的形状。
有没有办法做到这一点呢?
下面是这个问题的一个简单示例:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.normal(0,1.0,100)
y = np.random.normal(0,1.0,100)
scatter_plot = plt.scatter(x, y, facecolor="b", marker="o")
#update the colour
new_facecolors = ["r","g"]*50
scatter_plot.set_facecolors(new_facecolors)
#update the marker?
#new_marker = ["o","s"]*50
#scatter_plot.???(new_marker) #<--how do I access the marker shapes?
plt.show()
有什么想法吗?
2 个回答
我很确定没有办法做到这一点。scatter
函数把你的数据变成了一系列路径,现在它不再拥有你需要的额外信息(也就是说,它不知道为什么要画这些形状,它只知道要画哪些形状的列表)。
你也可以用set_array
来更新颜色(因为PathCollection
是ScalerMappable
的一个子类)。
如果你想这样做(并且点的数量比较少),你可以手动管理这些路径。
另一个(更简单的)选择是使用两个(或者多个,每种形状/颜色组合一个)Line2D
对象(因为在这个例子中你并没有改变标记的大小),并设置linestyle='none'
。在Line2D
对象上触发选择事件会告诉你你最近的点是哪个。
抱歉,内容有点啰嗦。
如果你想要突出显示用户选择的点,可以在选中的点上面再加一个点(用 dot = ax.scatter(...)
这个命令)。之后,当用户点击时,你可以用 dot.set_offsets((x, y))
来改变这个点的位置。
Joe Kington 写了一个很棒的例子(DataCursor),展示了当用户点击某个图形(比如散点图)时,如何添加一个注释来显示数据坐标。
这里有一个衍生的例子(FollowDotCursor
),当用户把鼠标悬停在某个点上时,它会突出显示并注释数据点。
在 DataCursor
中,显示的数据坐标是用户点击的位置——这可能和底层数据的坐标不完全一样。
而在 FollowDotCursor
中,显示的数据坐标总是和鼠标最近的底层数据点相对应。
import numpy as np
import matplotlib.pyplot as plt
import scipy.spatial as spatial
def fmt(x, y):
return 'x: {x:0.2f}\ny: {y:0.2f}'.format(x=x, y=y)
class FollowDotCursor(object):
"""Display the x,y location of the nearest data point.
"""
def __init__(self, ax, x, y, tolerance=5, formatter=fmt, offsets=(-20, 20)):
try:
x = np.asarray(x, dtype='float')
except (TypeError, ValueError):
x = np.asarray(mdates.date2num(x), dtype='float')
y = np.asarray(y, dtype='float')
self._points = np.column_stack((x, y))
self.offsets = offsets
self.scale = x.ptp()
self.scale = y.ptp() / self.scale if self.scale else 1
self.tree = spatial.cKDTree(self.scaled(self._points))
self.formatter = formatter
self.tolerance = tolerance
self.ax = ax
self.fig = ax.figure
self.ax.xaxis.set_label_position('top')
self.dot = ax.scatter(
[x.min()], [y.min()], s=130, color='green', alpha=0.7)
self.annotation = self.setup_annotation()
plt.connect('motion_notify_event', self)
def scaled(self, points):
points = np.asarray(points)
return points * (self.scale, 1)
def __call__(self, event):
ax = self.ax
# event.inaxes is always the current axis. If you use twinx, ax could be
# a different axis.
if event.inaxes == ax:
x, y = event.xdata, event.ydata
elif event.inaxes is None:
return
else:
inv = ax.transData.inverted()
x, y = inv.transform([(event.x, event.y)]).ravel()
annotation = self.annotation
x, y = self.snap(x, y)
annotation.xy = x, y
annotation.set_text(self.formatter(x, y))
self.dot.set_offsets((x, y))
bbox = ax.viewLim
event.canvas.draw()
def setup_annotation(self):
"""Draw and hide the annotation box."""
annotation = self.ax.annotate(
'', xy=(0, 0), ha = 'right',
xytext = self.offsets, textcoords = 'offset points', va = 'bottom',
bbox = dict(
boxstyle='round,pad=0.5', fc='yellow', alpha=0.75),
arrowprops = dict(
arrowstyle='->', connectionstyle='arc3,rad=0'))
return annotation
def snap(self, x, y):
"""Return the value in self.tree closest to x, y."""
dist, idx = self.tree.query(self.scaled((x, y)), k=1, p=1)
try:
return self._points[idx]
except IndexError:
# IndexError: index out of bounds
return self._points[0]
x = np.random.normal(0,1.0,100)
y = np.random.normal(0,1.0,100)
fig, ax = plt.subplots()
cursor = FollowDotCursor(ax, x, y, formatter=fmt, tolerance=20)
scatter_plot = plt.scatter(x, y, facecolor="b", marker="o")
#update the colour
new_facecolors = ["r","g"]*50
scatter_plot.set_facecolors(new_facecolors)
plt.show()