简体   繁体   English

Matplotlib - 仅使用 Y 坐标突出显示已绘制图形中的点

[英]Matplotlib - Highlight points from already plotted graph using only Y-coordinate

I have some stock data and I plotted the data index = x-axis , price = y-axis and now after calculating I have found an array of price ie sub array of price.我有一些股票数据,我绘制了数据index = x-axis , price = y-axis ,现在在计算之后我找到了一个价格数组,即价格的子数组。 I want to highlight the points in the array on the graph我想在图形上突出显示数组中的点

I have tried markvery() documentation but cant understand its working.我试过markvery()文档,但无法理解它的工作markvery() Here is my code这是我的代码

from matplotlib
import pyplot as plt


x =[ 1,2,3,4,5,6,7] # array to be plotted
y=[100,111,112,111,112,113,114] # array to be plotted

subArray = [111,114] # array to be highlighted
plt.plot(x,y)
plt.show()

Any Help would be appreciated任何帮助,将不胜感激

Your subArray contains two points which occur more than once in your y .您的 subArray 包含在您的y出现不止一次的两个点。 So first you can get the indices of your subArray elements from y and then plot them separately again to highlight them.因此,首先您可以从 y 获取 subArray 元素的索引,然后再次单独绘制它们以突出显示它们。 As@ImportanceOfBeingErnest pointed out, there is no in-built general approach for this.正如@ImportanceOfBeingErnest指出的那样,对此没有内置的通用方法。

That being said, things become easier if you convert to NumPy array.话虽如此,如果您转换为 NumPy 数组,事情会变得更容易。 Below is one way to find the indices among others listed here以下是查找此处列出的其他索引的一种方法

import numpy as np

x =np.array([ 1,2,3,4,5,6,7]) # array to be plotted
y=np.array([100,111,112,111,112,113,114]) # array to be plotted

subArray = [111,114] 
ids = np.nonzero(np.in1d(y, subArray))[0]

plt.plot(x,y)
plt.plot(x[ids], y[ids], 'bo')

在此处输入图片说明

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

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