Published and tested 22 November 2026.
Which code change produces this result?
For 11-node interpolation of 1/(1+25x²), Chebyshev roots cluster near ±1; compare errors on the same [−1,1] grid. In my example, the columns in data.csv are x, exact, equispaced, chebyshev.
This article uses simulated example data.

![Result output: For 11-node interpolation of 1/(1+25x²), Chebyshev roots cluster near ±1; compare errors on the same [−1,1] grid.](/research/examples/matplotlib/runge-interpolation-nodes/after.png)
What changes in the actual output?
The value I check is node count. In the script, n gives 11.
| x | exact | equispaced | chebyshev |
|---|---|---|---|
| -1.0 | 0.038461538461538464 | 0.0384615384619692 | -0.005326882782190179 |
| -0.996 | 0.038759089006372 | 0.337570343418532 | 0.014431024989103203 |
| -0.992 | 0.039060058746328355 | 0.6042365431668586 | 0.031228969828105635 |
| -0.988 | 0.03936449951975311 | 0.8405831865448976 | 0.04532507622759108 |
| -0.984 | 0.03967246413609242 | 1.0486339373773688 | 0.056962590884250375 |
| -0.98 | 0.03998400639744103 | 1.230316555121765 | 0.06637048472538876 |
| -0.976 | 0.04029918112063963 | 1.387466283895279 | 0.07376403750900862 |
| -0.972 | 0.04061804415993761 | 1.521829151656611 | 0.07934540535544521 |
| -0.968 | 0.04094065243023713 | 1.6350651812959591 | 0.0833041715641033 |
| -0.964 | 0.041267063930935446 | 1.7287515153618167 | 0.08581788106306254 |
| -0.96 | 0.0415973377703827 | 1.8043854561285366 | 0.08705255883521756 |
| -0.956 | 0.04193153419097298 | 1.8633874226855083 | 0.0871632126589329 |
The table shows the first 12 rows; the CSV contains all rows. (501)
What should I check before using this setting?
The polynomial passes through the supplied nodes, and node selection affects approximation error. Evaluate error at independent check points across the interval.
How can I reproduce the figures?
Run python example.py in a writable folder. Tested with Python 3.12.4, Matplotlib 3.8.4 and NumPy 1.26.4. The script writes data.csv, before.png, after.png and after.pdf beside itself.
These are the code, data and result files used in this article. Choose a file below to run the example or compare your results.
- example.pyComplete example code to run on your computer.
- data.csvThe input data used in the example; open it as a table.
- before.pngThe figure before the change, for comparison.
- after.pngThe figure after applying the settings in this article.
- after.pdfThe result in PDF format, for viewing and page layout.
- Usage license · LICENSE.txtTerms for reusing the code, data and figures, with source and font notices.
File verification
- hashes.jsonChecksums for checking whether the downloaded files are complete.
Complete Python code
# Original Bopbun example code and synthetic input: CC0-1.0.
# Python 3.12.4, NumPy 1.26.4, Matplotlib 3.8.4. See LICENSE.txt for font rights.
from pathlib import Path
import csv,json,math,itertools
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib import colors,patches
OUT=Path(__file__).resolve().parent
plt.rcdefaults()
plt.rcParams.update({'font.family':'DejaVu Sans','font.size':11,'figure.dpi':120,'savefig.dpi':160,'pdf.fonttype':42})
def pair():
f0,a0=plt.subplots(figsize=(7.2,4.5),layout='constrained')
f1,a1=plt.subplots(figsize=(7.2,4.5),layout='constrained')
return f0,a0,f1,a1
def data_file(names,rows):
with (OUT/'data.csv').open('w',newline='',encoding='utf-8') as f:
w=csv.writer(f);w.writerow(names);w.writerows(rows)
before,a0,after,ax=pair()
n=11;x=np.linspace(-1,1,501);f=lambda z:1/(1+25*z*z);e=np.linspace(-1,1,n);c=np.cos((2*np.arange(n)+1)*np.pi/(2*n));pe=np.polynomial.polynomial.polyfit(e,f(e),n-1);pc=np.polynomial.polynomial.polyfit(c,f(c),n-1);ye=np.polynomial.polynomial.polyval(x,pe);yc=np.polynomial.polynomial.polyval(x,pc)
a0.plot(x,f(x),label='Exact');a0.plot(x,ye,label='Equispaced');a0.scatter(e,f(e),s=15);a0.legend();a0.set(xlabel='x',ylabel='Value',title='Equispaced nodes produce endpoint oscillation')
ax.plot(x,f(x),label='Exact');ax.plot(x,yc,label='Chebyshev roots');ax.scatter(c,f(c),s=15);ax.legend();ax.set(xlabel='x',ylabel='Value',title='Endpoint-clustered nodes reduce this error');data_file(['x','exact','equispaced','chebyshev'],zip(x,f(x),ye,yc));metric=n
before.savefig(OUT/'before.png')
after.savefig(OUT/'after.png')
after.savefig(OUT/'after.pdf',metadata={'CreationDate':None,'ModDate':None})
after.canvas.draw()
print(json.dumps({'metric':float(metric),'axes':len(after.axes),'inputRows':sum(1 for _ in (OUT/'data.csv').open())-1}))
plt.close('all')
Which sources and licenses apply?
Original code and data: CC0-1.0. Figures use DejaVu Sans; its font license remains separate. Install Matplotlib and NumPy locally to run the code.
Learn R

