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, 21 Oct 2022 17:43:00 +0200A problem rendering text in 2d graphics; but whose problem is it?https://ask.sagemath.org/question/64550/a-problem-rendering-text-in-2d-graphics-but-whose-problem-is-it/I've run into a problem with the placement of LaTeX formatted text in 2d Graphics objects. The center point of the text object is incorrectly calculated when saving to an SVG file. There is no problem with PNG or PDF output. Here's a Sage script for creating an example.
<pre>
from matplotlib import rc
rc('text', usetex=True)
A=text(r"$\textrm{XOOOOXOOOO}$",\
(1,1),\
fontsize=5,\
color="red")\
+\
text(r"$\textrm{XOOOOXOOOOX}$",\
(1,1),\
fontsize=5,\
color="green",\
rotation=(90))\
+\
text(r"$\textrm{XOOOOXOOOOX}$",\
(1,1),\
fontsize=5,\
color="blue",\
rotation=(45))\
+\
circle((0,0), sqrt(2), color="black")\
+\
line([(-2,-2),(2,2)], color="black")
A.save("SP.svg")
A.save("SP.png")
A.save("SP.pdf")
</pre>
I'm running SageMath 9.7 using Python 3.10.5. (I don't have enough Karma to attach the output as images.)
Is the SVG renderer the problem or is it my use of text mode inside math mode over reaching to achieve the effect of using TeX fonts?richardquintFri, 21 Oct 2022 17:43:00 +0200https://ask.sagemath.org/question/64550/2d Random march Markov'Chainhttps://ask.sagemath.org/question/45425/2d-random-march-markovchain/Hi
problem of the robot that moves symmetrically with steps of 1 on a graduated network (table),
starting from the origin can be interpreted as the problem of the ruin of the "two-dimensional" player,
I put the sageMath code to solve this problem on cocalc.com in order to share it. Maybe it can help SageMath beginners get started.
However, I do not think my code is optimal ! Any constructive criticism will be welcome.
[code on cocalc.com](https://cocalc.com/share/bed80555-322d-4f9a-a456-e2473a51ff95/muGamblerRuin.ipynb?viewer=share)
[a discussion on the subject](https://www.maths-forum.com/informatique/gambler-ruin-markov-chain-t204039.html)ortolljTue, 12 Feb 2019 09:08:12 +0100https://ask.sagemath.org/question/45425/How do you get a 2d version of a 3d plot?https://ask.sagemath.org/question/40917/how-do-you-get-a-2d-version-of-a-3d-plot/ I am working on Cocalc. I would like to get a nice pdf of the view I want after rotating and zooming.
Yes, taking a screenshot works, but is there a better method? Thanks !alThu, 01 Feb 2018 15:16:06 +0100https://ask.sagemath.org/question/40917/how to plot a circle (without circle())https://ask.sagemath.org/question/38889/how-to-plot-a-circle-without-circle/ Hello,
I know you can plot a circle with circle(x,y,radius), but how do you do it with plot()?germacWed, 20 Sep 2017 22:43:54 +0200https://ask.sagemath.org/question/38889/2D integrate fails.https://ask.sagemath.org/question/10342/2d-integrate-fails/Hi Guys:
I have a function as bellow:
sage: h_1_1_1
0.500000000000000*e^(-1/2*p1^2 - 1/2*p2^2)/((abs(p1 + p2 + 1)^3 + 1)*pi)
I need to performa an integrate as bellow:
h_1_1_1.integrate((p1, -20, 20)).integrate((p2, -20, 20))
Got followings:
0.159154943092*integrate(integrate(e^(-1/2*p1^2 - 1/2*p2^2)/(abs(p1 + p2 + 1)^3 + 1), p1, -20, 20), p2, -20, 20)
Then i need to know the value, using following
N(_)
Then got errors:
.
199 # We only return the result.
--> 200 return numerical_integral(f, a, b)[0]
201
202 def _tderivative_(self, f, x, a, b, diff_param=None):
/Volumes/sage-5.9-OSX-64bit-10.7-x86_64-Darwin/Sage-5.9-OSX-64bit-10.7.app/Contents/Resources/sage/local/lib/python2.7/site-packages/sage/gsl/integration.so in sage.gsl.integration.numerical_integral (sage/gsl/integration.c:1802)()
ValueError: Integrand has wrong number of parameters
colinThu, 11 Jul 2013 02:40:59 +0200https://ask.sagemath.org/question/10342/Is it possible to embed a plot (or png) within another plot?https://ask.sagemath.org/question/34998/is-it-possible-to-embed-a-plot-or-png-within-another-plot/I have looked at the documentation for 2d plots without any success. I would like to know if it is possible to embed a plot within a plot.
I currently have a set of polygons and I would like to have plots of graphs embedded into each region. Something resembling the attached [image](/upfiles/14752402076305906.png).
Is it possible to achieve this using sagemath's plotting interface?
Thanks!fidbcFri, 30 Sep 2016 14:58:10 +0200https://ask.sagemath.org/question/34998/Plotting a 2d functionhttps://ask.sagemath.org/question/25122/plotting-a-2d-function/This should be easy, I want to make a nice colour plot of a 2d function where colour denotes the function value.
I can do
> y=var('y')
> f=y^2
> plot(f)
and I get a nice plot.
If I try
> y=var('y')
> f=y^2+x^2
> plot(f)
Then sage tells me it can't find the variable y. Which is ridiculous, since y is in the same place as when I tried to plot a 1d function of y. Anyway, I tried googling to find a function within sage to achieve this (without resorting to excessive matplotlib syntax) and had no success so thought I'd ask a question here. Intuitively, the second case is hardly more complicated than the former so there should be a simple function that can realize this for me.
Many thanks!DarwinThu, 04 Dec 2014 17:53:03 +0100https://ask.sagemath.org/question/25122/How to draw a 2D vector?https://ask.sagemath.org/question/23999/how-to-draw-a-2d-vector/ I'm sorry, I'm new to this and I really need to draw 2D vectors. But, I wasn't sure how to go about it...I only have limited Python knowledge and from the looks of it, this runs on Python.
I have vector **a**'s trajectory and vector **b**'s trajectory as well... What commands do I put in?
I also need to do vector addition...I'd be really grateful if someone could help me out. Thank you. :) Angel.Sat, 30 Aug 2014 06:59:58 +0200https://ask.sagemath.org/question/23999/R graphics/plots in sagenbhttps://ask.sagemath.org/question/8320/r-graphicsplots-in-sagenb/Are graphics in R on sagenb supported? I only get placeholder icons for output.riotQontrolWed, 14 Sep 2011 22:43:13 +0200https://ask.sagemath.org/question/8320/min(f,g) where f and g take two variableshttps://ask.sagemath.org/question/8907/minfg-where-f-and-g-take-two-variables/Hi
Is it possible to create a function h(x,y) that would be the minimum (or maximum) of any two other 2-variable functions?
Like h(x,y)=max(f(x,y),g(x,y))? (this does not work)
This seems to be a rather useful function but I can't seem to find it.
any ideas?
Thanks
N.sagembThu, 19 Apr 2012 15:52:55 +0200https://ask.sagemath.org/question/8907/interpolated 2D plothttps://ask.sagemath.org/question/8471/interpolated-2d-plot/ Hello!
Is there a way to create a plot from a list of data using some kind of non-linear interpolation (e.g. splines) between the data points?
Thanks.v_2eMon, 14 Nov 2011 17:10:36 +0100https://ask.sagemath.org/question/8471/2D interpolating function from numpy arrays to spherical-plot3dhttps://ask.sagemath.org/question/8424/2d-interpolating-function-from-numpy-arrays-to-spherical-plot3d/I need to plot an antenna emission pattern. I am importing data from a tabbed .txt file (theta[i,j],phi[i,j],r[i,j]) as numpy arrays. I then employ the same data to build a 2D interpolating function with the scipy module scipy.interpolate.interp2d. The interpolating function successfully works in plot3d, but somehow breaks spherical_plot3d. Is there anyway to build a working 2D interpolating function for spherical_plot3D?
-----Toy Data---------
v_phi=array([ 0. , 1.57079637, 3.14159274, 4.71238911, 6.28318548])
v_theta=array([ 0. , 0.78539819, 1.57079637, 2.35619456, 3.14159274])
m_r=array([[ 0.16763356, 0.25683223, 0.16649297, 0.10594339,
0.55282422],
[ 0.16763356, 0.19993708, 0.31403568, 0.47359696,
0.55282422],
[ 0.16763356, 0.25683223, 0.16649297, 0.10594339,
0.55282422],
[ 0.16763356, 0.19993708, 0.31403568, 0.47359696,
0.55282422],
[ 0.16763356, 0.25683223, 0.16649297, 0.10594339,
0.55282422]])
------Traceback from spherical_plot3d---------
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_326.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("c3BoZXJpY2FsX3Bsb3QzZChmLCh4LDAsMipwaSksKHksMCxwaSksY21hcD0ncGluaycsYWRhcHRpdmU9VHJ1ZSkuc2hvdyhhc3BlY3RfcmF0aW89KDEsMSwxKSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/tmp/tmpR1mM9J/___code___.py", line 3, in <module>
exec compile(u"spherical_plot3d(f,(x,_sage_const_0 ,_sage_const_2 *pi), (y,_sage_const_0 ,pi),cmap='pink',adaptive=True).show(aspect_ratio=(_sage_const_1 ,_sage_const_1 ,_sage_const_1 ))" + '\n', '', 'single')
File "", line 1, in <module>
File "base.pyx", line 1052, in sage.plot.plot3d.base.Graphics3d.show (sage/plot/plot3d/base.c:9313)
File "base.pyx", line 957, in sage.plot.plot3d.base.Graphics3d._process_viewing_options (sage/plot/plot3d/base.c:9112)
File "base.pyx", line 198, in sage.plot.plot3d.base.Graphics3d._determine_frame_aspect_ratio (sage/plot/plot3d/base.c:3404)
File "base.pyx", line 214, in sage.plot.plot3d.base.Graphics3d._safe_bounding_box (sage/plot/plot3d/base.c:3521)
File "parametric_surface.pyx", line 318, in sage.plot.plot3d.parametric_surface.ParametricSurface.bounding_box (sage/plot/plot3d/parametric_surface.c:2991)
File "parametric_surface.pyx", line 366, in sage.plot.plot3d.parametric_surface.ParametricSurface.triangulate (sage/plot/plot3d/parametric_surface.c:3521)
File "parametric_surface.pyx", line 361, in sage.plot.plot3d.parametric_surface.ParametricSurface.triangulate (sage/plot/plot3d/parametric_surface.c:3451)
File "parametric_surface.pyx", line 576, in sage.plot.plot3d.parametric_surface.ParametricSurface.eval_grid (sage/plot/plot3d/parametric_surface.c:5152)
File "<string>", line 4, in <lambda>
File "expression.pyx", line 3501, in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:15529)
File "expression.pyx", line 2089, in sage.symbolic.expression.Expression.coerce_in (sage/symbolic/expression.cpp:10788)
File "parent_old.pyx", line 229, in sage.structure.parent_old.Parent._coerce_ (sage/structure/parent_old.c:3420)
File "parent.pyx", line 1047, in sage.structure.parent.Parent.coerce (sage/structure/parent.c:7734)
TypeError: no canonical coercion from <type 'numpy.ndarray'> to Symbolic Ring
------------Code-------------
#importing Libraries
import numpy as np
import scipy
import scipy.interpolate
#Importing data
data=np.loadtxt('/home/giova/data/work/electrodynamics/gmm/shell/maps/test.msc')
#getting the data
theta=data[:,0]
phi=data[:,1]
r=data[:,2];
#reshaping the data
side=sqrt(len(r));m_r=r.reshape(side,side);m_r=m_r.T;
v_theta=theta[::side]
v_phi=phi[0:side]
#building interpolating 2D function
f=scipy.interpolate.RectBivariateSpline(v_phi,v_theta,m_r)
#plotting the function
x,y = var('x,y')
plot3d(f,(x,0,2*pi),(y,0,pi))
#Spherical Plotting
x,y = var('x,y')
spherical_plot3d(f,(x,0,2*pi),(y,0,pi)) Does not work!!!!!
#Workaround
def g(s,t):
out1=f(s,t).tolist()
out2=out1[0][0]
return out2
spherical_plot3d(g,(x,0,2*pi),(y,0,pi)) Does work!!!!!GiovanniMon, 31 Oct 2011 07:37:11 +0100https://ask.sagemath.org/question/8424/2D plot performancehttps://ask.sagemath.org/question/8420/2d-plot-performance/I have this function
sage: f=imag(I*(sqrt(-cos(l) + 1)*cosh(sin(1/2*l)) -
sqrt(2)*sinh(sqrt(sin(1/2*l)^2)))*sin(1/2*l)^3/((-cos(l)
+ 1)^(3/2)*e^(1/2*I*l)))
i.m.h.o. this not something terribly complicated. I wanted to plot it. So I do
sage: time plot(f,l,0,10)
..nice plot..
Time: CPU 9.28 s, Wall: 9.41 s
**I.e. I was waiting almost 10s** for this (on an intel core duo CPU P9500 @ 2.53GHz) laptop!? I thought that maybe `fast_callable` would help:
sage: ff=fast_callable(f,vars=[l],domain=CC)
sage: time plot(ff,0,10)
..same nice plot..
Time: CPU 13.24 s, Wall: 13.50 s
So that's even worse.
Now I compare to *Mathematica*
In[8]:= Timing[Plot[Im[(Sin[l/2]^3*(I*Sqrt[1 - Cos[l]]*Cosh[Sin[l/2]] -
I*Sqrt[2]*Sinh[Sqrt[Sin[l/2]^2]]))/(E^((I/2)*l)*(1 -
Cos[l])^(3/2))], {l, 0, 10}]]
Out[8]= {0.019997, ..same plot again..}
**~500 times faster** ... what am I doing wrong?XaverThu, 27 Oct 2011 16:57:58 +0200https://ask.sagemath.org/question/8420/transforming 2D plots to the surface of a spherehttps://ask.sagemath.org/question/8174/transforming-2d-plots-to-the-surface-of-a-sphere/Hi there Sagers:
I'm trying to use a cartographic projection $f:\mathbb{R}^2\to S^2\subset\mathbb{R}^3$ to map Sage 2D graphics objects onto the sphere $S^2$. Projecting points is easy, but how can i project polygons or function plots? Do i somehow grab the graphics object points and use point3d and $f$?
Thanks for your attention.
Alex araichevThu, 21 Jul 2011 00:48:57 +0200https://ask.sagemath.org/question/8174/