Foros del Web » Programando para Internet » Python »

[SOLUCIONADO] Mostrar números eje x

Estas en el tema de Mostrar números eje x en el foro de Python en Foros del Web. El siguiente código traza un papel de probabilidad, pero en el eje x solo se ve el 0 y el 1; cuando le agrego el ...
  #1 (permalink)  
Antiguo 29/03/2013, 18:43
 
Fecha de Ingreso: julio-2011
Mensajes: 62
Antigüedad: 12 años, 8 meses
Puntos: 0
Pregunta Mostrar números eje x

El siguiente código traza un papel de probabilidad, pero en el eje x solo se ve el 0 y el 1; cuando le agrego el signo % a cada uno de los números del eje x muestra un error, por favor como puede solucionarse.
Código:
from __future__ import unicode_literals

import numpy as np
from numpy import ma
from matplotlib import scale as mscale
from matplotlib import transforms as mtransforms
from matplotlib.ticker import Formatter, FixedLocator

class EscalaAcumuladaGumbel(mscale.ScaleBase):

    name = 'gumbel'

    def __init__(self, axis, **kwargs):
        mscale.ScaleBase.__init__(self)
        thresh = kwargs.pop("thresh", 5000)
        if thresh >= 5800.0:
            raise ValueError("thresh must be less than 4800")
        self.thresh = thresh

    def get_transform(self):
        return self.TransformacionAcumuladaGumbel(self.thresh)

    def set_default_locators_and_formatters(self, axis):
        class DegreeFormatter(Formatter):
            def __call__(self, x, pos=None):
                # \u0025 : simbolo %
                return "%d\u00b0" % (x*1.0)
        axis.set_major_locator(FixedLocator(np.arange(-3.0, 3.0, 0.25)))
        axis.set_major_formatter(DegreeFormatter())
        axis.set_minor_formatter(DegreeFormatter())

    def limit_range_for_scale(self, vmin, vmax, minpos):
        return max(vmin, -self.thresh), min(vmax, self.thresh)

    class TransformacionAcumuladaGumbel(mtransforms.Transform):
        input_dims = 1
        output_dims = 1
        is_separable = True

        def __init__(self, thresh):
            mtransforms.Transform.__init__(self)
            self.thresh = thresh

        def transform(self, a):
            masked = ma.masked_where((a < -self.thresh) | (a > self.thresh), a)
            if masked.mask.any():
                return ma.exp(-np.exp(-a))
            else:
                return np.exp(-np.exp(-a))

        def inverted(self):
            return EscalaAcumuladaGumbel.TransformacionInversaAcumuladaGumbel(self.thresh)

    class TransformacionInversaAcumuladaGumbel(mtransforms.Transform):
        input_dims = 1
        output_dims = 1
        is_separable = True

        def __init__(self, thresh):
            mtransforms.Transform.__init__(self)
            self.thresh = thresh

        def transform(self, a):
            return -np.log(-np.log(a))

        def inverted(self):
            return EscalaAcumuladaGumbel.TransformacionAcumuladaGumbel(self.thresh)

mscale.register_scale(EscalaAcumuladaGumbel)


if __name__ == '__main__':
    import matplotlib.pyplot as plt

    Datos = np.array([ 2690.0, 2700.0, 2700.667, 2701.333, 2702.0, 3196.0, 2372.0, 2395.0, 2128.0, 2727.0, 2431.0, 2850.0, 2216.0, 2057.0, 2269.0, 2208.0, 2628.0, 2729.0, 2588.0, 3448.0, 2508.0, 3081.0, 2417.0, 2770.0, 2283.0, 2455.0, 1963.0, 2786.0, 2885.0, 2357.0, 3422.0, 2423.0, 2148.0, 1305.0, 2472.0, 2186.0, 2720.0, 2430.0, 2304.0, 2556.0, 2625.0, 2164.0, 2585.0, ])
    DatosOrdenados = np.sort(Datos)
    mu = 2353.157
    sigma = 297.961

    x1 = ( DatosOrdenados - mu) / sigma
    y1 = (np.exp(-np.exp(-x1)))

    plt.plot(x1, y1, 'ro', lw=2)
    plt.gca().set_xscale('gumbel')

    plt.xlabel('F(z)')
    plt.ylabel('z')
    plt.title('Papel de probabilidad de Gumbel')
    plt.grid(True)

    plt.show()
Código basado en [URL="http://matplotlib.org/examples/api/custom_scale_example.html"]http://matplotlib.org/examples/api/custom_scale_example.html[/URL]
  #2 (permalink)  
Antiguo 29/03/2013, 18:51
Avatar de razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 1 mes
Puntos: 1360
Respuesta: Mostrar números eje x

A mi no me marco error.

Ni con el código que pusiste.

Ni con la modificación que le hice.

Código Python:
Ver original
  1. from __future__ import unicode_literals
  2.  
  3. import numpy as np
  4. from numpy import ma
  5. from matplotlib import scale as mscale
  6. from matplotlib import transforms as mtransforms
  7. from matplotlib.ticker import Formatter, FixedLocator
  8.  
  9. class EscalaAcumuladaGumbel(mscale.ScaleBase):
  10.  
  11.     name = 'gumbel'
  12.  
  13.     def __init__(self, axis, **kwargs):
  14.         mscale.ScaleBase.__init__(self)
  15.         thresh = kwargs.pop("thresh", 5000)
  16.         if thresh >= 5800.0:
  17.             raise ValueError("thresh must be less than 5800")
  18.         self.thresh = thresh
  19.  
  20.     def get_transform(self):
  21.         return self.TransformacionAcumuladaGumbel(self.thresh)
  22.  
  23.     def set_default_locators_and_formatters(self, axis):
  24.         class DegreeFormatter(Formatter):
  25.             def __call__(self, x, pos=None):
  26.                 # \u0025 : simbolo %
  27.                 return "%d%%" % (x*1.0)
  28.         axis.set_major_locator(FixedLocator(np.arange(-3.0, 3.0, 0.25)))
  29.         axis.set_major_formatter(DegreeFormatter())
  30.         axis.set_minor_formatter(DegreeFormatter())
  31.  
  32.     def limit_range_for_scale(self, vmin, vmax, minpos):
  33.         return max(vmin, -self.thresh), min(vmax, self.thresh)
  34.  
  35.     class TransformacionAcumuladaGumbel(mtransforms.Transform):
  36.         input_dims = 1
  37.         output_dims = 1
  38.         is_separable = True
  39.  
  40.         def __init__(self, thresh):
  41.             mtransforms.Transform.__init__(self)
  42.             self.thresh = thresh
  43.  
  44.         def transform(self, a):
  45.             masked = ma.masked_where((a < -self.thresh) | (a > self.thresh), a)
  46.             if masked.mask.any():
  47.                 return ma.exp(-np.exp(-a))
  48.             else:
  49.                 return np.exp(-np.exp(-a))
  50.  
  51.         def inverted(self):
  52.             return EscalaAcumuladaGumbel.TransformacionInversaAcumuladaGumbel(self.thresh)
  53.  
  54.     class TransformacionInversaAcumuladaGumbel(mtransforms.Transform):
  55.         input_dims = 1
  56.         output_dims = 1
  57.         is_separable = True
  58.  
  59.         def __init__(self, thresh):
  60.             mtransforms.Transform.__init__(self)
  61.             self.thresh = thresh
  62.  
  63.         def transform(self, a):
  64.             return -np.log(-np.log(a))
  65.  
  66.         def inverted(self):
  67.             return EscalaAcumuladaGumbel.TransformacionAcumuladaGumbel(self.thresh)
  68.  
  69. mscale.register_scale(EscalaAcumuladaGumbel)
  70.  
  71.  
  72. if __name__ == '__main__':
  73.     import matplotlib.pyplot as plt
  74.  
  75.     Datos = np.array([ 2690.0, 2700.0, 2700.667, 2701.333, 2702.0, 3196.0, 2372.0, 2395.0, 2128.0, 2727.0, 2431.0, 2850.0, 2216.0, 2057.0, 2269.0, 2208.0, 2628.0, 2729.0, 2588.0, 3448.0, 2508.0, 3081.0, 2417.0, 2770.0, 2283.0, 2455.0, 1963.0, 2786.0, 2885.0, 2357.0, 3422.0, 2423.0, 2148.0, 1305.0, 2472.0, 2186.0, 2720.0, 2430.0, 2304.0, 2556.0, 2625.0, 2164.0, 2585.0, ])
  76.     DatosOrdenados = np.sort(Datos)
  77.     mu = 2353.157
  78.     sigma = 297.961
  79.  
  80.     x1 = ( DatosOrdenados - mu) / sigma
  81.     y1 = (np.exp(-np.exp(-x1)))
  82.  
  83.     plt.plot(x1, y1, 'ro', lw=2)
  84.     plt.gca().set_xscale('gumbel')
  85.  
  86.     plt.xlabel('F(z)')
  87.     plt.ylabel('z')
  88.     plt.title('Papel de probabilidad de Gumbel')
  89.     plt.grid(True)
  90.  
  91.     plt.show()

