Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python pandas to visualise the tangent of a curve

import numpy, matplotlib
import matplotlib.pyplot as plt

xData = numpy.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0])
yData = numpy.array([21.05, 21.21, 20.76, 20.34, 20.27, 20.78, 20.60, 20.55, 19.95, 19.23, 19.64, 19.92, 19.91, 19.56, 19.39, 19.31, 19.35, 18.97, 18.69, 19.00, 19.15, 19.08, 18.97, 19.26, 19.52, 19.56, 19.28, 19.47, 19.85, 19.77])

# polynomial curve fit the test data
fittedParameters = numpy.polyfit(xData, yData, 3)


##########################################################
# graphics output section
def ModelAndScatterPlot(graphWidth, graphHeight):
    f = plt.figure(figsize=(graphWidth/100.0, graphHeight/100.0), dpi=100)
    axes = f.add_subplot(111)

    # first the raw data as a scatter plot
    axes.plot(xData, yData,  'D')

    # create data for the fitted equation plot
    xModel = numpy.linspace(min(xData), max(xData))
    yModel = numpy.polyval(fittedParameters, xModel)

    # now the model as a line plot
    axes.plot(xModel, yModel)

    axes.set_xlabel('X Data') # X axis data label
    axes.set_ylabel('Y Data') # Y axis data label

    # polynomial derivative from numpy
    deriv = numpy.polyder(fittedParameters)

    # for plotting
    minX = min(xData)
    maxX = max(xData)

    # value of derivative (slope) at a specific X value, so
    # that a straight line tangent can be plotted at the point
    # you might place this code in a loop to animate
    pointVal = 15.0 # example X value
    y_value_at_point = numpy.polyval(fittedParameters, pointVal)
    slope_at_point = numpy.polyval(deriv, pointVal)

    ylow = (minX - pointVal) * slope_at_point + y_value_at_point
    yhigh = (maxX - pointVal) * slope_at_point + y_value_at_point

    # now the tangent as a line plot
    axes.plot([minX, maxX], [ylow, yhigh])

    plt.show()
    plt.close('all') # clean up after using pyplot


graphWidth = 800
graphHeight = 600
ModelAndScatterPlot(graphWidth, graphHeight)
Comment

PREVIOUS NEXT
Code Example
Python :: Creating 2-dimesional array 
Python :: python class reflect method of member instance 
Python :: cours python 
Python :: get command line variables python 
Python :: tdlib python 
Python :: EDA describe missing and zero values 
Python :: python go back one using abspath 
Python :: xchacha20 
Python :: sns.kdeplot make line more detailed 
Python :: ring Date and Time Clock 
Python :: DELETE c1 FROM tablename c1 INNER JOIN tablename c2 WHERE c1.id c2.id AND c1.unique_field = c2.unique_field; 
Python :: candelstick chart matplotlib 
Python :: check string on substring godot 
Python :: python get message Exception 
Python :: cuantas palabras hay en una frase en python 
Python :: set change order python 
Python :: how to know google index of a page using python 
Python :: custom 3d image generator for segmentation 
Python :: remove inner list from outer list python 
Python :: python tuple multiply sequence 
Python :: django reverse accessor clashes for abstract class 
Python :: granges to string peak 
Python :: python unresolved import local visual studio code 2019 
Python :: eager tensor to numpy 
Python :: sleep python 
Python :: how to add import pydictionary in python 
Python :: pandas backward fill after upsampling 
Python :: Add extra data to Django notifications model object (extend the notify signal) 
Python :: smallest string with a given numeric value 
Python :: How to Use Sets to Remove Duplicate Items in Other Data Structures 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =