You can do something similar to the following, taking this sample of your data:

Copydata = {
        'x': [1539071748.0, 1539071752.0, 1539071755.0, 1539071757.0, 1539071759.0,
          1539071760.0, 1539071764.0, 1539071765.0, 1539071768.0, 1539071872.0,
          1539071979.0, 1539071998.0, 1539072006.0, 1539072123.0, 1539072137.0,
          1539072226.0, 1539072250.0, 1539072386.0, 1539072398.0, 1539072450.0,
          1539072637.0, 1539073158.0, 1539073243.0, 1539073268.0, 1539073615.0,
          1539074097.0, 1539074101.0, 1539074533.0, 1539074691.0, 1539074763.0,
          1539075159.0, 1539075623.0],
        'y': [707, 1212, 1616, 1818, 2020, 2121, 2323, 2424, 2525, 6969, 11009, 11716,
              12019, 16059, 16564, 19493, 20099, 23533, 23836, 25149, 29896, 43127,
              45147, 45753, 55045, 66761, 66862, 77467, 81204, 82921, 92718, 104434]
        }

To compute your derivative (note here that data['y_p'] will be of size n-1, therefore data['y_p'][i] is actually an approximation of the derivative at (data['x'][i] + data['x'][i+1]) / 2):

Copyimport numpy as np

data['y_p'] = np.diff(data['y']) / np.diff(data['x'])
data['x_p'] = (np.array(data['x'])[:-1] + np.array(data['x'])[1:]) / 2

Then plot your results:

Copyimport matplotlib.pyplot as plt

plt.figure(1)
plt.plot(data['x'], data['y'], 'r')
plt.plot(data['x_p'], data['y_p'], 'b')
plt.show()
Answer from rahlf23 on Stack Overflow
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-calculate-and-plot-the-derivative-of-a-function-using-python-ndash-matplotlib
How to Calculate and Plot the Derivative of a Function Using Python โ€“ Matplotlib?
October 11, 2023 - We have created x-axis values using the linspace() function from numpy, and used them to calculate the derivative values. Finally, we plot the function and its derivative using the plot function and add a legend to the graph using the legend function. In this article, we have calculated the derivative of a function using python - Matplotlib.
Discussions

Python / Matplotlib - How to compute/plot derivative without hard-coding it? - Stack Overflow
I am plotting a famous function and its derivative here. The famous function is the one which arises from the Bernoulli's inequality. I wonder if there's some way to calculate the derivative without " More on stackoverflow.com
๐ŸŒ stackoverflow.com
scipy - Numerical derivative in python - Computational Science Stack Exchange
I am trying to take the numerical derivative of a dataset. My first attempt was to use the gradient function from numpy but in that case the graph of the derivative looked not "smooth enough". So I... More on scicomp.stackexchange.com
๐ŸŒ scicomp.stackexchange.com
October 25, 2019
numpy - Get derivative of data in python - Stack Overflow
I write a program to get derivative. InterpolatedUnivariateSpline is used for calculating f(x+h). The red line is derivative of cosine, the green line is cosine consine, the blue line is -sine func... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How can I plot derivatives with matplotlib? - Stack Overflow
I am having a problem of plotting my derivatives. For example of my equation: The value of x = 3 f(x) = 6x^2 - 2 f'(x) = 12x f (3) = 36 from scipy.misc import derivative import matplotlib.pyplot a... More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 27, 2021
๐ŸŒ
Scikit-learn
contrib.scikit-learn.org โ€บ py-earth โ€บ auto_examples โ€บ plot_derivatives.html
Plotting derivatives of simple sine function โ€” py-earth 0.1.0 documentation
import numpy import matplotlib.pyplot as plt from pyearth import Earth # Create some fake data numpy.random.seed(2) m = 10000 n = 10 X = 20 * numpy.random.uniform(size=(m, n)) - 10 y = 10*numpy.sin(X[:, 6]) + 0.25*numpy.random.normal(size=m) # Compute the known true derivative with respect to the predictive variable y_prime = 10*numpy.cos(X[:, 6]) # Fit an Earth model model = Earth(max_degree=2, minspan_alpha=.5, smooth=True) model.fit(X, y) # Print the model print(model.trace()) print(model.summary()) # Get the predicted values and derivatives y_hat = model.predict(X) y_prime_hat = model.pred
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to plot derivative function by python - YouTube
Here we want to plot a function and its derivative function together using Matplotlib , Scipy and numpy libraries We used 'derivate' to derivate the functio...
Published ย  April 27, 2022
๐ŸŒ
UBC
cmps-people.ok.ubc.ca โ€บ jbobowsk โ€บ Python โ€บ html โ€บ Jupyter Discrete Derivatives.html
Jupyter Discrete Derivatives
plt.plot(x1, dydx, 'o', fillstyle = 'none') fitFcn = np.poly1d(a_fit) plt.plot(x1, fitFcn(x1), 'k-'); ... # Notice from the fit that the slope is indeed 2, but the y-intercept is 1 # instead of the expected zero. This is an artifact of taking derivatives # of a discrete set of the data.
๐ŸŒ
4Geeks
4geeks.com โ€บ lesson โ€บ calculus-derivatives-with-python
Calculus Derivatives with python
June 14, 2025 - Master the basics of derivatives in our โ€œIntroduction to Derivativesโ€ article! Learn how to calculate and visualize derivatives with Python - find out now!
Find elsewhere
๐ŸŒ
Readthedocs
scientific-python.readthedocs.io โ€บ en โ€บ latest โ€บ notebooks_rst โ€บ 2_Data_Analysis โ€บ 00_Support_Material โ€บ 03_Numerical_Derivation.html
Derivation of numerical data โ€” Scientific Python: a collection of science oriented python examples documentation
fig = plt.figure() plt.plot(x,df(x),'b',label = "Analytical derivative") plt.plot(xi[:-1],df_1p,'ro',label = "Finite difference derivative (1 point)") plt.plot(xi[1:-1],df_2p,'gs',label = "Finite difference derivative (2 points)") plt.legend() plt.grid()
Top answer
1 of 3
2

