Ask Your Question

How to plot the real line in SageMath

asked 2023-09-17 14:01:01 +0200

Garald gravatar image

updated 2023-09-19 14:45:37 +0200

I am preparing a SageMath Jupyter notebook for expository purposes. I would like to be able to display the real line, with chosen points on it, and some arrows between certain pairs of points.

While this would be easy to do in LaTeX (by means of TikZ), I do not know how to do it in Sage. This feels like something that should be well within Sage's capabilities. What to do?

Let me give an easy, concrete example. Say I want to plot the points -5/11 + \beta, -4/11 + \beta,..., 5/11+\beta (labelled as such) on the real line, where $\beta = 1/50$ (for instance).

In a second version of the graph, I also want curved arrows going from -5/11+\beta to -4/11+\beta, from -4/11+\beta to -3/11+\beta, and so on until -2/11+\beta to -1+\beta; I also want a curved arrow from -1+\beta to 0.

How to do this?

Update: Let me include some (extremely amateurish) TikZ code, so that people can see exactly what I want:

\draw[-] (-\scale*3.5,0) -- (\scale*3.5,0); 
\foreach \i in {-3,...,3} {
  \draw[shift={(\scale*\i,0)},color=gray] (0pt,0pt) -- (0pt,-3pt) node[below] {$\frac{\i}{q}$};
  \filldraw (\scale*\i+\scale*\bet,0) circle (2pt) node[above] {$\frac{\i}{q}+\beta$};
\foreach \i in {-3,...,-2} {
  \draw[*->*] (\scale*\i+\scale*\bet-0.08,0.04) to[bend right] (\scale*\i+\scale*\bet+\scale+0.08,0.04);
\draw[*->] (-\scale+\scale*\bet-0.08,0.04) to[bend right] (0,0);

I'd gladly include the result here, but I do not yet have enough points to upload files :(.

Of course I can just include the resulting image in my Jupyter notebook, but for some reason Jupyter is refusing to display vector graphics, and will display the image only if it is very low-resolution. Wouldn't it be nice if there were a way to do this within Sage...

edit retag flag offensive close merge delete


Since you don't tell us what you want to plot and how, it's pretty difficult to venture even a hint.

FWIW, look up text?, line? and arrow?... Alternatively, give us some more information.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2023-09-17 19:12:30 +0200 )edit

Hi @Garald

I propose something like that first, because as @Emmanuel Charpentier points out, your request is not very clear.

code on sageCell

Sorry if it is not convenient, then precise your demand. you can add line with code below:

line3d([(1,2,3), (1,0,-2), (3,1,4), (2,1,-2)])
ortollj gravatar imageortollj ( 2023-09-17 19:27:33 +0200 )edit

do you want plot in 3D ?,2D ?

ortollj gravatar imageortollj ( 2023-09-17 19:33:50 +0200 )edit

Just added some information.

Garald gravatar imageGarald ( 2023-09-17 19:49:17 +0200 )edit

I ask myself if you are trolling, if not, look here: plotting tutorial.

ortollj gravatar imageortollj ( 2023-09-18 08:49:10 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2023-09-18 20:14:21 +0200

The first part is easy:

beta = 1/50
P = line([(-0.5, 0), (0.5, 0)])
for i in range(-5, 6):
    P += point((beta + i/11, 0))

tikz is good at letting you position labels well, whereas Sage does not have anything like that built in: you have to place them manually. Do that with the text command. I did not have the patience to come up with anything that looked remotely good, so I have not included that, but you would do P += text("string", (x, y)). The same goes for curved lines connecting points, although you can try to use spline to do that:

for i in range(-5, 2):
    P += plot(spline([(beta + i/11, 0), (beta + (2*i+1)/22, 0.05), (beta + (i+1)/11, 0)]), (beta+i/11, beta+(i+1)/11))
P += plot(spline([(beta - 2/11, 0), (beta - 1, 0), (beta - (1/2 + 2/22), -0.05)]), (beta-1, beta-2/11))
P += plot(spline([(beta - 1, 0), (beta/2 - 1/2, -0.05), (0,0)]), (beta-1, 0))

Then you can adjust the aspect ratio to get it to look how you want:

edit flag offensive delete link more


I would also point out that tikz and latex are designed for presentation and so are good at certain things. Sage is instead focused on computation and so is good at different things. Some aspects of presentation may be awkward in Sage.

John Palmieri gravatar imageJohn Palmieri ( 2023-09-18 22:52:06 +0200 )edit

Right, but wouldn't it be nice if one could embed tikz in Sage, much as one can embed Sage in a LaTeX document...

Garald gravatar imageGarald ( 2023-09-19 14:47:22 +0200 )edit

I think I have to join everyone else in being confused about what you're asking. Are you asking about embedding tikz in Sage? I thought you were asking about reproducing tikz effects but just using native Sage code.

John Palmieri gravatar imageJohn Palmieri ( 2023-09-19 18:43:54 +0200 )edit

As far as I understand, Sage renders LaTeX in the notebook using an installation of MathJax, and MathJax by default does not support tikz.

John Palmieri gravatar imageJohn Palmieri ( 2023-09-19 18:57:13 +0200 )edit

My question is really about reproducing tikz effects in native Sage code, but I am getting the message that doing so is far from straightforward.

Garald gravatar imageGarald ( 2023-09-19 20:14:42 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2023-09-17 14:01:01 +0200

Seen: 191 times

Last updated: Sep 19