ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 17 Mar 2023 12:05:03 +0100TypeError: 'sage.rings.integer.Integer' object is not subscriptablehttps://ask.sagemath.org/question/66969/typeerror-sageringsintegerinteger-object-is-not-subscriptable/ def undamped(x, t):
return [x[1], x[0]-x[0]^3]
from scipy.integrate import odeint
from scipy import linspace
coordinates=0
nbpts = 1000 # number of points
endtime = 20 # end of time span
tspan = linspace(0.0, endtime, nbpts)
ics1 = [0.09, 0.0] # first initial conditions
sol1 = odeint(undamped, ics1, tspan)
solx1 = [(tspan[k], sol[m]) for k in range(nbpts)]
xplot1 = list_plot(sol, axes_labels=['t','x'], size=1)
coordinates1 = [(s[0], s[1]) for s in sol1]
phaseportrait1 = list_plot(coordinates,axes_labels=['x','y'], size=1)
a = graphics_array([xplot1, phaseportrait1])
a.show()Adwait1602Fri, 17 Mar 2023 12:05:03 +0100https://ask.sagemath.org/question/66969/Elegant solution to 'thin out' array and plot linehttps://ask.sagemath.org/question/8231/elegant-solution-to-thin-out-array-and-plot-line/Sometimes I don't know how many points will be in the target array, yet I need to plot a line with markers. If too many points presented, markers on the plot will overlap and an single bold line will be plotted:
line([(x, np.sin(x)) for x in srange(0,np.pi,np.pi/128)], marker='d')
The solution acceptable for me is to 'thin out' input array in order to exclude points, which are too close to each other. My first implementation of such functional is:
def thin_out_array(points, size):
if len(points) <= size: return points
g = int(len(points)/size)
return [p for i,p in enumerate(points) if i % g == 0]
def line(points, thin_out = None, **kwds):
if thin_out: points = thin_out_array(points, thin_out)
return sage.plot.line.line(points, **kwds)
So this code gives line with separated markers:
line([(x, np.sin(x)) for x in srange(0,np.pi,np.pi/128)], marker='d', thin_out = 32)
Perhaps, more clear and common way to do such thing is already designed?
EugeneSat, 16 Jul 2011 15:48:10 +0200https://ask.sagemath.org/question/8231/