python - 如何在图表上指示线的起点?
问题描述
我想指出图表的起点 - 线的起点。这是我的代码
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()
谢谢!
解决方案
可以通过在代码中添加 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()
推荐阅读
- javascript - 等待页面加载 - 赛普拉斯
- omnet++ - 在 OMNET++ 项目中子类化 INET 模块时出错
- vue.js - Axios 事件处理程序无法在 EventBux 上触发事件
- c++ - 避免竞争条件性能
- linux - 打开一个终端以无限循环结束
- rust - 在 Bevy 游戏引擎中将 1000x1000 像素纹理分配给 SpriteSheetBundle 大约需要 5 秒
- kotlin - android.content.ActivityNotFoundException:没有找到处理 Intent 的活动 { act=android.intent.action.VIEW dat=zhanysch@gmail.com }
- javascript - 从下拉菜单中选择值并将 setCountries() 设置为所选值 - React JS
- c++ - 如何在 Clion IDE 中使用 z3 和 C++?
- python - Django 在模型中按多对多分组