ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 28 Sep 2020 14:57:37 +0200Using base 10 exponent of a number as label in a contour plothttps://ask.sagemath.org/question/53631/using-base-10-exponent-of-a-number-as-label-in-a-contour-plot/ 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:
print(valuation(1/(1e-19),10))
print(valuation(1/(1e-20),10))
print(valuation(1/(1e-21),10))
print(valuation(1/(1e-22),10))
print(valuation(1/(1e-23),10))
yielding
19
20
0
22
0
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?Mon, 28 Sep 2020 08:00:42 +0200https://ask.sagemath.org/question/53631/using-base-10-exponent-of-a-number-as-label-in-a-contour-plot/Comment by slelievre for <p>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.</p>
<pre><code>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)
</code></pre>
<p>If I do this, I get 10^{-0} for several contours.</p>
<p>So, I checked, what valuation does to the numbers:</p>
<pre><code>print(valuation(1/(1e-19),10))
print(valuation(1/(1e-20),10))
print(valuation(1/(1e-21),10))
print(valuation(1/(1e-22),10))
print(valuation(1/(1e-23),10))
</code></pre>
<p>yielding</p>
<pre><code>19
20
0
22
0
</code></pre>
<p>If I'm using something like </p>
<pre><code>label_fmt=lambda x: r'''$10^{-%s}$'''%(log(1/x,10))
</code></pre>
<p>instead, I get only the message "Graphics object consisting of 1 graphics primitive", and </p>
<pre><code>label_fmt=lambda x: r'''$10^{-%s}$'''%(log(Integer(1/x),10))
</code></pre>
<p>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.</p>
<p>I'm currently doing this mostly in a jupyter notebook on Windows 10 with Sage 9.1.</p>
<p>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?</p>
https://ask.sagemath.org/question/53631/using-base-10-exponent-of-a-number-as-label-in-a-contour-plot/?comment=53635#post-id-53635Welcome to Ask Sage! Thank you for your question!Mon, 28 Sep 2020 12:19:39 +0200https://ask.sagemath.org/question/53631/using-base-10-exponent-of-a-number-as-label-in-a-contour-plot/?comment=53635#post-id-53635Answer by eric_g for <p>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.</p>
<pre><code>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)
</code></pre>
<p>If I do this, I get 10^{-0} for several contours.</p>
<p>So, I checked, what valuation does to the numbers:</p>
<pre><code>print(valuation(1/(1e-19),10))
print(valuation(1/(1e-20),10))
print(valuation(1/(1e-21),10))
print(valuation(1/(1e-22),10))
print(valuation(1/(1e-23),10))
</code></pre>
<p>yielding</p>
<pre><code>19
20
0
22
0
</code></pre>
<p>If I'm using something like </p>
<pre><code>label_fmt=lambda x: r'''$10^{-%s}$'''%(log(1/x,10))
</code></pre>
<p>instead, I get only the message "Graphics object consisting of 1 graphics primitive", and </p>
<pre><code>label_fmt=lambda x: r'''$10^{-%s}$'''%(log(Integer(1/x),10))
</code></pre>
<p>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.</p>
<p>I'm currently doing this mostly in a jupyter notebook on Windows 10 with Sage 9.1.</p>
<p>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?</p>
https://ask.sagemath.org/question/53631/using-base-10-exponent-of-a-number-as-label-in-a-contour-plot/?answer=53637#post-id-53637Because 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]
['$10^{-25}$',
'$10^{-24}$',
'$10^{-23}$',
'$10^{-22}$',
'$10^{-21}$',
'$10^{-20}$',
'$10^{-19}$']
NB: of course, `0` deserves a special treatment and has been excluded from the above list.
Mon, 28 Sep 2020 14:06:57 +0200https://ask.sagemath.org/question/53631/using-base-10-exponent-of-a-number-as-label-in-a-contour-plot/?answer=53637#post-id-53637Comment by Wraith-of-Seth for <p>Because Sage decimal numbers (elements of <code>RR</code>) do not have a proper format, I would recommend to transform them into a Python <code>float</code> before sending them to a string. In your case, you could use:</p>
<pre><code>label_fmt = lambda x: r"$10^{{{0:.3g}}}$".format(float(log(x, 10)))
</code></pre>
<p>It yields</p>
<pre><code>sage: contours=[1e-25,1e-24,1e-23,1e-22,1e-21,1e-20,1e-19]
sage: [label_fmt(c) for c in contours]
['$10^{-25}$',
'$10^{-24}$',
'$10^{-23}$',
'$10^{-22}$',
'$10^{-21}$',
'$10^{-20}$',
'$10^{-19}$']
</code></pre>
<p>NB: of course, <code>0</code> deserves a special treatment and has been excluded from the above list.</p>
https://ask.sagemath.org/question/53631/using-base-10-exponent-of-a-number-as-label-in-a-contour-plot/?comment=53639#post-id-53639Thank 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.Mon, 28 Sep 2020 14:57:37 +0200https://ask.sagemath.org/question/53631/using-base-10-exponent-of-a-number-as-label-in-a-contour-plot/?comment=53639#post-id-53639