[英]Changing the size and color of points in a scatter plot using a slider in python
[英]Python - plot rectangles of known size at scatter points
我有一组要点:
a = ([126, 237, 116, 15, 136, 348, 227, 247, 106, 5, -96, 25, 146], [117, 127, 228, 107, 6, 137, 238, 16, 339, 218, 97, -4, -105])
我像这样绘制它们的散点图:
fig = plt.figure(figsize = (15,6))
ax = fig.add_subplot(111)
ax.scatter(a[0], a[1], color = 'red', s=binradius)
这使得这个情节:
--
我用一张图片覆盖它,其中每个散点都有一个球形斑点。 我想拟合这个斑点,所以我在散点周围定义了一个矩形区域,以便执行拟合。
我想在图上看到这个矩形,以直观地查看它们是否足够大以包围 blob,如下所示:
我可以用scatter
来做吗? 或者我还有其他方法可以做到吗?
虽然@ralf-htp 的回答很好而且干净,并且使用了scatter
,但据我所知,标记的比例是以points
表示的(参见例如此处)。 此外,如果放大,自定义标记不会改变大小。
也许这正是您要寻找的。 如果不是,使用单独的Rectangle
对象也能很好地达到目的。 这允许您以数据单位而不是点指定宽度和高度,并且您可以放大。如果需要,通过设置angle
属性也可以很容易地应用旋转:
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
# Your data
a = ([126, 237, 116, 15, 136, 348, 227, 247, 106, 5, -96, 25, 146],
[117, 127, 228, 107, 6, 137, 238, 16, 339, 218, 97, -4, -105])
# Your scatter plot
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(a[0], a[1], color = 'red', s=10)
# Add rectangles
width = 30
height = 20
for a_x, a_y in zip(*a):
ax.add_patch(Rectangle(
xy=(a_x-width/2, a_y-height/2) ,width=width, height=height,
linewidth=1, color='blue', fill=False))
ax.axis('equal')
plt.show()
注意:如有必要,您可以通过ax.get_children()
获取Rectangle
实例。
您可以在matplotlib.pyplot.scatter
的verts
选项中定义标记,如了解 matplotlib verts和https://matplotlib.org/gallery/lines_bars_and_markers/scatter_custom_symbol.html
verts:(x,y)的序列,可选
如果 marker 为 None [这并不完全有效,请参见Understanding matplotlib verts ],这些顶点将用于构建标记。 标记的中心位于标准化单位的 (0, 0) 处。 整体标记按 s 重新缩放。
来源: https ://matplotlib.org/api/_as_gen/matplotlib.pyplot.scatter.html
但是我不确定如何准确定义您绘制的标记,您必须构建一个逐像素包含所需标记的list
带verts
的矩形:
verts = list(zip([-10.,10.,10.,-10],[-5.,-5.,5.,5]))
ax.scatter([0.5,1.0],[1.0,2.0], marker=(verts,0))
资料来源: 了解 matplotlib verts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.