Skip to content

Instantly share code, notes, and snippets.

@mdmitry1
Last active December 6, 2025 11:08
Show Gist options
  • Select an option

  • Save mdmitry1/8da3c0540e16b813e57960467081f1aa to your computer and use it in GitHub Desktop.

Select an option

Save mdmitry1/8da3c0540e16b813e57960467081f1aa to your computer and use it in GitHub Desktop.
#!/usr/bin/tcsh -f
"/usr/bin/true" '''\'
setenv PYTHONPATH /home/mdmitry/python3.12/dist-packages:/home/mdmitry/python3.12/dist-packages6
exec /usr/bin/python3.12 $0
'''
'''
https://octave.sourceforge.io/octave/function/sombrero.html
'''
import numpy as np
import matplotlib
matplotlib.use('Qt5Agg')
from re import search
from sys import version as python_version
#Workaround for matplotlib bug
if search('GCC UCRT', python_version): from PyQt5 import QtCore
from matplotlib import cm, pyplot as plt
N = 1000
# Create 1D arrays
x_1d = np.linspace(-8, 8, num=N+1, endpoint=True)
y_1d = np.linspace(-8, 8, num=N+1, endpoint=True)
# Create 2D meshgrid
X, Y = np.meshgrid(x_1d, y_1d)
# Calculate r with protection against division by zero
r = np.sqrt(X**2 + Y**2)
r = np.maximum(r, np.finfo(float).eps) # Prevent division by zero
# Calculate z values
Z = np.sin(r) / r
# Create 3D plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X, Y, Z, cmap=cm.rainbow)
# Add colorbar
plt.colorbar(surf, shrink=0.5, aspect=5)
# Set title
rs = 'sqrt(x**2 + y**2)'
plt.setp(plt.title('sin(' + rs + ')/' + rs), color='b', backgroundcolor='lightgray')
# Set window title
fig.canvas.manager.set_window_title('Sombrero plot')
plt.tight_layout()
plt.show()
#!/usr/bin/python3.14
'''
https://octave.sourceforge.io/octave/function/sombrero.html
'''
# Now do your imports
import matplotlib
matplotlib.use('Qt5Agg')
from numpy import sin, sqrt, linspace,finfo, transpose
from matplotlib import cm, pyplot as plt
N=1000
x=linspace(-8,8,num=N+1,endpoint=True).reshape(1,N+1)
x[0][N//2]=finfo(float).eps
y=x.transpose()
rs='sqrt(x**2 + y**2)'
r=eval(rs)
plt.colorbar(plt.axes(projection = '3d').plot_surface(x,y,sin(r)/r, cmap=cm.rainbow), shrink=0.5, aspect=5)
plt.setp(plt.title('sin(' + rs + ')/' + rs ), color='b', backgroundcolor='lightgray')
plt.gcf().canvas.manager.set_window_title('Sombrero plot')
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment