Cuando tratamos con datos y queremos representarlos en gráficos, en ocasiones necesitamos generar esos gráficos en un formato que permita escalar a cualquier resolución y sin perder calidad. Si además necesitamos cierta interactividad, poder crear representaciones gráficas en el formato SVG es una muy buena opción.
Los gráficos vectoriales están compuestos por texto en XML, lo que permite que cualquier navegador que respete los estándares W3C los represente sin problema.
Pandas y Pygal para gráficos SVG en Python
Para la demostración, vamos a utilizar un dataset que nos permita mostrar distintos tipos de representaciones gráficas del mismo. En este caso, utilizaré un dataset de Pokémon, donde podremos ver las características de 800 pokémon de 6 generaciones distintas.
Utilizaremos Pandas para crear un dataframe con esta estructura, con el que trabajaremos en los siguientes pasos.
Dataframe de Pandas representando la estructura de datos que vamos a utilizar.
Para generar los gráficos en SVG utilizaremos la librería de Python Pygal .
Empecemos con algo sencillo, vamos a realizar una gráfica simple para ver cuántos pokémon hay de cada generación.
Para ello, haremos un agrupado por generación y sacaremos el número de pokémon de cada una de ellas con este simple código.
pie_chart = pygal.Pie()
pie_chart.title = 'Pokemon by Generation'
for id, row in data.groupby(["Generation"]).size().reset_index().iterrows():
pie_chart.add("Generation %s" % row["Generation"], row[0])
pie_chart.render_to_file('pie_chart_by_generation.svg')
Pokemon by Generation 166 391.4928844728903 171.48743594049068 106 437.4147128241523 284.8629929327153 160 362.8301650050641 378.7126447794734 121 238.79006958495955 356.81312547254106 165 203.08996784921055 233.93256930870518 82 280.5752923931324 153.02568661574566 Pokemon by Generation Generation 1 Generation 2 Generation 3 Generation 4 Generation 5 Generation 6
Bien, ahora podemos profundizar un poco más y realizar una representación que represente un grupo mayor de datos. En ella, mostraremos la distribución de los principales atributos agrupados por tipo de Pokémon. Para ello, generaremos una gráfica del tipo Box Plot que nos permitirá ver de forma sencilla todos estos datos agrupados con el siguiente código:
for this_attribute in ["HP", "Attack", "Defense"]:
box_plot = pygal.Box(box_mode="tukey")
box_plot.title = 'Pokemon Attribute: %s' % this_attribute
for this_type in data["Type 1"].unique():
box_plot.add(this_type, data[data["Type 1"]==this_type]["HP"].values)
box_plot.render_to_file('box_plot_attribute_%s.svg' % this_attribute)
Lo que nos permite ver las gráficas de los tres atributos por separado.
Pokemon Attribute: HP 0 0 20 20 40 40 60 60 80 80 100 100 120 120 140 140 160 160 180 180 200 200 220 220 240 240 Min: 30
Lower Whisker: 30
Q1: 50
Q2: 65.5
Q3: 75
Upper Whisker: 100
Max: 123 20.342307692307692 320.7541478129714 Min: 38
Lower Whisker: 38
Q1: 58
Q2: 70
Q3: 80
Upper Whisker: 110
Max: 115 43.996153846153845 309.1349924585219 Min: 20
Lower Whisker: 20
Q1: 51.5
Q2: 70
Q3: 90.5
Upper Whisker: 130
Max: 170 67.65 307.17119155354453 Min: 1
Lower Whisker: 20
Q1: 45
Q2: 60
Q3: 70
Upper Whisker: 86
Max: 86 91.30384615384614 333.68250377073906 Min: 30
Lower Whisker: 30
Q1: 55
Q2: 70
Q3: 90
Upper Whisker: 140
Max: 255 114.9576923076923 299.6432880844646 Min: 35
Lower Whisker: 35
Q1: 50
Q2: 67.5
Q3: 80.5
Upper Whisker: 105
Max: 105 138.6115384615385 315.02639517345403 Min: 20
Lower Whisker: 20
Q1: 50
Q2: 60
Q3: 70
Upper Whisker: 90
Max: 90 162.26538461538462 330.73680241327304 Min: 10
Lower Whisker: 10
Q1: 54.5
Q2: 75
Q3: 92.5
Upper Whisker: 115
Max: 115 185.91923076923078 312.08069381598796 Min: 35
Lower Whisker: 35
Q1: 54.75
Q2: 78
Q3: 91.25
Upper Whisker: 126
Max: 126 209.57307692307694 299.6432880844646 Min: 30
Lower Whisker: 30
Q1: 50
Q2: 70
Q3: 79.5
Upper Whisker: 120
Max: 144 233.2269230769231 311.26244343891403 Min: 20
Lower Whisker: 20
Q1: 50
Q2: 68
Q3: 80
Upper Whisker: 120
Max: 190 256.88076923076926 315.02639517345403 Min: 30
Lower Whisker: 30
Q1: 50
Q2: 68.5
Q3: 80
Upper Whisker: 123
Max: 123 280.53461538461534 310.6078431372549 Min: 20
Lower Whisker: 20
Q1: 45
Q2: 59.5
Q3: 64.5
Upper Whisker: 90
Max: 150 304.18846153846147 334.33710407239823 Min: 36
Lower Whisker: 36
Q1: 53
Q2: 70
Q3: 90
Upper Whisker: 110
Max: 110 327.84230769230766 308.1530920060332 Min: 41
Lower Whisker: 41
Q1: 67
Q2: 80
Q3: 102.5
Upper Whisker: 125
Max: 125 351.4961538461538 289.66063348416293 Min: 35
Lower Whisker: 35
Q1: 50.5
Q2: 65
Q3: 74.25
Upper Whisker: 100
Max: 126 375.15 319.36312217194575 Min: 40
Lower Whisker: 40
Q1: 57
Q2: 60
Q3: 75
Upper Whisker: 100
Max: 100 398.8038461538461 316.9901960784314 Min: 40
Lower Whisker: 40
Q1: 59.5
Q2: 79
Q3: 82
Upper Whisker: 85
Max: 85 422.45769230769224 312.5716440422323 Pokemon Attribute: HP Grass Fire Water Bug Normal Poison Electric Ground Fairy Fighting Psychic Rock Ghost Ice Dragon Dark Steel Flying
Pokemon Attribute: Attack 0 0 20 20 40 40 60 60 80 80 100 100 120 120 140 140 160 160 180 180 Min: 27
Lower Whisker: 27
Q1: 55
Q2: 70
Q3: 94
Upper Whisker: 132
Max: 132 20.342307692307692 259.6093117408907 Min: 30
Lower Whisker: 30
Q1: 61.5
Q2: 84.5
Q3: 102
Upper Whisker: 160
Max: 160 43.996153846153845 233.253036437247 Min: 10
Lower Whisker: 10
Q1: 53
Q2: 72
Q3: 92
Upper Whisker: 150
Max: 155 67.65 260.0485829959514 Min: 10
Lower Whisker: 10
Q1: 45
Q2: 65
Q3: 90
Upper Whisker: 155
Max: 185 91.30384615384614 265.31983805668017 Min: 5
Lower Whisker: 10
Q1: 55
Q2: 70.5
Q3: 85
Upper Whisker: 130
Max: 160 114.9576923076923 271.68927125506076 Min: 43
Lower Whisker: 43
Q1: 60
Q2: 74
Q3: 91
Upper Whisker: 106
Max: 106 138.6115384615385 261.3663967611336 Min: 30
Lower Whisker: 30
Q1: 52.5
Q2: 65
Q3: 85
Upper Whisker: 123
Max: 123 162.26538461538462 269.49291497975713 Min: 40
Lower Whisker: 40
Q1: 72
Q2: 85
Q3: 122
Upper Whisker: 180
Max: 180 185.91923076923078 206.45748987854256 Min: 20
Lower Whisker: 20
Q1: 43.75
Q2: 52
Q3: 74
Upper Whisker: 80
Max: 131 209.57307692307694 307.16042510121457 Min: 35
Lower Whisker: 35
Q1: 80
Q2: 100
Q3: 120
Upper Whisker: 145
Max: 145 233.2269230769231 214.8036437246964 Min: 20
Lower Whisker: 20
Q1: 45
Q2: 57
Q3: 96.25
Upper Whisker: 165
Max: 190 256.88076923076926 257.3031376518219 Min: 40
Lower Whisker: 40
Q1: 59.5
Q2: 95
Q3: 120.5
Upper Whisker: 165
Max: 165 280.53461538461534 214.8036437246964 Min: 30
Lower Whisker: 30
Q1: 52.5
Q2: 66
Q3: 93.5
Upper Whisker: 120
Max: 165 304.18846153846147 266.6376518218624 Min: 30
Lower Whisker: 30
Q1: 50
Q2: 67
Q3: 90
Upper Whisker: 130
Max: 130 327.84230769230766 264.4412955465587 Min: 50
Lower Whisker: 50
Q1: 85.5
Q2: 113.5
Q3: 134.5
Upper Whisker: 180
Max: 180 351.4961538461538 178.12449392712554 Min: 50
Lower Whisker: 50
Q1: 65
Q2: 88
Q3: 102.5
Upper Whisker: 150
Max: 150 375.15 225.56578947368425 Min: 24
Lower Whisker: 50
Q1: 76.25
Q2: 89
Q3: 110
Upper Whisker: 150
Max: 150 398.8038461538461 216.89018218623488 Min: 30
Lower Whisker: 30
Q1: 50
Q2: 85
Q3: 107.5
Upper Whisker: 115
Max: 115 422.45769230769224 255.4362348178138 Pokemon Attribute: Attack Grass Fire Water Bug Normal Poison Electric Ground Fairy Fighting Psychic Rock Ghost Ice Dragon Dark Steel Flying
Pokemon Attribute: Defense 0 0 20 20 40 40 60 60 80 80 100 100 120 120 140 140 160 160 180 180 200 200 220 220 Min: 30
Lower Whisker: 30
Q1: 50
Q2: 66
Q3: 85
Upper Whisker: 131
Max: 131 20.342307692307692 294.2926421404682 Min: 37
Lower Whisker: 37
Q1: 50
Q2: 64
Q3: 78
Upper Whisker: 120
Max: 140 43.996153846153845 299.01003344481603 Min: 20
Lower Whisker: 20
Q1: 54
Q2: 70
Q3: 89
Upper Whisker: 133
Max: 180 67.65 292.84113712374585 Min: 30
Lower Whisker: 30
Q1: 48.75
Q2: 60
Q3: 90
Upper Whisker: 140
Max: 230 91.30384615384614 291.8432274247492 Min: 5
Lower Whisker: 5
Q1: 43
Q2: 60
Q3: 75
Upper Whisker: 120
Max: 126 114.9576923076923 315.7023411371237 Min: 35
Lower Whisker: 35
Q1: 52.5
Q2: 67
Q3: 82.5
Upper Whisker: 120
Max: 120 138.6115384615385 296.10702341137124 Min: 15
Lower Whisker: 15
Q1: 49.5
Q2: 65
Q3: 80
Upper Whisker: 115
Max: 115 162.26538461538462 307.9005016722408 Min: 25
Lower Whisker: 25
Q1: 52.5
Q2: 84.5
Q3: 110
Upper Whisker: 160
Max: 160 185.91923076923078 268.8913043478261 Min: 28
Lower Whisker: 28
Q1: 49.5
Q2: 66
Q3: 77.5
Upper Whisker: 95
Max: 95 209.57307692307694 310.9849498327759 Min: 30
Lower Whisker: 30
Q1: 53.5
Q2: 70
Q3: 79.75
Upper Whisker: 95
Max: 95 233.2269230769231 306.53971571906357 Min: 15
Lower Whisker: 15
Q1: 48
Q2: 65
Q3: 81.25
Upper Whisker: 130
Max: 160 256.88076923076926 302.5480769230769 Min: 40
Lower Whisker: 40
Q1: 71
Q2: 100
Q3: 122
Upper Whisker: 168
Max: 200 280.53461538461534 243.85284280936455 Min: 30
Lower Whisker: 30
Q1: 60
Q2: 72.5
Q3: 114
Upper Whisker: 145
Max: 145 304.18846153846147 272.7015050167224 Min: 15
Lower Whisker: 15
Q1: 47.5
Q2: 75
Q3: 85
Upper Whisker: 110
Max: 184 327.84230769230766 304.99749163879596 Min: 35
Lower Whisker: 35
Q1: 67.5
Q2: 90
Q3: 100
Upper Whisker: 130
Max: 130 351.4961538461538 272.3386287625418 Min: 30
Lower Whisker: 30
Q1: 50.5
Q2: 70
Q3: 90
Upper Whisker: 125
Max: 125 375.15 293.0225752508361 Min: 50
Lower Whisker: 50
Q1: 96.25
Q2: 120
Q3: 150
Upper Whisker: 230
Max: 230 398.8038461538461 191.14506688963212 Min: 35
Lower Whisker: 35
Q1: 52.5
Q2: 75
Q3: 80
Upper Whisker: 80
Max: 80 422.45769230769224 308.626254180602 Pokemon Attribute: Defense Grass Fire Water Bug Normal Poison Electric Ground Fairy Fighting Psychic Rock Ghost Ice Dragon Dark Steel Flying
Si lo que necesitamos es poder comparar los atributos principales entre ciertos Pokémon, crearemos un gráfico del tipo radar que nos permita ver estos datos de una forma sencilla, de la siguiente manera:
x_labels = ['HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed']
radar_chart = pygal.Radar(width=600, height=500)
radar_chart.title = 'Pokemon Attributes'
radar_chart.x_labels = x_labels
radar_chart.add('Bulbasaur', data[data["Name"]=="Bulbasaur"][x_labels].values[0])
radar_chart.render_to_file('radar_plot_attribute_Bulbasaur.svg')
display(SVG('radar_plot_attribute_Bulbasaur.svg'))
Pokemon Attributes 60 60 50 50 40 40 30 30 20 20 10 10 0 0 HP 1.570796327 Attack 2.617993878 Defense 3.665191429 Sp. Atk 4.71238898 Sp. Def 5.759586532 Speed 6.806784083 Pokemon Attributes 45 220.2 74.30769230769232 HP 49 87.091155245679 134.75641025641022 Attack 49 87.09115524567893 279.2435897435897 Defense 65 220.19999999999996 398.6666666666667 Sp. Atk 65 396.77295732716044 302.8333333333334 Sp. Def 45 342.4428166111112 140.65384615384625 Speed Bulbasaur
Este tipo de gráficos nos permite hacer comparaciones de datos de una forma sencilla y comprensible, mostrando un grupo de datos comparativos amplios a la vez. Por ejemplo, podemos comparar los atributos de estos cuatro Pokémon legendarios para ver sus diferencias de un vistazo.
Pokemon Attributes 140 140 120 120 100 100 80 80 60 60 40 40 20 20 0 0 HP 1.570796327 Attack 2.617993878 Defense 3.665191429 Sp. Atk 4.71238898 Sp. Def 5.759586532 Speed 6.806784083 Pokemon Attributes 90 221.4 94.98701298701297 HP 100 106.11739754817539 144.77056277056272 Attack 90 117.64565779335781 263.0064935064935 Defense 125 221.39999999999995 362.5735930735931 Sp. Atk 85 319.3902120840509 259.89502164502164 Sp. Def 90 325.1543422066422 150.99350649350657 Speed 90 221.4 94.98701298701297 HP 85 123.40978791594908 154.1049783549783 Attack 100 106.11739754817535 269.22943722943717 Defense 95 221.39999999999995 325.23593073593076 Sp. Atk 125 365.5032530647807 284.7867965367966 Sp. Def 85 319.390212084051 154.10497835497847 Speed 106 221.4 75.07359307359309 HP 110 94.58913730299294 138.54761904761898 Attack 90 117.64565779335781 263.0064935064935 Defense 154 221.39999999999995 398.6666666666667 Sp. Atk 90 325.1543422066422 263.0064935064936 Sp. Def 130 371.2673831873721 126.10173160173173 Speed 90 221.4 94.98701298701297 HP 90 117.64565779335786 150.99350649350646 Attack 85 123.40978791594904 259.89502164502164 Defense 125 221.39999999999995 362.5735930735931 Sp. Atk 90 325.1543422066422 263.0064935064936 Sp. Def 100 336.6826024518247 144.77056277056283 Speed Moltres Articuno Mewtwo Zapdos
Si lo que queremos es ver la media de cada uno de los atributos de los Pokémon, pero esta vez agrupados por tipo, de forma que nos facilite, por ejemplo, encontrar qué tipo tiene, de media, el ataque o la defensa más altos, podemos mostrarlo de la siguiente forma:
x_labels = ['HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed']
dot_chart = pygal.Dot(x_label_rotation=30, width=600, height=600)
dot_chart.title = 'Pokemon attributes by Type'
dot_chart.x_labels = x_labels
for id, row in data.groupby(["Type 1"])[x_labels].mean().reset_index().iterrows():
dot_chart.add(row["Type 1"], row[x_labels])
dot_chart.render_to_file('dot_chart_attribute_by_type.svg')
Pokemon attributes by Type Bug 17.5 Dark 16.5 Dragon 15.5 Electric 14.5 Fairy 13.5 Fighting 12.5 Fire 11.5 Flying 10.5 Ghost 9.5 Grass 8.5 Ground 7.5 Ice 6.5 Normal 5.5 Poison 4.5 Psychic 3.5 Rock 2.5 Steel 1.5 Water 0.5 HP Attack Defense Sp. Atk Sp. Def Speed 56.88405797 39.38857347523289 23.107905982905947 HP 70.97101449 102.91853069335045 23.107905982905947 Attack 70.72463768 166.44848791146802 23.107905982905947 Defense 53.86956522 229.9784451295856 23.107905982905947 Sp. Atk 64.79710145 293.5084023477031 23.107905982905947 Sp. Def 61.68115942 357.0383595658207 23.107905982905947 Speed 66.80645161 39.38857347523289 49.97756410256409 HP 88.38709677 102.91853069335045 49.97756410256409 Attack 70.22580645 166.44848791146802 49.97756410256409 Defense 74.64516129 229.9784451295856 49.97756410256409 Sp. Atk 69.51612903 293.5084023477031 49.97756410256409 Sp. Def 76.16129032 357.0383595658207 49.97756410256409 Speed 83.3125 39.38857347523289 76.84722222222223 HP 112.125 102.91853069335045 76.84722222222223 Attack 86.375 166.44848791146802 76.84722222222223 Defense 96.84375 229.9784451295856 76.84722222222223 Sp. Atk 88.84375 293.5084023477031 76.84722222222223 Sp. Def 83.03125 357.0383595658207 76.84722222222223 Speed 59.79545455 39.38857347523289 103.71688034188031 HP 69.09090909 102.91853069335045 103.71688034188031 Attack 66.29545455 166.44848791146802 103.71688034188031 Defense 90.02272727 229.9784451295856 103.71688034188031 Sp. Atk 73.70454545 293.5084023477031 103.71688034188031 Sp. Def 84.5 357.0383595658207 103.71688034188031 Speed 74.11764706 39.38857347523289 130.58653846153845 HP 61.52941176 102.91853069335045 130.58653846153845 Attack 65.70588235 166.44848791146802 130.58653846153845 Defense 78.52941176 229.9784451295856 130.58653846153845 Sp. Atk 84.70588235 293.5084023477031 130.58653846153845 Sp. Def 48.58823529 357.0383595658207 130.58653846153845 Speed 69.85185185 39.38857347523289 157.45619658119654 HP 96.77777778 102.91853069335045 157.45619658119654 Attack 65.92592593 166.44848791146802 157.45619658119654 Defense 53.11111111 229.9784451295856 157.45619658119654 Sp. Atk 64.7037037 293.5084023477031 157.45619658119654 Sp. Def 66.07407407 357.0383595658207 157.45619658119654 Speed 69.90384615 39.38857347523289 184.32585470085468 HP 84.76923077 102.91853069335045 184.32585470085468 Attack 67.76923077 166.44848791146802 184.32585470085468 Defense 88.98076923 229.9784451295856 184.32585470085468 Sp. Atk 72.21153846 293.5084023477031 184.32585470085468 Sp. Def 74.44230769 357.0383595658207 184.32585470085468 Speed 70.75 39.38857347523289 211.19551282051282 HP 78.75 102.91853069335045 211.19551282051282 Attack 66.25 166.44848791146802 211.19551282051282 Defense 94.25 229.9784451295856 211.19551282051282 Sp. Atk 72.5 293.5084023477031 211.19551282051282 Sp. Def 102.5 357.0383595658207 211.19551282051282 Speed 64.4375 39.38857347523289 238.0651709401709 HP 73.78125 102.91853069335045 238.0651709401709 Attack 81.1875 166.44848791146802 238.0651709401709 Defense 79.34375 229.9784451295856 238.0651709401709 Sp. Atk 76.46875 293.5084023477031 238.0651709401709 Sp. Def 64.34375 357.0383595658207 238.0651709401709 Speed 67.27142857 39.38857347523289 264.93482905982904 HP 73.21428571 102.91853069335045 264.93482905982904 Attack 70.8 166.44848791146802 264.93482905982904 Defense 77.5 229.9784451295856 264.93482905982904 Sp. Atk 70.42857143 293.5084023477031 264.93482905982904 Sp. Def 61.92857143 357.0383595658207 264.93482905982904 Speed 73.78125 39.38857347523289 291.8044871794872 HP 95.75 102.91853069335045 291.8044871794872 Attack 84.84375 166.44848791146802 291.8044871794872 Defense 56.46875 229.9784451295856 291.8044871794872 Sp. Atk 62.75 293.5084023477031 291.8044871794872 Sp. Def 63.90625 357.0383595658207 291.8044871794872 Speed 72 39.38857347523289 318.67414529914527 HP 72.75 102.91853069335045 318.67414529914527 Attack 71.41666667 166.44848791146802 318.67414529914527 Defense 77.54166667 229.9784451295856 318.67414529914527 Sp. Atk 76.29166667 293.5084023477031 318.67414529914527 Sp. Def 63.45833333 357.0383595658207 318.67414529914527 Speed 77.2755102 39.38857347523289 345.54380341880335 HP 73.46938776 102.91853069335045 345.54380341880335 Attack 59.84693878 166.44848791146802 345.54380341880335 Defense 55.81632653 229.9784451295856 345.54380341880335 Sp. Atk 63.7244898 293.5084023477031 345.54380341880335 Sp. Def 71.55102041 357.0383595658207 345.54380341880335 Speed 67.25 39.38857347523289 372.41346153846155 HP 74.67857143 102.91853069335045 372.41346153846155 Attack 68.82142857 166.44848791146802 372.41346153846155 Defense 60.42857143 229.9784451295856 372.41346153846155 Sp. Atk 64.39285714 293.5084023477031 372.41346153846155 Sp. Def 63.57142857 357.0383595658207 372.41346153846155 Speed 70.63157895 39.38857347523289 399.28311965811963 HP 71.45614035 102.91853069335045 399.28311965811963 Attack 67.68421053 166.44848791146802 399.28311965811963 Defense 98.40350877 229.9784451295856 399.28311965811963 Sp. Atk 86.28070175 293.5084023477031 399.28311965811963 Sp. Def 81.49122807 357.0383595658207 399.28311965811963 Speed 65.36363636 39.38857347523289 426.15277777777777 HP 92.86363636 102.91853069335045 426.15277777777777 Attack 100.7954545 166.44848791146802 426.15277777777777 Defense 63.34090909 229.9784451295856 426.15277777777777 Sp. Atk 75.47727273 293.5084023477031 426.15277777777777 Sp. Def 55.90909091 357.0383595658207 426.15277777777777 Speed 65.22222222 39.38857347523289 453.0224358974359 HP 92.7037037 102.91853069335045 453.0224358974359 Attack 126.3703704 166.44848791146802 453.0224358974359 Defense 67.51851852 229.9784451295856 453.0224358974359 Sp. Atk 80.62962963 293.5084023477031 453.0224358974359 Sp. Def 55.25925926 357.0383595658207 453.0224358974359 Speed 72.0625 39.38857347523289 479.892094017094 HP 74.15178571 102.91853069335045 479.892094017094 Attack 72.94642857 166.44848791146802 479.892094017094 Defense 74.8125 229.9784451295856 479.892094017094 Sp. Atk 70.51785714 293.5084023477031 479.892094017094 Sp. Def 65.96428571 357.0383595658207 479.892094017094 Speed Pokemon attributes by Type Bug Dark Dragon Electric Fairy Fighting Fire Flying Ghost Grass Ground Ice Normal Poison Psychic Rock Steel Water
Pero si lo que queremos ver es cómo son los atributos de un Pokémon concreto respecto al resto, podemos representarlo de la siguiente manera, lo que nos permitiría incluso comparar atributos entre diferentes Pokémon de forma sencilla.
pokemon_names = ["Pikachu", "Dragonite"]
x_labels = ['HP', 'Attack', 'Defense', 'Sp. Atk', 'Sp. Def', 'Speed']
for pokemon_name in pokemon_names:
this_pokemon = data[data["Name"]==pokemon_name]
gauge = pygal.SolidGauge(inner_radius=0.70)
gauge.title = '%s stats vs others Pokemon' % pokemon_name
for this_label in x_labels:
gauge.add(this_label, [{'value': this_pokemon[this_label].values[0], 'max_value': data[this_label].max()}])
gauge.render_to_file('gauge_%s_vs_all.svg' % pokemon_name)
Pikachu stats vs others Pokemon 35 198.25683008161667 27.147141968167723 55 563.1286315822535 47.18132745652396 40 205.17587323816252 208.8268548414523 50 558.6984820770726 220.04015662402026 50 212.76762268943932 392.1856967723896 90 577.485 445.0 Pikachu stats vs others Pokemon 255 35 190 55 230 40 194 50 230 50 180 90 HP Attack Defense Sp. Atk Sp. Def Speed
Dragonite stats vs others Pokemon 91 231.11644562613893 59.40505582406426 134 563.812714768591 129.92460991276477 95 235.36022350907496 248.630886835774 100 577.404670544551 270.3063588926622 100 236.46094010985098 431.1476973513114 80 576.4506358638362 433.17716382354706 Dragonite stats vs others Pokemon 255 91 190 134 230 95 194 100 230 100 180 80 HP Attack Defense Sp. Atk Sp. Def Speed
Conclusión
De esta forma, con representaciones de nuestros datos en gráficos SVG, podemos representar esos datos a cualquier tamaño y escala, sin preocuparnos por tener una resolución específica y obteniendo un resultado óptimo. Además, herramientas como Pygal , nos permiten integrar este tipo de gráficos no sólo en Dashboards o presentaciones puntuales, también en todo tipo de intranets , webapps o páginas webs donde podemos adaptarlas sin problemas al entorno visual y respetando el diseño original.
¡Esto es todo! Si este post te ha parecido interesante, te animamos a visitar la categoría Data Analytics para ver todos los posts relacionados y a compartirlo en redes. ¡Hasta pronto!