Advertisement
Guest User

Untitled

a guest
Jun 12th, 2023
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Spyder Editor
  4.  
  5. This is a temporary script file.
  6. """
  7. import matplotlib.pyplot as plt
  8. import numpy as np
  9. import re
  10.  
  11. def decompose(in_str): # décompose les params de l'entrée en une liste
  12. str2 = in_str.strip('{}') # vise les {}
  13. list_tmp = str2.split('|')
  14. liste = [i.strip() for i in list_tmp]
  15. return liste[0], liste[1:] # le premier à part, c'est le titre du modèle
  16.  
  17. def list2dic(list_arg):
  18. output = {}
  19. for argi in list_arg:
  20. l = argi.find('=')
  21. output[argi[:l].strip()] = argi[l+1:].strip() # enlever les espace
  22. return output
  23.  
  24. def nombreseries(parametres):
  25. nseries = 0
  26. while ('y'+str(nseries+1)) in parametres.keys():
  27. nseries+=1
  28. return nseries
  29.  
  30.  
  31. def barres(ax, parametres):
  32. labels = parametres['x'].split(',')
  33. x = np.arange(len(labels))
  34. nseries = nombreseries(parametres)
  35. multiplier=0
  36. width = 1/(nseries+1/2)
  37. # if 'colors' in parametres:
  38. couleurs = parametres['colors'].split(',')
  39. print(couleurs)
  40.  
  41.  
  42. for iserie in range(nseries):
  43. offset = width * multiplier
  44. ytxt = parametres['y'+str(1+iserie)]
  45. y = [float(i) for i in ytxt.split(',')]
  46. labelserie = parametres['y'+str(iserie+1)+'Title']
  47. rects = ax.bar(x+offset, y, width, label=labelserie, color=couleurs[iserie])
  48. ax.bar_label(rects, padding=3)
  49. multiplier += 1
  50. ax.set_xticks(x + width/2, labels, fontsize=12)
  51. ax.legend(loc='upper right')
  52.  
  53.  
  54.  
  55. def placetitres(ax, parametres):
  56. if 'xAxisTitle' in parametres:
  57. ax.set_xlabel(parametres['xAxisTitle'], fontsize=14)
  58. if 'yAxisTitle' in parametres:
  59. ax.set_ylabel(parametres['yAxisTitle'], fontsize=14)
  60.  
  61. def settaille(parametres):
  62. if 'width' in parametres:
  63. w = int(parametres['width'])
  64. else :
  65. w = 400
  66. if 'height' in parametres:
  67. h = int(parametres['height'])
  68. else:
  69. h = 300
  70. return h,w
  71.  
  72.  
  73.  
  74.  
  75.  
  76. in_str = "{{Graph:Chart |width=325 |height=200 |xAxisTitle=Torchage par pays (ex-URSS) |yAxisTitle=Milliards de mètres cubes de gaz |y1Title=2012 |y2Title=2021 |legend= |type=rect |x=Russie, Kazakhstan, Turkménistan, Ouzbékistan |y1=24.9, 3.96, 2.45, 1.83|y2=25.4, 1.5, 1.17, 0.47 |colors=#FF0000aa,#0000ffaa}}"
  77.  
  78. titremodele, liste_arg = decompose(in_str)
  79. parametres = list2dic(liste_arg)
  80.  
  81. h, w = settaille(parametres)
  82. dpi = 6
  83.  
  84. f, ax = plt.subplots(figsize=(10, 5))
  85. barres(ax, parametres)
  86. placetitres(ax, parametres)
  87.  
  88. f.savefig("torchagegaz_exUSS.svg")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement