1

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 i.e sub array of price. I want to highlight the points in the array on the graph

I have tried markvery() documentation but cant understand its working. 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

Salman Arshad
  • 343
  • 6
  • 23
  • What do you mean by "highlight"? Do you want those points to be a different color? – Jordan Singer Jan 29 '19 at 16:05
  • 1
    you could just plot again on top of the first plot – sam46 Jan 29 '19 at 16:06
  • @JordanSinger Yes – Salman Arshad Jan 29 '19 at 16:20
  • @BanishedBot Yes I can do that but is there any matplotlib way of doing this because if I do this manually I have to traverse the whole dataset to find the index of the price an then plot – Salman Arshad Jan 29 '19 at 16:26
  • Mind that the memers of the `subarray` do not necessarily need to be unique inside the initial `y` array. So there is no "general" solution to this. What you should do instead is to review how you created the `subArray` in the first place and store together with it a list of the respective indices (and/or values from `x`). – ImportanceOfBeingErnest Jan 29 '19 at 16:30

1 Answers1

2

Your subArray contains two points which occur more than once in your y. So first you can get the indices of your subArray elements from y and then plot them separately again to highlight them. As @ImportanceOfBeingErnest pointed out, there is no in-built general approach for this.

That being said, things become easier if you convert to NumPy array. 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')

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71