Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Spyder Editor
- This is a temporary script file.
- """
- import matplotlib.pyplot as plt
- import numpy as np
- import re
- def decompose(in_str): # décompose les params de l'entrée en une liste
- str2 = in_str.strip('{}') # vise les {}
- list_tmp = str2.split('|')
- liste = [i.strip() for i in list_tmp]
- return liste[0], liste[1:] # le premier à part, c'est le titre du modèle
- def list2dic(list_arg):
- output = {}
- for argi in list_arg:
- l = argi.find('=')
- output[argi[:l].strip()] = argi[l+1:].strip() # enlever les espace
- return output
- def nombreseries(parametres):
- nseries = 0
- while ('y'+str(nseries+1)) in parametres.keys():
- nseries+=1
- return nseries
- def barres(ax, parametres):
- labels = parametres['x'].split(',')
- x = np.arange(len(labels))
- nseries = nombreseries(parametres)
- multiplier=0
- width = 1/(nseries+1/2)
- # if 'colors' in parametres:
- couleurs = parametres['colors'].split(',')
- print(couleurs)
- for iserie in range(nseries):
- offset = width * multiplier
- ytxt = parametres['y'+str(1+iserie)]
- y = [float(i) for i in ytxt.split(',')]
- labelserie = parametres['y'+str(iserie+1)+'Title']
- rects = ax.bar(x+offset, y, width, label=labelserie, color=couleurs[iserie])
- ax.bar_label(rects, padding=3)
- multiplier += 1
- ax.set_xticks(x + width/2, labels, fontsize=12)
- ax.legend(loc='upper right')
- def placetitres(ax, parametres):
- if 'xAxisTitle' in parametres:
- ax.set_xlabel(parametres['xAxisTitle'], fontsize=14)
- if 'yAxisTitle' in parametres:
- ax.set_ylabel(parametres['yAxisTitle'], fontsize=14)
- def settaille(parametres):
- if 'width' in parametres:
- w = int(parametres['width'])
- else :
- w = 400
- if 'height' in parametres:
- h = int(parametres['height'])
- else:
- h = 300
- return h,w
- 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}}"
- titremodele, liste_arg = decompose(in_str)
- parametres = list2dic(liste_arg)
- h, w = settaille(parametres)
- dpi = 6
- f, ax = plt.subplots(figsize=(10, 5))
- barres(ax, parametres)
- placetitres(ax, parametres)
- f.savefig("torchagegaz_exUSS.svg")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement