# plot scattered data

I'm totally new and ingnorant in sage however trying to learn by doing. Thanks to other tips that I got here, I managed to make a real progress and now struggling with something that is pretty straightforward in other environments not in sage because of the logic (that I'm still missing) i'm putting the full example that I simplified to focus on the issue. i;e in practice I don't have these two functions. One has to understand that I have no contro on the structure of the data that I need to plot. it's simply two vectors u & v in R^2

#example:
f(x) = 2*sin(2*x+4)
g(x) = 3*sin(-x-4)
eq=f-g
pf = plot(f(x), (x,-pi,pi), color="red", fill=max_symbolic(f(x),g(x)),
fillcolor="lightgreen")
pg = plot(g(x), (x,-pi,pi), color="blue")
show(pf+pg)
nbi=20
endpoints = [-pi+2*pi*i/(nbi-1) for i in range(nbi)]
c=[]
for i in range(nbi-1):
try:
a=endpoints[i]
b=endpoints[i+1]
sol=eq.find_root(a,b)
c.append(n(sol,digits=3))
except RuntimeError:
rep="no sol" #sometime I need to treat that properly
yc=[f(k) for k in c]
mypoints=[[c[k],f(c[k])] for k in range(len(c))]
print(mespoints)
# problems appear from here
ph=plot(mypoints, marker="*",color="black")
show(pf+pg+ph)


Basically I just need to approx highlight the zeros of f-g (intersection points) any help is a step further in my learning path. thx M

edit retag close merge delete

Sort by ยป oldest newest most voted

The following worked for me:

# example:
f(x) = 2*sin(2*x + 4)
g(x) = 3*sin(-x - 4)

eq = f - g
pf = plot(f(x), (x, -pi, pi)
, color="red"
, fill=max_symbolic(f(x),g(x))
, fillcolor="lightgreen")
pg = plot(g(x), (x, -pi, pi)
, color="blue")
# show(pf + pg)

nbi = 20
endpoints = [-pi + 2*pi*k/(nbi - 1) for k in range(nbi)]
c = []

for i in range(nbi-1):
try:
a = endpoints[i]
b = endpoints[i + 1]
sol = eq.find_root(a, b)
c.append(n(sol, digits=3))
except RuntimeError:
rep = "no sol" #sometime I need to treat that properly

yc = [f(k) for k in c]
mes_points = point([ (p, f(p)) for p in c], marker='o', color='maroon', size=60)
print(mes_points)
ph = plot(mes_points)

show(pf + pg + ph)


Your points have to be constructed such that a graphics object results from this.

sage: type(mes_points)
<class 'sage.plot.graphics.Graphics'>


It should also work:

show(pf + pg + mes_points)


Also take a look at sage point doc, the section with

sage.plot.point.point2d(points, alpha=1, aspect_ratio='automatic', faceted=False, legend_color=None, legend_label=None, marker='o', markeredgecolor=None, rgbcolor=(0, 0, 1), size=10, **options)

more

Thank you vey much ! it works and Ineed to get some new kwoledge out of this ! apprciated.

( 2023-01-07 15:24:34 +0200 )edit