4

I'm trying to plot scatter with over lined line plot. I have two sets of data and if I plot both of them as scatter plots it works, but if I try to plot the second one as a line graph (connected scatter plot), it won't even show.

plt.scatter(column1,column2,s=0.1,c='black')
plt.plot(column3,column4, marker='.', linestyle=':', color='r',)

(I tried using plt.scatter, I tried changing the markers and linestyle, tried without these as well and I still can't get it to work, I sometimes get the dots, but once I want them to be connected they disappear or nothing happens.)

plt.gca().invert_yaxis()
plt.show()

That's what I get: Plot 1

6
  • I removed the tag, added it by mistake. Commented Aug 29, 2017 at 10:33
  • which version of matplotlib are you using? For me, overlaying plt.scatter and plt.plot works fine. Commented Aug 29, 2017 at 10:35
  • @McLawrence 2.0.0 Commented Aug 29, 2017 at 10:38
  • Can you give more detail about your data. Maybe all the scattered data is plotted over the lineplot. Try reversing both plot commands. Commented Aug 29, 2017 at 10:42
  • So, the black is whats supposed to be the scatter plot, and the red should be the lineplot? linestyle='-'makes no difference? Commented Sep 1, 2017 at 11:33

1 Answer 1

10

matplotlib simply overlays plot commands in the called order as long as you do not create a new figure.

As an example, try this code:

import matplotlib.pyplot as plt
import numpy as np

np.random.seed(19680801)

N = 100
x = 0.9 * np.random.rand(N)
y = 0.9 * np.random.rand(N)

plt.scatter(x, y, c='green')
plt.plot(np.linspace(0, 1, 10), np.power(np.linspace(0, 1, 10), 2), c= "red", marker='.', linestyle=':')

plt.gca().invert_yaxis()
plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

I get one figure with both data sets on, I just can't get the second 'scatter' to be a line graph, no matter what I do. Even if I plot it separately.
So, the example code produces a different output for you? Or the example picture does not look like the result you want to have?
Can you upload the plot you get and describe how the plot you want should look like in more detail?
If I copy and paste any plots from the internet it works, but as soon as I use my data it doesn't. I also tried with only parts of the data, but I have the same problem.
could you upload you're data somewhere. I am afraid otherwise I cannot reproduce your error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.