[英]How to do calculation on zoomed plot area
I have a time series plot along with a scatter plot on top to indicate some points of the series with certain characteristics.我有一个时间序列图以及顶部的散点图,以指示具有某些特征的系列中的某些点。 On jupyter notebook I am using
%matplotlib notebook
to get interaction plot and zoom.在 jupyter notebook 上,我使用
%matplotlib notebook
来获取交互图和缩放。
Is it possible to calculate all points是否可以计算所有点
EDIT : The following code is a dummy example of ploting radnom data and marking with red dots those point where their value is above a certain threshold.编辑:以下代码是绘制 radnom 数据并用红点标记其值高于某个阈值的那些点的虚拟示例。
%matplotlib notebook
# generate random data [0, 10]
random_data = np.random.randint(10, size = 20)
# implement rule --> i.e. check which data point is > 3
index = np.where([random_data > 3])[1]
value = np.where([random_data > 3])[0]
# plot data and mark data point where rule applies
plt.plot(random_data)
plt.scatter(index, random_data[index], c = 'r')
This generates the plot below.这将生成下面的图。
Is it possible to to get a result that calculates the red dots every time i zoom in the plot是否有可能在每次放大绘图时获得计算红点的结果
So after a lot of search I came up with the following solution.所以经过大量搜索,我想出了以下解决方案。
%matplotlib notebook
# generate random data [0, 10]
random_data = np.random.randint(10, size = 20)
# implement rule --> i.e. check which data point is > 3
index = np.where([random_data > 3])[1]
value = np.where([random_data > 3])[0]
# plot data and mark data point where rule applies
fig, ax = plt.subplots(1,1)
ax.plot(random_data)
ax.scatter(index, random_data[index], c = 'r')
global scatter_index
scatter_data = index
def on_xlims_change(axes):
d1, d2 = axes.get_xlim()
number_of_points = index[np.where((index > d1 )& (index < d2))].shape[0]
axes.legend([f'{ number_of_points } numbers of points in area' ])
# use a maplotlib callback to do the calculation
ax.callbacks.connect('xlim_changed', on_xlims_change)
The idea is that you can use a callback to get the new axis limits and filter data based on those limits.这个想法是您可以使用回调来获取新的轴限制并根据这些限制过滤数据。 Hope
希望
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.