首页 > 解决方案 > 如何在图表上指示线的起点?

问题描述

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

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()
生成此图:在此处输入图像描述 但是,尚不清楚该线的实际开始位置(我认为在右上角的某处,靠近它的结束位置)。有没有什么方法可以在起点上添加一个颜色鲜艳的点,而不仅仅是这个图(即我可以在其他不同条件的图上重现)?

谢谢!

标签: pythonmatplotlibgraph

解决方案


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

绘图功能的完整文档(感谢大多数氧气的评论),以更好地了解如何更改点的颜色、大小和形状。

输出

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()

推荐阅读