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.Thu, 26 Jan 2012 10:04:52 +0100Decrease accuracy for floats (decrease decimal places reported)https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/When working with matrices of floats and plotting said matrices, the results become unreadable fast with the default accuracy. How do I only display a handful (3~4) of digits?Tue, 24 Jan 2012 18:33:23 +0100https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/Comment by richtopia for <p>When working with matrices of floats and plotting said matrices, the results become unreadable fast with the default accuracy. How do I only display a handful (3~4) of digits?</p>
https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?comment=20422#post-id-20422As an update, the quickest method of beautifying my matrices was taking jdc's advice. I'm plotting Markov Chains, which I should have mentioned earlier, however n.(digits) can be used even inside DiGraph. For example: DiGraph(My_Matrix.n(digits=3),format="weighted_adjacency_matrix")Wed, 25 Jan 2012 19:33:54 +0100https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?comment=20422#post-id-20422Answer by Volker Braun for <p>When working with matrices of floats and plotting said matrices, the results become unreadable fast with the default accuracy. How do I only display a handful (3~4) of digits?</p>
https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?answer=13194#post-id-13194You can round floating-point matrices easily:
sage: random_matrix(RDF,4)
[ 0.249460263401 0.560301434409 -0.663582797018 0.932897872107]
[ 0.334365993609 -0.868997217291 -0.546936618217 -0.636108817037]
[ -0.555384416581 0.961681767214 -0.146447558808 0.700186827417]
[-0.0701126985289 0.611616686035 0.890061111172 -0.22607396116]
sage: random_matrix(RDF,4).round(3)
[ 0.246 0.673 -0.168 0.239]
[ 0.869 -0.043 0.646 -0.36]
[ 0.467 -0.309 0.25 -0.35]
[ 0.508 0.671 -0.597 -0.583]
Wed, 25 Jan 2012 19:52:50 +0100https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?answer=13194#post-id-13194Answer by kcrisman for <p>When working with matrices of floats and plotting said matrices, the results become unreadable fast with the default accuracy. How do I only display a handful (3~4) of digits?</p>
https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?answer=13191#post-id-13191This is just Shashank's answer partly converted to Sage.
sage: P = plot(sin(0.1*pi*x)*e^(-.01*x),(x,0,100))
sage: from matplotlib.ticker import FormatStrFormatter
sage: xFormatter = FormatStrFormatter('%d')
sage: yFormatter = FormatStrFormatter('%.2f')
sage: P.show(tick_formatter=[xFormatter,yFormatter])
It's definitely possible to format things quite a bit from within Sage if you want to. See the [show()](http://www.sagemath.org/doc/reference/sage/plot/plot.html#sage.plot.plot.Graphics.show) documentation.
However, this probably doesn't answer the original question about matrices of floats.Wed, 25 Jan 2012 10:59:10 +0100https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?answer=13191#post-id-13191Answer by Shashank for <p>When working with matrices of floats and plotting said matrices, the results become unreadable fast with the default accuracy. How do I only display a handful (3~4) of digits?</p>
https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?answer=13187#post-id-13187I normally don't use sage's default plotting. But, I have pasted below a matplotlib example I found online and modified a few lines for clarity. The format of the x and y ticks is given is c-style format. Please let me know if you have any question. In general matplotlib is far more versatile than sage's default plotter and I use is to make plots.
from pylab import *
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
majorLocator = MultipleLocator(20)
xFormatter = FormatStrFormatter('%d')
yFormatter = FormatStrFormatter('%.2f')
minorLocator = MultipleLocator(5)
t = arange(0.0, 100.0, 0.1)
s = sin(0.1*pi*t)*exp(-t*0.01)
ax = subplot(111)
plot(t,s)
ax.xaxis.set_major_locator(majorLocator)
ax.xaxis.set_major_formatter(xFormatter)
ax.yaxis.set_major_formatter(yFormatter)
#for the minor ticks, use no labels; default NullFormatter
ax.xaxis.set_minor_locator(minorLocator)
savefig('test.png')Tue, 24 Jan 2012 20:47:59 +0100https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?answer=13187#post-id-13187Comment by kcrisman for <p>I normally don't use sage's default plotting. But, I have pasted below a matplotlib example I found online and modified a few lines for clarity. The format of the x and y ticks is given is c-style format. Please let me know if you have any question. In general matplotlib is far more versatile than sage's default plotter and I use is to make plots.</p>
<pre><code>from pylab import *
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
majorLocator = MultipleLocator(20)
xFormatter = FormatStrFormatter('%d')
yFormatter = FormatStrFormatter('%.2f')
minorLocator = MultipleLocator(5)
t = arange(0.0, 100.0, 0.1)
s = sin(0.1*pi*t)*exp(-t*0.01)
ax = subplot(111)
plot(t,s)
ax.xaxis.set_major_locator(majorLocator)
ax.xaxis.set_major_formatter(xFormatter)
ax.yaxis.set_major_formatter(yFormatter)
#for the minor ticks, use no labels; default NullFormatter
ax.xaxis.set_minor_locator(minorLocator)
savefig('test.png')
</code></pre>
https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?comment=20423#post-id-20423If you are serious about absolute control in simplification and symbolics, use Maxima directly (or its methods) in Sage; similarly for plotting and matplotlib. It depends on how power of a user you are.Wed, 25 Jan 2012 10:57:09 +0100https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?comment=20423#post-id-20423Answer by jdc for <p>When working with matrices of floats and plotting said matrices, the results become unreadable fast with the default accuracy. How do I only display a handful (3~4) of digits?</p>
https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?answer=13192#post-id-13192Do you mean printing (instead of plotting) the matrices? (Not sure what it would mean to plot a matrix.) If you're just trying to get a floating point number to display with fewer digits, you might try something like
foo.n(digits = 3)
Unfortunately, this simple approach doesn't always give expressions of a uniform "width", in terms of the number of characters. For example:
print pi.n(digits = 3)
print (pi*100000).n(digits = 3)
print (pi*100000000).n(digits = 3)
print (pi/100000).n(digits = 3)
print (pi/100000000).n(digits = 3)
returns
3.14
314000.
3.14e8
0.0000314
3.14e-8Wed, 25 Jan 2012 17:37:41 +0100https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?answer=13192#post-id-13192Comment by jdc for <p>Do you mean printing (instead of plotting) the matrices? (Not sure what it would mean to plot a matrix.) If you're just trying to get a floating point number to display with fewer digits, you might try something like</p>
<pre><code>foo.n(digits = 3)
</code></pre>
<p>Unfortunately, this simple approach doesn't always give expressions of a uniform "width", in terms of the number of characters. For example:</p>
<pre><code>print pi.n(digits = 3)
print (pi*100000).n(digits = 3)
print (pi*100000000).n(digits = 3)
print (pi/100000).n(digits = 3)
print (pi/100000000).n(digits = 3)
</code></pre>
<p>returns</p>
<pre><code>3.14
314000.
3.14e8
0.0000314
3.14e-8
</code></pre>
https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?comment=20420#post-id-20420Ah... I see. Thanks.Thu, 26 Jan 2012 10:04:52 +0100https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?comment=20420#post-id-20420Comment by kcrisman for <p>Do you mean printing (instead of plotting) the matrices? (Not sure what it would mean to plot a matrix.) If you're just trying to get a floating point number to display with fewer digits, you might try something like</p>
<pre><code>foo.n(digits = 3)
</code></pre>
<p>Unfortunately, this simple approach doesn't always give expressions of a uniform "width", in terms of the number of characters. For example:</p>
<pre><code>print pi.n(digits = 3)
print (pi*100000).n(digits = 3)
print (pi*100000000).n(digits = 3)
print (pi/100000).n(digits = 3)
print (pi/100000000).n(digits = 3)
</code></pre>
<p>returns</p>
<pre><code>3.14
314000.
3.14e8
0.0000314
3.14e-8
</code></pre>
https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?comment=20421#post-id-20421Totally off topic, but for matrix plots see http://www.sagemath.org/doc/reference/sage/plot/matrix_plot.html and http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.imshowWed, 25 Jan 2012 21:54:56 +0100https://ask.sagemath.org/question/7549/decrease-accuracy-for-floats-decrease-decimal-places-reported/?comment=20421#post-id-20421