# 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?

edit retag close merge delete

As 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")

( 2012-01-25 19:33:54 +0200 )edit

Sort by » oldest newest most voted

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

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-8

more

Ah... I see. Thanks.

( 2012-01-26 10:04:52 +0200 )edit

This 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() documentation.

However, this probably doesn't answer the original question about matrices of floats.

more

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.

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

more

If 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.

( 2012-01-25 10:57:09 +0200 )edit

You 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]

more