Jude Li
Jude Li

Reputation: 1

plotting/marking seleted points from a 1D array

this seems a simple question but I have tried it for a really long time. I got a 1d array data(named 'hightemp_unlocked', after I found the peaks(an array of location where the peaks are located) of it, I wanted to mark the peaks on the plot.

import matplotlib
from matplotlib import pyplot as plt

.......

plt.plot([x for x in range(len(hightemp_unlocked))],hightemp_unlocked,label='200 mk db ramp')
plt.scatter(peaks, hightemp_unlocked[x in peaks], marker='x', color='y', s=40)

for some reason, it keeps telling me that x, y must be the same size it shows:

 File "period.py", line 86, in <module>
    plt.scatter(peaks, hightemp_unlocked[x in peaks], marker='x', color='y', s=40)
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/pyplot.py", line 2548, in scatter
    ret = ax.scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, faceted, verts, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/matplotlib/axes.py", line 5738, in scatter
    raise ValueError("x and y must be the same size")

Upvotes: 0

Views: 448

Answers (2)

chdorr
chdorr

Reputation: 298

You are almost on the right track, but hightemp_unlocked[x in peaks] is not what you are looking for. How about something like:

from matplotlib import pyplot as plt

# dummy temperatures
temps = [10, 11, 14, 12, 10, 8, 5, 7, 10, 12, 15, 13, 12, 11, 10]

# list of x-values for plotting
xvals = list(range(len(temps)))

# say our peaks are at indices 2 and 10 (temps of 14 and 15)
peak_idx = [2, 10]

# make a new list of just the peak temp values
peak_temps = [temps[i] for i in peak_idx]

# repeat for x-values
peak_xvals = [xvals[i] for i in peak_idx]

# now we can plot the temps
plt.plot(xvals, temps)

# and add the scatter points for the peak values
plt.scatter(peak_xvals, peak_temps)

Upvotes: 1

juseg
juseg

Reputation: 571

I don't think hightemp_unlocked[x in peaks] is what you want. Here x in peaks reads as the conditional statement "is x in peaks?" and will return True or False depending on what was last stored in x. When parsing hightemp_unlocked[x in peaks], True or False is interpreted as 0 or 1, which returns only the first or second element of hightemp_unlocked. This explains the array size error.

If peaks is an array of indexes, then simply hightemp_unlocked[peaks] will return the corresponding values.

Upvotes: 1

Related Questions