在已经绘制的数据上绘制水平线

时间:2019-08-30 21:30:39

标签: python matplotlib plot

因此,我必须将恒定为1的密度绘制到绘制质量与半径(相对于重力)的图上。我需要在数据上附加一行以表示恒定密度。

我尝试过使用axhline,但是它总是导致相对于半径的红线变为1。

%matplotlib notebook
import numpy as np  
import matplotlib.pyplot as plt 

start = 0
end = 1.0
rad_star = np.arange(start,end,0.01)
rho = 1 # constant density
G = 6.67e-11 # gravitational constant
mass = (4.0/3.0)*np.pi*rho*(rad_star)**3
grav = (4*G*np.pi*rho*rad_star)/3

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()
ax1.plot(rad_star,mass, 'b-') 
ax2.plot(rad_star,grav, 'r-')
# ax.hline is not working, everytime rho is mentioned, the radius 
line goes directly to one

ax1.set_title("Stellar Interior of Star with Constant Density")
ax1.set_ylabel('Mass ($M/M_{*}$)', color='blue') 
ax2.set_ylabel('Gravity ($g/g_{surface}$)', color='red')
ax1.set_xlabel('Radius ($r/R_{*}$)')
plt.show()

应该是3个地块;红色的直线,蓝色的指数线和等于1的黑线,代表密度。

0 个答案:

没有答案