Margaret Hopkins
Margaret Hopkins

Reputation: 31

How to overlay scatter plot on top of a line plot using matplotlib?

So I have a line plot, and I want to add markers on only some of the points along the plot (I have detected the peaks in the plot and want to mark them). When I plot without the peaks labelled it works as it should, and when I plot the peaks alone it seems to plot them properly, but when I try to plot them on the same plot, the line plot disappears over most of the graph and seems to maybe have become compressed to the side of the plot, if that makes any sense? Here is my code without the peaks plotted and the resulting graph:

 def plotPeaks(file):
      indices, powerSums, times=detectPeaks(file)
      plt.figure(figsize=(100, 10))
      plt.plot(times, powerSums)

Plot without peaks marked

Then when I add the code that should show the peaks, which occur at x-values corresponding to the values stored in the indices, I get this:

def plotPeaks(file):
  indices, powerSums, times=detectPeaks(file)
  plt.figure(figsize=(100, 10))
  plt.plot(times, powerSums)
  for i in indices:
      plt.scatter(i, powerSums[i], marker='o')

Plot with peaks marked

Am I missing something obvious, or is this a glitch that someone has a solution for?

Upvotes: 2

Views: 3635

Answers (1)

Bert Kellerman
Bert Kellerman

Reputation: 1629

Assuming indices stores indices of times, this should be the last line.

  plt.scatter(times[i], powerSums[i], marker='o')

Upvotes: 1

Related Questions