0

Estaba intentando hacer un gráfico en el cual mostrar valores asociados a una etiqueta (variable A), con el propósito de visualizar los valores ascendentes (eje y) en función de su etiqueta (eje x). Mi variable A:

A = array([[  1.        ,   1.51545416],
   [  2.        ,   0.92105152],
   [  3.        ,   1.32540165],
   [  4.        ,   1.53666124],
   [  5.        ,   1.24074641],
   [  6.        ,   1.22161184],
   [  7.        ,   1.18863495],
   [  8.        ,   2.79575549],
   [  9.        ,   1.39858833],
   [ 10.        ,   1.67070284],
   [ 11.        ,   1.68800135],
   [ 12.        ,   1.26665515],
   [ 13.        ,   1.70171469],
   [ 14.        ,   1.35315168],
   [ 15.        ,   1.19747605],
   [ 16.        ,   1.73701212],
   [ 17.        ,   1.5814438 ],
   [ 18.        ,   1.83155713],
   [ 19.        ,   1.4108153 ],
   [ 20.        ,   1.56890976],
   [ 21.        ,   1.93348536],
   [ 22.        ,   1.66430381],
   [ 23.        ,   2.12699475],
   [ 24.        ,   1.17939465],
   [ 25.        ,   1.49588583],
   [ 26.        ,   1.67787019],
   [ 27.        ,   1.86811636],
   [ 28.        ,   1.91399636],
   [ 29.        ,   1.54877607],
   [ 30.        ,   1.655113  ],
   [ 31.        ,   1.90556009],
   [ 32.        ,   2.24303354],
   [ 33.        ,   1.90962274],
   [ 34.        ,   2.46928062],
   [ 35.        ,   1.53419281],
   [ 36.        ,   1.87549598],
   [ 37.        ,   2.08298678],
   [ 38.        ,   2.57443832],
   [ 39.        ,   1.12708797],
   [ 40.        ,   1.5808429 ],
   [ 41.        ,   2.61126354],
   [ 42.        ,   2.95227462],
   [ 43.        ,   1.9364137 ],
   [ 44.        ,   2.94897491],
   [ 45.        ,   1.68425785],
   [ 46.        ,   2.31623894],
   [ 47.        ,   1.56903689],
   [ 48.        ,   3.8287992 ],
   [ 49.        ,   4.05121551],
   [ 50.        ,   1.33463897],
   [ 51.        ,   1.44309968],
   [ 52.        ,   1.55667113],
   [ 53.        ,   2.68976388],
   [ 54.        ,   2.56148278],
   [ 55.        ,   3.28133842],
   [ 56.        ,   3.33924649],
   [ 57.        ,   1.30164185],
   [ 58.        ,   0.72855339],
   [ 59.        ,   1.86037849],
   [ 60.        ,   2.65273098],
   [ 61.        ,   0.76011003],
   [ 62.        ,   1.88334334],
   [ 63.        ,   1.93361464],
   [ 64.        ,   1.6036668 ],
   [ 65.        ,   2.02444607],
   [ 66.        ,   1.32850223],
   [ 67.        ,   1.47564139],
   [ 68.        ,   2.03729127],
   [ 69.        ,   1.47978079],
   [ 70.        ,   2.02114407],
   [ 71.        ,   1.43295138],
   [ 72.        ,   1.97968055],
   [ 73.        ,   2.71543594],
   [ 74.        ,   6.84955878],
   [ 75.        ,   1.58720266],
   [ 76.        ,   1.65231254],
   [ 77.        ,   1.86028445],
   [ 78.        ,   1.78758744],
   [ 79.        ,   3.60057309],
   [ 80.        ,   1.6043282 ],
   [ 81.        ,   2.02946608],
   [ 82.        ,   1.84933445],
   [ 83.        ,   6.02552801],
   [ 84.        ,   7.05783584],
   [ 85.        ,   6.42081655],
   [ 86.        ,   4.53304139],
   [ 87.        ,   4.67289848],
   [ 88.        ,   2.18723446],
   [ 89.        ,   2.42949039],
   [ 90.        ,   3.07777225],
   [ 91.        ,   1.65519607],
   [ 92.        ,   3.05168227],
   [ 93.        ,   2.5264844 ],
   [ 94.        ,   0.        ],
   [ 95.        ,   2.11647051],
   [ 96.        ,   2.27729623],
   [ 97.        ,   2.38029384],
   [ 98.        ,   1.3533427 ],
   [ 99.        ,   5.76654075],
   [100.        ,   3.36332311],
   [101.        ,   2.0966741 ],
   [102.        ,   1.35803666],
   [103.        ,   1.93339371],
   [104.        ,   1.31171455],
   [105.        ,   1.17387829],
   [106.        ,   2.81503828],
   [107.        ,   5.82386601],
   [108.        ,   5.20974647],
   [109.        ,   9.3757877 ],
   [110.        ,   4.67747407],
   [111.        ,   0.88059179],
   [112.        ,   1.8125935 ],
   [113.        ,   1.87670035],
   [114.        ,   1.5993903 ],
   [115.        ,   1.74148821],
   [116.        ,   1.40802643],
   [117.        ,   2.24655088],
   [118.        ,   1.25664942],
   [119.        ,   1.37730095],
   [120.        ,   1.77298028],
   [121.        ,   1.53828835],
   [122.        ,   1.24318497],
   [123.        ,   1.38390501],
   [124.        ,   2.74341465],
   [125.        ,   1.73789458],
   [126.        ,   1.50795386],
   [127.        ,   1.5833537 ],
   [128.        ,   0.74709751],
   [129.        ,   1.31198555],
   [130.        ,   2.99645827],
   [131.        ,   1.15523068],
   [132.        ,   1.63511695],
   [133.        ,   1.38385385],
   [134.        ,   1.39873976],
   [135.        ,   0.97887727],
   [136.        ,   1.06433057],
   [137.        ,   1.3325748 ],
   [138.        ,   1.15240602],
   [139.        ,   2.70389181],
   [140.        ,   2.23183602],
   [141.        ,   1.16443398],
   [142.        ,   3.67429258],
   [143.        ,   1.80352927],
   [144.        ,   1.93624837],
   [145.        ,   1.58981035],
   [146.        ,   1.85537995],
   [147.        ,   2.17859119],
   [148.        ,   1.90558106],
   [149.        ,   1.63352479],
   [150.        ,   1.16737204],
   [151.        ,   1.21850403],
   [152.        ,   1.08606747],
   [153.        ,   1.09289736],
   [154.        ,   1.7301665 ],
   [155.        ,   0.91306907],
   [156.        ,   1.23034401],
   [157.        ,   1.29756661],
   [158.        ,   1.55718317],
   [159.        ,   2.34227336],
   [160.        ,   1.92048523],
   [161.        ,   2.94140732],
   [162.        ,   0.97855339],
   [163.        ,   1.73904682],
   [164.        ,   4.27600858],
   [165.        ,   1.35244229],
   [166.        ,   1.42772753],
   [167.        ,   1.63497882],
   [168.        ,   2.99290745],
   [169.        ,   2.1888843 ],
   [170.        ,   2.88079347],
   [171.        ,   2.16064988],
   [172.        ,   1.94023665],
   [173.        ,   2.82229232],
   [174.        ,   1.20701602],
   [175.        ,   1.07079971],
   [176.        ,   3.33874121],
   [177.        ,   1.56863437],
   [178.        ,   1.66251272],
   [179.        ,   1.99321738],
   [180.        ,   2.519336  ],
   [181.        ,   1.20734984],
   [182.        ,   2.39362749],
   [183.        ,   1.34408252],
   [184.        ,   1.2112659 ],
   [185.        ,   1.8962926 ],
   [186.        ,   3.05109282],
   [187.        ,   2.54678885],
   [188.        ,   2.04829839],
   [189.        ,   1.17107491],
   [190.        ,   1.34155466],
   [191.        ,   1.50899447],
   [192.        ,   1.0605842 ],
   [193.        ,   1.30563947],
   [194.        ,   0.96334149],
   [195.        ,   1.77587089],
   [196.        ,   1.21215307],
   [197.        ,   1.16102583],
   [198.        ,   1.40280455],
   [199.        ,   2.06259196],
   [200.        ,   1.89910732],
   [201.        ,   1.2884716 ],
   [202.        ,   1.39991145],
   [203.        ,   1.36551179],
   [204.        ,   1.11746183],
   [205.        ,   2.11818442],
   [206.        ,   1.65175985],
   [207.        ,   2.53128519],
   [208.        ,   0.80258428],
   [209.        ,   2.88233653],
   [210.        ,   1.55351222],
   [211.        ,   2.40060343],
   [212.        ,   3.03453391],
   [213.        ,   1.25745908],
   [214.        ,   1.60361367],
   [215.        ,   1.0169604 ],
   [216.        ,   1.19877746],
   [217.        ,   1.19126611],
   [218.        ,   1.52626066],
   [219.        ,   1.58313905],
   [220.        ,   1.55422272],
   [221.        ,   2.70916065],
   [222.        ,   2.34252505],
   [223.        ,   1.12790749],
   [224.        ,   1.47735158],
   [225.        ,   2.03284713],
   [226.        ,   2.35170545],
   [227.        ,   1.45666335],
   [228.        ,   1.15812069],
   [229.        ,   1.35885238],
   [230.        ,   1.66425174],
   [231.        ,   1.30904447],
   [232.        ,   1.97312502],
   [234.        ,   1.17408153],
   [235.        ,   1.46371938],
   [236.        ,   1.46903067],
   [237.        ,   0.        ],
   [238.        ,   1.33679756],
   [239.        ,   1.53861696],
   [240.        ,   1.18074547],
   [241.        ,   1.59929235],
   [242.        ,   1.61450358],
   [243.        ,   1.56501464],
   [244.        ,   1.52575718],
   [245.        ,   1.32069092],
   [246.        ,   1.34188109],
   [247.        ,   0.77345908],
   [248.        ,   1.13971053],
   [249.        ,   1.00713273],
   [250.        ,   1.03289332],
   [251.        ,   1.12161482],
   [252.        ,   0.93336293],
   [253.        ,   1.66988677]])

