[822] | 1 | #!/dsk/l1/misc/meunier/tools/bin/python3 |
---|
| 2 | # -*- coding: utf-8 -*- |
---|
| 3 | |
---|
| 4 | import matplotlib |
---|
| 5 | matplotlib.use('PDF') |
---|
| 6 | import matplotlib.pyplot as plt |
---|
| 7 | from matplotlib.ticker import MultipleLocator |
---|
| 8 | |
---|
| 9 | class Stack: |
---|
| 10 | def __init__(self, couleurs, title, label_X, label_Y): |
---|
| 11 | self.xlabel = label_X |
---|
| 12 | self.ylabel = label_Y |
---|
| 13 | self.title = title |
---|
| 14 | self.couleurs = couleurs |
---|
| 15 | self.actual_colors = [] |
---|
| 16 | self.color_index = 0 |
---|
| 17 | self.dataset = [] |
---|
| 18 | self.X = [] |
---|
| 19 | #self.clear() |
---|
| 20 | self.init_picture() |
---|
| 21 | |
---|
| 22 | def add_y(self, data): |
---|
| 23 | self.dataset.append(data) |
---|
| 24 | self.actual_colors.append(self.couleurs[self.get_color()]) |
---|
| 25 | |
---|
| 26 | def add_x(self, data): |
---|
| 27 | self.X = data |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | def init_picture(self): |
---|
| 31 | plt.clf() # Clear previous figure |
---|
| 32 | textcolor = '#000000' |
---|
| 33 | plt.rcParams['font.family'] = 'serif' |
---|
| 34 | plt.rcParams['font.size'] = 10 |
---|
| 35 | |
---|
| 36 | plt.rcParams['mathtext.fontset'] = 'cm' |
---|
| 37 | plt.rcParams['mathtext.fallback_to_cm'] = True |
---|
| 38 | plt.rcParams['mathtext.default'] = 'regular' |
---|
| 39 | plt.rcParams['mathtext.default'] = 'sf' |
---|
| 40 | |
---|
| 41 | plt.rcParams['lines.linewidth'] = '1' |
---|
| 42 | plt.rcParams['lines.antialiased'] = True |
---|
| 43 | |
---|
| 44 | plt.rcParams['text.latex.unicode'] = True |
---|
| 45 | plt.rcParams['text.color'] = textcolor |
---|
| 46 | plt.rcParams['text.hinting'] = 'auto' |
---|
| 47 | plt.rcParams['axes.labelcolor'] = textcolor |
---|
| 48 | plt.rcParams['axes.edgecolor'] = textcolor # couleur du cadre |
---|
| 49 | plt.rcParams['axes.facecolor'] = '#FFFFFF' # couleur du fond |
---|
| 50 | plt.rcParams['axes.linewidth'] = '0.1' |
---|
| 51 | #plt.rcParams['ytick.major.pad'] = 8 |
---|
| 52 | |
---|
| 53 | plt.rcParams['grid.color'] = textcolor |
---|
| 54 | plt.rcParams['grid.alpha'] = '0.1' |
---|
| 55 | plt.rcParams['grid.linestyle'] = ':' |
---|
| 56 | plt.rcParams['grid.linewidth'] = '0.1' |
---|
| 57 | plt.rcParams['xtick.direction'] = 'in' |
---|
| 58 | plt.rcParams['ytick.direction'] = 'in' |
---|
| 59 | plt.rcParams['xtick.color'] = textcolor |
---|
| 60 | plt.rcParams['ytick.color'] = textcolor |
---|
| 61 | plt.rcParams['xtick.major.size'] = 6 |
---|
| 62 | plt.rcParams['ytick.major.size'] = 6 |
---|
| 63 | plt.rcParams['xtick.minor.size'] = 3 |
---|
| 64 | plt.rcParams['ytick.minor.size'] = 3 |
---|
| 65 | plt.rcParams['legend.fancybox'] = True |
---|
| 66 | plt.rcParams['xtick.major.pad'] = 4 |
---|
| 67 | plt.rcParams['ytick.major.pad'] = 12 |
---|
| 68 | plt.rcParams['figure.figsize'] = 7,4 |
---|
| 69 | |
---|
| 70 | axes = plt.gca() |
---|
| 71 | axes.set_xlim([0, 100]) |
---|
| 72 | plt.axes().xaxis.set_tick_params(width = 0.1) |
---|
| 73 | plt.axes().yaxis.set_tick_params(width = 0.1) |
---|
| 74 | |
---|
| 75 | plt.grid(True) |
---|
| 76 | self.draw_labels() |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | def draw_labels(self): |
---|
| 80 | plt.xlabel(self.xlabel) |
---|
| 81 | plt.ylabel(self.ylabel) |
---|
| 82 | pass |
---|
| 83 | |
---|
| 84 | def draw_title(self): |
---|
| 85 | plt.title(self.title) |
---|
| 86 | pass |
---|
| 87 | |
---|
| 88 | def plot(self): |
---|
| 89 | plt.stackplot(self.X, self.dataset, colors = self.actual_colors) |
---|
| 90 | |
---|
| 91 | def save(self, filename): |
---|
| 92 | plt.savefig(filename, bbox_inches = 'tight') |
---|
| 93 | |
---|
| 94 | def get_color(self): |
---|
| 95 | color_index = self.color_index |
---|
| 96 | self.color_index = (self.color_index + 1) % len(self.couleurs) |
---|
| 97 | return color_index |
---|
| 98 | |
---|
| 99 | def clear(self): |
---|
| 100 | # Other options to explore |
---|
| 101 | #plt.clf() |
---|
| 102 | #self.color_index = 0 |
---|
| 103 | #yminorLocator = MultipleLocator(5) |
---|
| 104 | #xmajorLocator = MultipleLocator(10) |
---|
| 105 | #xminorLocator = MultipleLocator(1) |
---|
| 106 | |
---|
| 107 | #plt.axes().yaxis.set_minor_locator(yminorLocator) |
---|
| 108 | #plt.axes().yaxis.set_label_position('left') |
---|
| 109 | #plt.axes().xaxis.set_minor_locator(xminorLocator) |
---|
| 110 | #plt.axes().xaxis.set_major_locator(xmajorLocator) |
---|
| 111 | #ylabel = plt.axes().yaxis.get_label() |
---|
| 112 | #ylabel.set_va('baseline') |
---|
| 113 | #ylabel.set_ha('center') |
---|
| 114 | #plt.grid(True) |
---|
| 115 | #self.draw_labels() |
---|
| 116 | #self.draw_title() |
---|
| 117 | pass |
---|