Home > Net >  Plot a 3 line graphs on a scatter plot_Python
Plot a 3 line graphs on a scatter plot_Python

Time:02-18

I want to plot a 3 line plots on the scatter plot to check how much scatter are the points from the line plot My scatter plot is obtained as below

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

x = np.array([38420690,53439687,82878917,97448841])
y = np.array([47581627,12731149,3388697,911432])

plt.scatter(x,y)

plt.plot()
plt.show()

Now, I want to plot another 3 line graphs on the scatter plot such that,

  1. 1 line graph @ x = y
  2. 2nd Line graph @ x = 10*y
  3. 3rd Line graph @ x = 10/y

Expected outout

enter image description here

Please help me how to do this in python

CodePudding user response:

You can create a linspace of let's say 50 points using the min and max values of your x array and then apply the operations to it:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

x = np.array([38420690,53439687,82878917,97448841])
y = np.array([47581627,12731149,3388697,911432])
min_x = min(x)
max_x = max(x)

newx = np.linspace(min_x, max_x, 50)
newy = newx 
plt.figure(figsize=(12, 8))
plt.scatter(x,y, label='scatter')
plt.plot(newx, newy, color='red', label='x=y') # x=y
plt.plot(newx, newy*10, color='blue', label='x=10*y') # x -> 10*y'
plt.plot(newx, 10/newy, color='black',label='x=10/y') # x -> 10/y

plt.legend()

plt.show()

This results in:

enter image description here

CodePudding user response:

What you describe would be the following:

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

x = np.array([38420690,53439687,82878917,97448841])
y = np.array([47581627,12731149,3388697,911432])

val = [0, 97448841*0.5, 97448841]

plt.scatter(x,y)
plt.plot(val, val, color='red')
plt.plot(val, [i*10 for i in val], color='blue')
plt.plot(val, [i*0.1 for i in val], color='black')
plt.plot()
plt.show()

But you are likely looking for 3 lines with similar slope but different intersection point so instead (more like in the drawing):

plt.plot(val, val, color='red')
plt.plot(val, [i 10000000 for i in val], color='blue')
plt.plot(val, [i-10000000 for i in val], color='black')
  • Related