# How to plot the real line in SageMath

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:

\begin{tikzpicture}
\def\scale{2}
\def\bet{0.2}
\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);
\end{tikzpicture}


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 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.

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)])


Sort by » oldest newest most voted 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:

P.set_aspect_ratio(2)

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.

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

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.

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

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.