Ask Your Question
0

Ticks formatter rounding

asked 2023-08-09 18:34:48 +0100

JC gravatar image

Hi all,

On sage 10.0, the code

var('x,y')
f(x,y) = sqrt(x^2 + y^2)
C = contour_plot(f ,(-0.2,0.2), (-0.2,0.2), axes = True, cmap = "Spectral", 
            labels = True,  label_fmt=lambda x: "$%s$"%round(x,2), label_colors="black", 
            label_inline=True,
            label_fontsize=16,
            gridlines=True, axes_labels=["$x$","$y$"],
            ticks = [[-0.2,-0.1,0,0.1,0.2],[-0.2,-0.1,0,0.1,0.2]],
            tick_formatter = "latex", fontsize= 16)

produces a figure in which the ticks markers are more like -0.200000000000, see the figure. I cannot figure out how to print a nice -0.2 (using latex) instead.

Any ideas? Thanks in advance!

image description

edit retag flag offensive close merge delete

Comments

as a workaround I suggest this, but it does not quite answer your question :

var('x,y')
f(x,y) = sqrt(x^2 + y^2)
C = contour_plot(f ,(-0.2,0.2), (-0.2,0.2), axes = True, cmap = "Spectral", 
            labels = True,  label_fmt=lambda x: "$%s$"%round(x,2), label_colors="black", 
            label_inline=True,
            label_fontsize=16,
            gridlines=True, axes_labels=["$x$","$y$"],
            ticks = [[-1/5,-1/10,0,1/10,2/10],[-1/5,-1/10,0,1/10,1/5]],               
            tick_formatter = "latex", fontsize= 16)
ortollj gravatar imageortollj ( 2023-08-10 06:09:19 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2023-08-10 09:47:47 +0100

This seems a side effect of the default type for real values.

sage: a = 0.2; a; type(a)
0.200000000000000
<class 'sage.rings.real_mpfr.RealLiteral'>
sage: b = round(a, 1); b; type(b)
0.2
<class 'sage.rings.real_double_element_gsl.RealDoubleElement_gsl'>
sage: c = float(a); c; type(c)
0.2
<class 'float'>

Forcing values to float, or using round, produces what you expect:

TF = lambda L:list(map(float, L))
var('x,y')
f(x,y) = sqrt(x^2 + y^2)
C = contour_plot(f ,(-0.2,0.2), (-0.2,0.2), axes = True, cmap = "Spectral", 
            labels = True,  label_fmt=lambda x: "$%s$"%round(x,2), label_colors="black", 
            label_inline=True,
            label_fontsize=16,
            gridlines=True, axes_labels=["$x$","$y$"],
            ticks = [TF([-0.2,-0.1,0,0.1,0.2]), TF([-0.2,-0.1,0,0.1,0.2])],
            tick_formatter = "latex", fontsize= 16)
C.show()

image description

edit flag offensive delete link more
0

answered 2023-08-10 06:29:23 +0100

ortollj gravatar image

updated 2023-08-10 06:31:13 +0100

or maybe this , just remove tickformatter!

var('x,y')
f(x,y) = sqrt(x^2 + y^2)
C = contour_plot(f ,(-0.2,0.2), (-0.2,0.2), axes = True, cmap = "Spectral", 
            labels = True,  label_fmt=lambda x: "$%s$"%round(x,2), label_colors="black", 
            label_inline=True,
            label_fontsize=16,
            gridlines=True, axes_labels=["$x$","$y$"],

            #ticks = [[-2/10,-1/10,0,1/10,2/10],[-2/10,-1/10,0,1/10,2/10]],
            ticks = [[-0.2,-0.1,0,0.1,0.2],[-0.2,-0.1,0,0.1,0.2]],
            fontsize= 16)
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2023-08-09 18:34:48 +0100

Seen: 216 times

Last updated: Aug 10 '23