Para separar en x e y lo que quiero representar, con las condiciones que he dicho antes:

x = np.argsort(A[:,1])
y = A[:,1][np.argsort(A[:,1])]

Obteniendo:

x = array([ 93, 235,  57, 127,  60, 245, 207, 110, 154,   1, 250, 193, 161,
   134, 247, 214, 248, 191, 135, 174, 151, 152, 203, 249,  38, 222,
   246, 137, 130, 227, 196, 140, 149, 188, 104, 232,  23, 238,   6,
   216,  14, 215, 173, 180, 183, 195, 150,   5, 155,   4, 121, 117,
   212,  11, 200, 156,  56, 192, 230, 103, 128, 243,   2,  65, 136,
    49, 236, 189, 244, 182, 164,  13,  97, 101, 228, 202, 118, 132,
   122,   8, 133, 201, 197, 115,  18, 165,  70,  50, 226, 233, 234,
    66, 223,  68,  24, 125, 190,   0, 242, 217,  34,   3, 120, 237,
    28, 209, 219,  51, 157, 241, 176,  19,  46,  39,  16, 218, 126,
    74, 144, 239, 113, 213,  63,  79, 240, 148, 166, 131, 205,  75,
    29,  90, 177, 229,  21, 251,   9,  25,  44,  10,  12, 153,  15,
   124, 162, 114, 119, 194,  77, 142, 111,  17,  81, 145,  76,  58,
    26,  35, 112,  61, 184, 199,  30, 147,  32,  27, 159, 102,  20,
    62, 143,  42, 171, 231,  71, 178,  69,  64,  80, 224,  67, 187,
   198,  36, 100,  94, 204,  22, 170, 146,  87, 168, 139,  31, 116,
    95,  45, 158, 221, 225,  96, 181, 210,  88,  33, 179,  92, 206,
   186,  53,  37,  40,  59,  52, 138, 220,  72, 123,   7, 105, 172,
   169, 208, 160,  43,  41, 167, 129, 211, 185,  91,  89,  54, 175,
    55,  99,  78, 141,  47,  48, 163,  85,  86, 109, 107,  98, 106,
    82,  84,  73,  83, 108])
y = array([0.        , 0.        , 0.72855339, 0.74709751, 0.76011003,
   0.77345908, 0.80258428, 0.88059179, 0.91306907, 0.92105152,
   0.93336293, 0.96334149, 0.97855339, 0.97887727, 1.00713273,
   1.0169604 , 1.03289332, 1.0605842 , 1.06433057, 1.07079971,
   1.08606747, 1.09289736, 1.11746183, 1.12161482, 1.12708797,
   1.12790749, 1.13971053, 1.15240602, 1.15523068, 1.15812069,
   1.16102583, 1.16443398, 1.16737204, 1.17107491, 1.17387829,
   1.17408153, 1.17939465, 1.18074547, 1.18863495, 1.19126611,
   1.19747605, 1.19877746, 1.20701602, 1.20734984, 1.2112659 ,
   1.21215307, 1.21850403, 1.22161184, 1.23034401, 1.24074641,
   1.24318497, 1.25664942, 1.25745908, 1.26665515, 1.2884716 ,
   1.29756661, 1.30164185, 1.30563947, 1.30904447, 1.31171455,
   1.31198555, 1.32069092, 1.32540165, 1.32850223, 1.3325748 ,
   1.33463897, 1.33679756, 1.34155466, 1.34188109, 1.34408252,
   1.35244229, 1.35315168, 1.3533427 , 1.35803666, 1.35885238,
   1.36551179, 1.37730095, 1.38385385, 1.38390501, 1.39858833,
   1.39873976, 1.39991145, 1.40280455, 1.40802643, 1.4108153 ,
   1.42772753, 1.43295138, 1.44309968, 1.45666335, 1.46371938,
   1.46903067, 1.47564139, 1.47735158, 1.47978079, 1.49588583,
   1.50795386, 1.50899447, 1.51545416, 1.52575718, 1.52626066,
   1.53419281, 1.53666124, 1.53828835, 1.53861696, 1.54877607,
   1.55351222, 1.55422272, 1.55667113, 1.55718317, 1.56501464,
   1.56863437, 1.56890976, 1.56903689, 1.5808429 , 1.5814438 ,
   1.58313905, 1.5833537 , 1.58720266, 1.58981035, 1.59929235,
   1.5993903 , 1.60361367, 1.6036668 , 1.6043282 , 1.61450358,
   1.63352479, 1.63497882, 1.63511695, 1.65175985, 1.65231254,
   1.655113  , 1.65519607, 1.66251272, 1.66425174, 1.66430381,
   1.66988677, 1.67070284, 1.67787019, 1.68425785, 1.68800135,
   1.70171469, 1.7301665 , 1.73701212, 1.73789458, 1.73904682,
   1.74148821, 1.77298028, 1.77587089, 1.78758744, 1.80352927,
   1.8125935 , 1.83155713, 1.84933445, 1.85537995, 1.86028445,
   1.86037849, 1.86811636, 1.87549598, 1.87670035, 1.88334334,
   1.8962926 , 1.89910732, 1.90556009, 1.90558106, 1.90962274,
   1.91399636, 1.92048523, 1.93339371, 1.93348536, 1.93361464,
   1.93624837, 1.9364137 , 1.94023665, 1.97312502, 1.97968055,
   1.99321738, 2.02114407, 2.02444607, 2.02946608, 2.03284713,
   2.03729127, 2.04829839, 2.06259196, 2.08298678, 2.0966741 ,
   2.11647051, 2.11818442, 2.12699475, 2.16064988, 2.17859119,
   2.18723446, 2.1888843 , 2.23183602, 2.24303354, 2.24655088,
   2.27729623, 2.31623894, 2.34227336, 2.34252505, 2.35170545,
   2.38029384, 2.39362749, 2.40060343, 2.42949039, 2.46928062,
   2.519336  , 2.5264844 , 2.53128519, 2.54678885, 2.56148278,
   2.57443832, 2.61126354, 2.65273098, 2.68976388, 2.70389181,
   2.70916065, 2.71543594, 2.74341465, 2.79575549, 2.81503828,
   2.82229232, 2.88079347, 2.88233653, 2.94140732, 2.94897491,
   2.95227462, 2.99290745, 2.99645827, 3.03453391, 3.05109282,
   3.05168227, 3.07777225, 3.28133842, 3.33874121, 3.33924649,
   3.36332311, 3.60057309, 3.67429258, 3.8287992 , 4.05121551,
   4.27600858, 4.53304139, 4.67289848, 4.67747407, 5.20974647,
   5.76654075, 5.82386601, 6.02552801, 6.42081655, 6.84955878,
   7.05783584, 9.3757877 ])

La forma que se me ha ocurrido para representarlo en con bar de matplotlib. Sin embargo, al hacer

import matplotlib.pyplot as pet
plt.bar(x,y)

Obtengo el siguiente resultado introducir la descripción de la imagen aquí

Yo quiero que en este gráfico se pueda visualizar cómo aumentan los valores del eje y con respecto a x.

0

1 respuesta 1

1

Buen día,

Puedes utilizar una combinación de los métodos range() y len() para ordenar el eje y

Utilizando como ejemplo el arreglo A de tu pregunta

x = np.argsort(A[:,1])
y = A[:,1][np.argsort(A[:,1])]
plt.bar(range(len(x)),sorted(y))

Esto devuelve

Ejemplo

Edición: Si quieres ver las etiquetas de cada valor en x lo que podrías hacer es convertir cada valor de x a string pero al ser tantos datos se vuelve ilegible

Intenté con varios tamaños de gráfica y se sigue viendo casi igual

x = np.argsort(A[:,1])
x = [str(x) for x in x]
y = A[:,1][np.argsort(A[:,1])]
plt.bar(x,sorted(y))
plt.rcParams["figure.figsize"] = (100,5)
2
  • muchas gracias por tu respuesta. Esto me soluciona el problema principal: la representación. Sin embargo, no hay forma de hacer que el eje x muestre exactamente qué etiqueta le corresponde ese valor?
    – Alberto
    Commented el 27 abr. 2022 a las 12:34
  • Si es posible, pero al ser tantos datos no se pueden leer, edite la respuesta agregando la forma para hacerlo. Básicamente, hay que convertir los valores de x a string Commented el 27 abr. 2022 a las 13:00

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.