0

I want to plot on the same graph one function (data coming from an array) and the line between (xmin,ymin) to (xmax,ymax) of data

import numpy as np
import matplotlib.pyplot as plt

exo_data = np.loadtxt('./exoplanets.dat')

au2m        = 149597870700
day2seconds = 86400 
M_Jupiter   = 1.898e27
M_Sun       = 1.989e30
G           = 6.674e11

R=exo_data[:,0]*au2m
T=exo_data[:,1]*day2seconds 
Mp=exo_data[:,2]*M_Jupiter
Ms=exo_data[:,3]*M_Sun
Mstar=np.mean(Ms)
C=(mth.pi*4)/G*Mstar

x=R**3
y=T**2

xmin=np.min(R)
ymin=np.min(T)
xmax=np.max(R)
ymax=np.max(T)

plt.plot([xmin,ymin], [xmax,ymax], color='red')
plt.plot(x,y, 'ro', color='blue')
plt.show
plt.close

I have only the first plot in spyder console, not the second one

Graph

Expected this :enter image description here

Michel Chance
  • 79
  • 1
  • 10

3 Answers3

0

I'm not sure if it's going to work for you, I suggest you to try the following

plt.plot([xmin,ymin], [xmax,ymax], x, y,'ro', color = 'red')
plt.show
0

Try to run the script not in the spider console, but in the editor to the left of it. The console processes command by command, therefore, it will process one plot at the time and print it in the same way.

Create a file code.py with your script, and then run it. You can do this with the play button in spyder, or by going to a console and typing python code.py.

Proof are doing everything right. As stated here. This should work:

from numpy import *
import math
import matplotlib.pyplot as plt

t = linspace(0, 2*math.pi, 400)
a = sin(t)
b = cos(t)
c = a + b

plt.plot(t, a, 'r') # plotting t, a separately 
plt.plot(t, b, 'b') # plotting t, b separately 
plt.plot(t, c, 'g') # plotting t, c separately 
plt.show()

And should produce this.

image

Duarte O.C
  • 11
  • 4
0

Finally it works... It's my fault. I declared G with e11 and it must be e-11

Michel Chance
  • 79
  • 1
  • 10