# Ticks formatter rounding

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.

edit retag close merge delete

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)

( 2023-08-10 06:09:19 +0200 )edit

Sort by ยป oldest newest most voted

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)

more

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


more