Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put line plot and scatter plot on the same plot in? [duplicate]

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

like image 991
Pikabu Avatar asked Sep 14 '25 15:09

Pikabu


1 Answers

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

like image 70
McLawrence Avatar answered Sep 17 '25 05:09

McLawrence