Estoy usando python 2.7
  #3 (permalink)  
Antiguo 29/03/2013, 20:32
 
Fecha de Ingreso: julio-2011
Mensajes: 62
Antigüedad: 12 años, 8 meses
Puntos: 0
Respuesta: Mostrar números eje x

No sabia que se podia usar "%d%%", en el eje x mostraba el 0 debido a que los valores eran menores a 1, se aumentaron lineas para rotar los numeros y mostrarlos con 3 decimales.
Gracias por responder.
Código:
from __future__ import unicode_literals

import numpy as np
from numpy import ma
from matplotlib import scale as mscale
from matplotlib import transforms as mtransforms
from matplotlib.ticker import Formatter, FixedLocator, FuncFormatter

class EscalaAcumuladaGumbel(mscale.ScaleBase):

    name = 'gumbel'

    def __init__(self, axis, **kwargs):
        mscale.ScaleBase.__init__(self)
        thresh = kwargs.pop("thresh", 5000)
        if thresh >= 5800.0:
            raise ValueError("thresh must be less than 5800")
        self.thresh = thresh

    def get_transform(self):
        return self.TransformacionAcumuladaGumbel(self.thresh)

    def set_default_locators_and_formatters(self, axis):
        class DegreeFormatter(Formatter):
            def __call__(self, x, pos=None):
                # \u0025 : simbolo %
                return "%d%%" % (x*0.02)
        axis.set_major_locator(FixedLocator(np.arange(0.0, 1.0, 0.02)))
        axis.set_major_formatter(DegreeFormatter())
        axis.set_minor_formatter(DegreeFormatter())

    def limit_range_for_scale(self, vmin, vmax, minpos):
        return max(vmin, -self.thresh), min(vmax, self.thresh)

    class TransformacionAcumuladaGumbel(mtransforms.Transform):
        input_dims = 1
        output_dims = 1
        is_separable = True

        def __init__(self, thresh):
            mtransforms.Transform.__init__(self)
            self.thresh = thresh

        def transform(self, a):
            masked = ma.masked_where((a < -self.thresh) | (a > self.thresh), a)
            if masked.mask.any():
                return ma.exp(-np.exp(-a))
            else:
                return np.exp(-np.exp(-a))

        def inverted(self):
            return EscalaAcumuladaGumbel.TransformacionInversaAcumuladaGumbel(self.thresh)

    class TransformacionInversaAcumuladaGumbel(mtransforms.Transform):
        input_dims = 1
        output_dims = 1
        is_separable = True

        def __init__(self, thresh):
            mtransforms.Transform.__init__(self)
            self.thresh = thresh

        def transform(self, a):
            return -np.log(-np.log(a))

        def inverted(self):
            return EscalaAcumuladaGumbel.TransformacionAcumuladaGumbel(self.thresh)

mscale.register_scale(EscalaAcumuladaGumbel)


if __name__ == '__main__':
    import matplotlib.pyplot as plt

    Datos = np.array([ 2690.0, 2700.0, 2700.667, 2701.333, 2702.0, 3196.0, 2372.0, 2395.0, 2128.0, 2727.0, 2431.0, 2850.0, 2216.0, 2057.0, 2269.0, 2208.0, 2628.0, 2729.0, 2588.0, 3448.0, 2508.0, 3081.0, 2417.0, 2770.0, 2283.0, 2455.0, 1963.0, 2786.0, 2885.0, 2357.0, 3422.0, 2423.0, 2148.0, 1305.0, 2472.0, 2186.0, 2720.0, 2430.0, 2304.0, 2556.0, 2625.0, 2164.0, 2585.0, ])
    DatosOrdenados = np.sort(Datos)
    mu = 2353.157
    sigma = 297.961

    y1 = ( DatosOrdenados - mu) / sigma
    x1 = np.exp(-np.exp(-y1))

    plt.plot(x1, y1, 'ro', lw=2)
    plt.gca().set_xscale('gumbel')

    plt.xlabel('F(z)')
    plt.ylabel('z')
    plt.title('Papel de probabilidad de Gumbel')
    plt.xticks(rotation='vertical', fontsize=7)
    def form3(x, pos):
        return '%.3f' % x
    formatter = FuncFormatter(form3)
    plt.gca().xaxis.set_major_formatter(FuncFormatter(formatter))
    plt.grid(True)

    plt.show()

Última edición por Bael_Balzac; 29/03/2013 a las 21:58

Etiquetas: mac
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:07.