Ask Your Question

Using base 10 exponent of a number as label in a contour plot

asked 2020-09-28 08:00:42 +0200

Wraith-of-Seth gravatar image

I am currently trying to improve the labelling of a contour plot. As contours I'm only using negative powers of ten, so I hope to have a simple LaTeX label of the form, say, 10^{-23}. My starting point was one of the examples at the help page for contour_plot (here was the link to the sage documentation). After various failed attempts with log(x, 10), I found in the question "Integer types and log()" (that would be question id(?) 28679/integer-types-and-log) an example using valuation(x,10). However, the results seem odd. Below the slightly simplified thing I try to plot.

var('x y')
rp = 6.77048070412999e-10*(x + sqrt(x^2-y^2*x^2))
sigma0 = 1.44008750449331e-18*(3*x + sqrt(9*x^2 - 8*y^2*x^2))^4/(8*(3*x^2 - 2*y^2*x^2 + x*sqrt(9*x^2 - 8*y^2*x^2)))
f(x,y) = (1.12469227142351e-70*y*x^2/(rp)^6*exp(-(rp)^2/(sqrt(y)*x*0.169262017603250))-1.65389403372211e-162*(sqrt(x^2-y*x^2))^3*sigma0*(x-2*sqrt(x^2-y*x^2))/(rp)^10)
contour_plot(f(x,y), (x,0,100), (y,0,1), contours=[0,1e-25,1e-24,1e-23,1e-22,1e-21,1e-20,1e-19],aspect_ratio='automatic',labels=True, label_fmt=lambda x: r'''$10^{-%s}$'''%(valuation(1/x,10)), fill=False)

If I do this, I get 10^{-0} for several contours.

So, I checked, what valuation does to the numbers:




If I'm using something like

label_fmt=lambda x: r'''$10^{-%s}$'''%(log(1/x,10))

instead, I get only the message "Graphics object consisting of 1 graphics primitive", and

label_fmt=lambda x: r'''$10^{-%s}$'''%(log(Integer(1/x),10))

yields another plot with odd labels - which have the odd behaviour at the same exponents as the example with "valuation", but I don't know why.

I'm currently doing this mostly in a jupyter notebook on Windows 10 with Sage 9.1.

Am I misunderstanding the use of "valuation" or log(x,10) in this case? Did I run into a bug? How can I get the exponents I'm after?

edit retag flag offensive close merge delete


Welcome to Ask Sage! Thank you for your question!

slelievre gravatar imageslelievre ( 2020-09-28 12:19:39 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-09-28 14:06:57 +0200

eric_g gravatar image

Because Sage decimal numbers (elements of RR) do not have a proper format, I would recommend to transform them into a Python float before sending them to a string. In your case, you could use:

label_fmt = lambda x: r"$10^{{{0:.3g}}}$".format(float(log(x, 10)))

It yields

sage: contours=[1e-25,1e-24,1e-23,1e-22,1e-21,1e-20,1e-19]
sage: [label_fmt(c) for c in contours]

NB: of course, 0 deserves a special treatment and has been excluded from the above list.

edit flag offensive delete link more


Thank you, also for picking up the possible "0" issue. I just had to import log10 from numpy to avoid some type errors when I introduced this into the contour_plot.

Wraith-of-Seth gravatar imageWraith-of-Seth ( 2020-09-28 14:57:37 +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

1 follower


Asked: 2020-09-28 08:00:42 +0200

Seen: 345 times

Last updated: Sep 28 '20