Loading...

matplotlib-users@lists.sourceforge.net

[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: [Matplotlib-users] ScalarFormatter Gökhan Sever Mon Feb 20 19:00:15 2012

Hello,

Stealing a solution from ->
http://old.nabble.com/scientific-notation-in-ticklabels-for-linear-plot-td29993489.html

This seems to produce nicer looking y tick-labels. I tend to switch to
log-scale in cases like yours, but this one provides a clean solution as
well.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

def scinot(x,pos=None):
    if x == 0:
        s = '0'
    else:
        xp = int(np.floor(np.log10(np.abs(x))))
        mn = x/10.**xp
        # Here we truncate to 2 significant digits -- may not be enough
        # in all cases
        s = '$'+str('%.3f'%mn) +'\\times 10^{'+str(xp)+'}$'
        return s

A = np.array([[  1.00000000e+00,   0.00000000e+00,  1.99236400e-04],
      [  2.00000000e+00,   1.00000000e+00,  2.00043800e-04],
      [  3.00000000e+00,   2.00000000e+00,  2.00046000e-04],
      [  4.00000000e+00,   3.00000000e+00,  2.00043900e-04]])


fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(A[:,1],A[:,2])
ax.yaxis.set_major_formatter(FuncFormatter(scinot))
y_min,y_max = ax.get_ylim()
print 'y range', y_max-y_min
plt.show()


On Mon, Feb 20, 2012 at 8:32 AM, Nils Wagner
<[EMAIL PROTECTED]>wrote:

> Hi all,
>
> How can I improve the presentation of yticks for a small y range, e.g.
>
>  y_min,y_max = ax.get_ylim()
>>>
>>>> y_max-y_min
>>>>
>>> 8.9999999999996767e-07
>
> I would like to avoid "+1.992 x 10^{-4}" in the attached figure.
>
> import numpy as np
> import matplotlib.pyplot as plt
> from matplotlib.ticker import ScalarFormatter
> formatter = ScalarFormatter(useMathText=**True)
> formatter.set_scientific(True)
> formatter.set_powerlimits((-**15,15))
> #A = np.loadtxt('trash.dat')
> A = np.array([[  1.00000000e+00,   0.00000000e+00,  1.99236400e-04],
>       [  2.00000000e+00,   1.00000000e+00,  2.00043800e-04],
>       [  3.00000000e+00,   2.00000000e+00,  2.00046000e-04],
>       [  4.00000000e+00,   3.00000000e+00,  2.00043900e-04]])
>
>
> fig = plt.figure()
> ax = fig.add_subplot(111)
> ax.yaxis.set_major_formatter(**formatter)
> ax.plot(A[:,1],A[:,2])
> y_min,y_max = ax.get_ylim()
> print 'y range', y_max-y_min
> plt.show()
>
>
> Nils
>
>
> ------------------------------------------------------------------------------
> Try before you buy = See our experts in action!
> The most comprehensive online learning library for Microsoft developers
> is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
> Metro Style Apps, more. Free future releases when you subscribe now!
> http://p.sf.net/sfu/learndevnow-dev2
> _______________________________________________
> Matplotlib-users mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users
>
>


-- 
Gökhan
------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
Matplotlib-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/matplotlib-users