Charlotte Noxon
Charlotte Noxon

Reputation: 109

How to indicate on a graph where the line started?

I would like to indicate the starting point of the graph - where the line started. This is my code

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()
which generates this graph:enter image description here However, it is unclear where the line actually started (somewhere in the upper right, I think, near where it ended). Is there some way I can add a brightly coloured dot to the starting point not specific just to this graph (i.e. so I can reproduce in on other graphs with different conditions)?

Thank you!

Upvotes: 1

Views: 61

Answers (1)

SimonF
SimonF

Reputation: 1895

Plotting the first point can be done by adding plt.plot(n[0], u[0], '*') to your code, see below.

Full documentation for the plot function (thanks for the comment mostlyoxygen), to get a better idea of how you can change the colour, size and shape of the dot.

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

Upvotes: 4

Related Questions