Among many others, there are two following methods:


Method 1

You can use derivative from scipy that takes a function f and returns its derivative w.r.t t. So you don't have to define the derivative function f1(t) explicitly.

from scipy.misc import derivative

def f(t):
    return np.power(1 + t, n) - n * t - 1

# Rest of the code

t = np.linspace(a, b, 4000)
g = f(t)

plt.plot(t, g, 'r') # plotting t, g separately 
plt.plot(t, derivative(f, t, dx=0.001), 'g')

Method 2

You can use gradient function of NumPy which uses central differences and returns the same shape as the input array.

t, dt = np.linspace(a, b, 4000, retstep=True)
g1 = np.gradient(f(t), dt)
plt.plot(t, g1, 'g')

2 of 3
1

You can use sympy to calculate the derivative symbolically. If you have a nice mathematical expression, this gives a better accuracy than numerical methods.

Sympy has its own plot functions, but they can be cumbersome if you want to combine many elements. In those cases, it can be easier to use lambdify to convert them to numpy functions.

from sympy import Pow, lambdify
from sympy.abc import t, n

f = Pow(1 + t, n) - n * t - 1
f1 = f.diff(t)  # result: -n + n*(t + 1)**n/(t + 1)

f_np = lambdify(t, f.subs(n, 10))
f1_np = lambdify(t, f1.subs(n, 10))

import numpy as np
from matplotlib import pyplot as plt

a = -2.2
b = +0.25
x = np.linspace(a, b, 1000)

plt.plot(x, f_np(x), 'r')
plt.plot(x, f1_np(x), 'g')

plt.axhline(0, color='k')
plt.axvline(0, color='k')
plt.show()

PS: Purely staying within sympy, plotting can happen as follows:

from sympy import Pow, plot
from sympy.abc import t, n

a = -2.2
b = +0.25
f = Pow(1 + t, n) - n * t - 1
f1 = f.diff(t)
p1 = plot(f.subs(n, 10), (t, a, b), line_color='r', show=False)
p2 = plot(f1.subs(n, 10), (t, a, b), line_color='g', show=False)
p1.append(p2[0])
p1.show()

๐ŸŒ
SciPy
docs.scipy.org โ€บ doc โ€บ scipy โ€บ reference โ€บ generated โ€บ scipy.differentiate.derivative.html
derivative โ€” SciPy v1.17.0 Manual
The implementation was inspired by jacobi [1], numdifftools [2], and DERIVEST [3], but the implementation follows the theory of Taylor series more straightforwardly (and arguably naively so). In the first iteration, the derivative is estimated using a finite difference formula of order order with maximum step size initial_step.
๐ŸŒ
Moonbooks
en.moonbooks.org โ€บ Articles โ€บ How-to-calculate-and-plot-the-derivative-of-a-function-using-matplotlib-and-python-
How to Calculate and Plot the Derivative of a Function Using Matplotlib and Python ?
February 4, 2019 - In Python, we can compute these numerical derivatives using libraries such as findiff and scipy.misc. While findiff is a modern and flexible tool, scipy.misc has been deprecated but may still be useful in older projects. This article covers: - Installing and using findiff to compute derivatives.
๐ŸŒ
Readthedocs
calc-again.readthedocs.io โ€บ en โ€บ latest โ€บ calc_notebooks โ€บ 2.7_calc_3D-calc.html
Calculus in Three Dimensions โ€” Calculus with Python Fall 2018 documentation
December 10, 2022 - These ideas are easily generalized ... dimensional functions and the calculus of derivatives. Similarly, I want to relate this to basic statistical work, and Linear Regression. We will use the mplot3d toolkit to generate our plots....
๐ŸŒ
Turing
turing.com โ€บ kb โ€บ derivative-functions-in-python
How to Calculate Derivative Functions in Python
Derivatives enable us to determine the slope of a curve at any specific point. By examining the slope, we can discern whether a function is increasing or decreasing, identify its maximum or minimum points, and analyze its concavity. Such information is crucial for understanding the dynamics of physical systems, modeling real-world phenomena, optimizing processes, and making predictions in various fields. Python provides a versatile platform for performing derivative calculations.