简体   繁体   English

如何从使用markevery参数绘制的线中提取标记数据点?

[英]How to extract marker data points from a line plotted with markevery argument?

I am trying to get the data coordinates of line markers drawn with a help of markevery option. 我正在尝试借助markevery选项获得绘制的线标记的数据坐标。 My attempt at the code below shows that the retrieved (x,y) data contains all original data values and the argument is ignored: 我对以下代码的尝试表明,检索到的(x,y)数据包含所有原始数据值,并且忽略了该参数:

import numpy as np
from matplotlib import pyplot as plt

line, = plt.plot(np.arange(100)**2, marker='o', markevery=(0, 0.1))

x, y = line.get_data()
assert len(x) != 100

Note the value of markevery argument—advanced indexing on the data in this case is not applicable as an alternative. 请注意markevery参数的值-在这种情况下,对数据的高级索引不能用作替代方法。

Of course, I can successfully use private utility method lines._mark_every_path() to solve the problem I deal with. 当然,我可以成功使用私有实用程序方法lines._mark_every_path()解决我要处理的问题。 But is there any more, let's say, "correct" way to do that? 但是,还有其他的“正确”方法吗?

The markevery option allows to subsample the marked datapoints on a line. markevery选项允许对行中的标记数据点进行二次采样。 Which points are marked or not is only determined at the moment the line is drawn. 标记或不标记哪些点仅在绘制直线时确定。 There is hence no attribute of the line that would store the data of the shown points, because they might change all the time, depending on the size and limits of the plot. 因此,该线没有属性可以存储所显示点的数据,因为它们可能随时变化,具体取决于绘图的大小和限制。

The solution is indeed to replicate the drawing behaviour of the line to access the coordinates of the marked points, using _mark_every_path . 解决方案的确是使用_mark_every_path复制线的绘制行为以访问标记点的坐标。

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import lines

line, = plt.plot(np.arange(100)**2, marker='o', markevery=(0, 0.1))

tpath, affine = line._get_transformed_path().get_transformed_points_and_affine()
p = lines._mark_every_path(line.get_markevery(), 
                      tpath, affine, line.axes.transAxes)

print(len(p.vertices)) # prints 10
print(p.vertices)      # print array of points, shape (10,2)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM