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

Pikabu
  • 59
  • 1
  • 1
  • 4

1 Answers1

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

McLawrence
  • 4,975
  • 7
  • 39
  • 51
  • 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. – Pikabu Aug 29 '17 at 13:54
  • So, the example code produces a different output for you? Or the example picture does not look like the result you want to have? – McLawrence Aug 29 '17 at 14:10
  • Can you upload the plot you get and describe how the plot you want should look like in more detail? – McLawrence Aug 31 '17 at 04:41
  • 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. – Pikabu Sep 01 '17 at 10:51
  • could you upload you're data somewhere. I am afraid otherwise I cannot reproduce your error. – McLawrence Sep 08 '17 at 15:21