在散点图区域内选择点

编程入门 行业动态 更新时间:2024-10-28 17:16:07
本文介绍了在散点图区域内选择点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我必须从两个区域之间的散点图中选择值.这些被定义为黑线和红线.所以基本上我需要红线和黑线之间的所有点的值.

I have to select values from a scatter plot between two regions. These are defined as the black and the red line. So basically I need all the values of the points between the red and black line.

N = 500 x = np.random.rand(N) * 2 y = np.random.rand(N) * 2.5 area = np.pi*3 colors = (0,0,0) fig = plt.figure() ax1 = fig.add_subplot(111) plt.scatter(x, y, s=area, alpha=0.5) plt.xlim(0.0, 2.0) plt.ylim(0.0, 2.5) plt.plot([0, 0.8], [1.1, 1.1], 'k-', lw = 2, c = 'r') plt.plot([0, 0.8], [1.5, 1.5], 'k-', lw =2) plt.plot([1.8, 1.8], [2.0, 2.5], 'k-', lw = 2, c = 'r') plt.plot([1.4, 1.4], [2.05, 2.5], 'k-', lw =2) plt.plot([0.8, 1.8], [1.1, 2.0], 'k-', lw = 2, c = 'r') plt.plot([0.8, 1.4], [1.5, 2.05], 'k-', lw = 2) plt.title('Scatter Plot') plt.xlabel('X') plt.ylabel('Y')

这只是一般术语的示例.我的观点不是随机的,而是来自字典.我不知道如何选择该区域内的值.如果您有任何疑问或疑虑,请告诉我.

This is just an example for general terms. My points aren't random but come from a dictionary. I do not know how to select the values within that region. Let me know if you have any questions or concerns.

推荐答案

我们可以为由 3 条红线分隔的凸面区域创建过滤器(作为布尔表达式):

We can create a filter (as a boolean expression) for the convex region delimited by the 3 red lines:

(y >= 1.1) & (x <= 1.8) & (ar * x + br * y + cr >= 0)

对角线的方程可以通过解一个一般方程ax+by+c=0经过2个具体点得到.等式在线上为零,在左侧为负,在右侧为正.

The equation of the diagonal line can be found by solving a general equation ax+by+c=0 to go through 2 specific points. The equation will be zero on the line, negative on the 'left' side and positive on the 'right' side.

类似的滤镜定义了由黑线界定的区域.而表达式 in_red&~in_black 定义了感兴趣的区域.此类过滤器可用于选择要绘制的点,但也可用于选择用于其他计算的点.

A similar filter defines the region delimited by the black lines. And the expression in_red & ~in_black defines the region of interest. Such a filter can be used to select the points to be drawn, but could also be used to select the points for other calculations.

from matplotlib import pyplot as plt import numpy as np # line equation of the form ax+by+c = 0 through (x0,y0) and (x1,y1); # ax+by+c < 0 for points left of the line def get_line_eq(x0, x1, y0, y1): return y0 - y1, x1 - x0, x0 * y1 - x1 * y0 N = 500 x = np.random.uniform(0, 2, N) y = np.random.uniform(0, 2.5, N) ar, br, cr = get_line_eq(0.8, 1.8, 1.1, 2.0) in_red = (y >= 1.1) & (x <= 1.8) & (ar * x + br * y + cr >= 0) ak, bk, ck = get_line_eq(0.8, 1.4, 1.5, 2.05) in_black = (y >= 1.5) & (x <= 1.4) & (ak * x + bk * y + ck >= 0) plt.scatter(x[in_black], y[in_black], s=5, c='k') # inside the black region plt.scatter(x[~in_red], y[~in_red], s=5, c='b') # outside the red region plt.scatter(x[in_red & ~in_black], y[in_red & ~in_black], s=5, c='r') # inside red but outside black plt.xlim(0.0, 2.0) plt.ylim(0.0, 2.5) plt.plot([0, 0.8], [1.1, 1.1], 'k-', lw=2, c='r') plt.plot([0, 0.8], [1.5, 1.5], 'k-', lw=2) plt.plot([1.8, 1.8], [2.0, 2.5], 'k-', lw=2, c='r') plt.plot([1.4, 1.4], [2.05, 2.5], 'k-', lw=2) plt.plot([0.8, 1.8], [1.1, 2.0], 'k-', lw=2, c='r') plt.plot([0.8, 1.4], [1.5, 2.05], 'k-', lw=2) plt.tight_layout() plt.show()

更多推荐

在散点图区域内选择点

本文发布于:2023-05-26 17:54:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/371054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图区   域内

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!