如何在图形上指示线的起点?

时间:2018-12-29 21:31:33

标签: python matplotlib graph

我想指出图表的起点-直线的起点。这是我的代码

import numpy as np
from scipy.integrate import odeint
from numpy import sin, cos, pi, array
import matplotlib
from matplotlib import rcParams
import matplotlib.pyplot as plt
from pylab import figure, axes, title, show
import xlsxwriter

def deriv(z, t):
    l = 0.3    #unextended length of the spring, in m
    m = 1       #mass of the bob, in kg
    k = 1      #spring constant, in Nm^-1
    g = 9.81    #gravitational acceleration, in ms^-2
    
    x, y, dxdt, dydt = z
    
    dx2dt2 = (l+x)*(dydt)**2 - k/m*x + g*cos(y)
    dy2dt2 = (-g*sin(y) - 2*(dxdt)*(dydt))/(l+x)
            #equations of motion
    
    return np.array([dxdt, dydt, dx2dt2, dy2dt2])

init = array([0.3, pi/2, 0.0, 2])
            #initial conditions (x, y, xdot, ydot)

time = np.linspace(0, 100, 10000)
            #time intervals (start, end, number of intervals)

sol = odeint(deriv, init, time)
            #solving the equations of motion

x = sol[:,0]
y = sol[:,1]

l = 0.3    #unextended length of the spring, in m
 
n = (l+x) * sin(y)
u = -(l+x) * cos(y)
            #converting x and y to Cartesian coordinates

plt.plot(n,u)
plt.xlabel('$n$ (m)')
plt.ylabel('$u$ (m)')
plt.title('$n$ versus $u$ for 'r'$\theta_0 = \frac{\pi}{2}+0.001$')
plt.show()
会生成此图:Here is a demo 但是,尚不清楚该行实际在何处开始(我认为在右上角的某个地方,它接近结束的地方)。有什么方法可以在起点上添加一个鲜艳的点,而不仅限于此图(即,我可以在具有不同条件的其他图上复制)?

谢谢!

1 个答案:

答案 0 :(得分:4)

可以通过在代码中添加plt.plot(n [0],u [0],'*')来绘制第一点,如下所示。

plot function的完整文档(感谢注释大部分为氧气),以使您更好地了解如何更改点的颜色,大小和形状。

output

from scipy.integrate import odeint
from numpy import array, linspace, sin, cos, pi, array
from matplotlib import rcParams
import matplotlib.pyplot as plt

def deriv(z, t):
    l = 0.3    #unextended length of the spring, in m
    m = 1       #mass of the bob, in kg
    k = 1      #spring constant, in Nm^-1
    g = 9.81    #gravitational acceleration, in ms^-2

    x, y, dxdt, dydt = z

    dx2dt2 = (l+x)*(dydt)**2 - k/m*x + g*cos(y)
    dy2dt2 = (-g*sin(y) - 2*(dxdt)*(dydt))/(l+x)
            #equations of motion

    return array([dxdt, dydt, dx2dt2, dy2dt2])

init = array([0.3, pi/2, 0.0, 2])
            #initial conditions (x, y, xdot, ydot)

time = linspace(0, 100, 10000)
            #time intervals (start, end, number of intervals)

sol = odeint(deriv, init, time)
            #solving the equations of motion

x = sol[:,0]
y = sol[:,1]

l = 0.3    #unextended length of the spring, in m

n = (l+x) * sin(y)
u = -(l+x) * cos(y)
            #converting x and y to Cartesian coordinates

plt.plot(n,u)
plt.plot(n[0], u[0], '*')
plt.xlabel('$n$ (m)')
plt.ylabel('$u$ (m)')
plt.title('$n$ versus $u$ for 'r'$\theta_0 = \frac{\pi}{2}+0.001$')
plt.show()