1

x and y are lists of 50 elements. SV is a list with 4 elements.

I would like to plot only the elements of x,y that are in positions SV. For example if SV=[3,7,10,15] I would like to plot only x[3],x[7],x[10],x[15] and y[3],y[7],y[10],y[15].

Thus, the list SV indicate the position (not the value) of x,y that I want to plot. I tried something like this but I didn't manage to do it:

plt.scatter(x[SV],y[SV])
DavidG
  • 24,279
  • 14
  • 89
  • 82

3 Answers3

1

Use this:

x, y = [x[i] for i in SV], [y[i] for i in SV]
szamani20
  • 650
  • 9
  • 26
1

x and y need to be numpy arrays, rather than lists, for "fancy indexing":

In [11]: x = np.arange(100, step=2)

In [12]: x[SV]
Out[12]: array([ 6, 14, 20, 30])

In [13]: list(x)[SV]  # doesn't work if a list
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-7dce2933b313> in <module>()
----> 1 list(x)[SV]

TypeError: list indices must be integers or slices, not list

i.e. use the np.array constructor:

x, y = np.array(x), np.array(y)
plt.scatter(x[SV], y[SV])
Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
0

Here's a simple one liner:

plt.scatter(*zip(*[(x[i], y[i]) for i in SV]))
Daniel Trugman
  • 8,186
  • 20
  • 41