matplotlib

La bibliothèque

Matplotlib est une bibliothèque de tracé deux dimensions pour python.
D’une très grande richesse, elle propose en autres :
- tous les types classiques de tracé : courbes, histogrammes, diagrammes à barres, nuages de points, etc.
- un mode à la Matlab® avec pylab (utilisé dans l’exemple ci-dessous)
- une intégration facile avec des applications web et six bibliothèques d’interface graphique (Qt, gtk, etc.)
- export des figures en de nombreux formats, dont le SVG.

La documentation

La documentation de Matplotlib est excellente, surtout grâce à la galerie d’exemples illustrés : Matplotlib — Thumbnail gallery

Autres sources d’exemples et d’astuces :
- Cookbook/Matplotlib -
- Plotting data using Matplotlib : Part 2

Un exemple

Comme exemple, je vous propose de tracer les courbes de croissance standards de l’OMS disponible au lien suivant : WHO

Sans être une référence, il y a surement mille façons de faire cela, ce script concentre de nombreuses astuces glanées au fil du temps, comme :
- sélection des couleurs dans un color map
- lecture directe des fichiers CSV
- axes partagés
- min max des axes imposés
- masquage des repères d’axe
- etc.

  1. # -*- coding:utf-8 -*-
  2. """
  3. Drawing Standard Child Grow curve with Matplotlib
  4. More info : http://intrw.net/log/spip.php?article149
  5. """
  6. import pylab
  7.  
  8.  
  9. def getColor(name, n):
  10.     # Usage name is one of the cm (colormaps) names
  11.     # cl = getColor('jet', 4)
  12.     # then call cl(i) with i between 1 and n
  13.     # A list of cm is available here:
  14.     # http://matplotlib.sourceforge.net/
  15.     # examples/pylab_examples/show_colormaps.html
  16.     import matplotlib.cm as cm
  17.     return cm.get_cmap(name, lut=n+2)
  18.  
  19. # Define text box appearance
  20. percent_box = dict(boxstyle="round",
  21.                    ec='none',
  22.                    fc='w',
  23.                    alpha=0.5)
  24.  
  25. #
  26. # Load data
  27. #
  28.  
  29. # Load boys data
  30. boys_bfa = pylab.csv2rec('bfa_boys_p_exp.txt', delimiter='\t')
  31. boys_lhfa = pylab.csv2rec('lhfa_boys_p_exp.txt', delimiter='\t')
  32. boys_wfa = pylab.csv2rec('wfa_boys_p_exp.txt', delimiter='\t')
  33.  
  34. # Load girls data
  35. girls_bfa = pylab.csv2rec('bfa_girls_p_exp.txt', delimiter='\t')
  36. girls_lhfa = pylab.csv2rec('lhfa_girls_p_exp.txt', delimiter='\t')
  37. girls_wfa = pylab.csv2rec('wfa_girls_p_exp.txt', delimiter='\t')
  38.  
  39. f = pylab.figure()
  40.  
  41. #
  42. # Boys
  43. #
  44.  
  45. # Get boys color list
  46. cl = getColor('PuBu', 3)
  47.  
  48. # Plot body mass index boy data
  49. ax1 = pylab.subplot(321)
  50. ax1.grid(True)
  51.  
  52. pylab.fill_between(boys_bfa.day, boys_bfa.p3, 0, color= cl(1), alpha=0.5)
  53. pylab.plot(boys_bfa.day, boys_bfa.p3, color= cl(1))
  54. pylab.plot(boys_bfa.day, boys_bfa.p15, color= cl(2))
  55. pylab.plot(boys_bfa.day, boys_bfa.p50, color= cl(3))
  56. pylab.plot(boys_bfa.day, boys_bfa.p85, color= cl(2))
  57. pylab.plot(boys_bfa.day, boys_bfa.p97, color= cl(1))
  58. pylab.fill_between(boys_bfa.day, boys_bfa.p97,
  59.                    max(max(boys_bfa.p97), max(boys_bfa.p97))*1.2,
  60.                    color= cl(1), alpha=0.5)
  61.  
  62. # Add percentage for BMI boy  data
  63. pylab.text(max(boys_bfa.day)-10, boys_bfa.p3[-1:], "3%", size=10,
  64.            ha="right", va="center",
  65.            bbox = percent_box)
  66. pylab.text(max(boys_bfa.day)-10, boys_bfa.p15[-1:], "15%", size=10,
  67.            ha="right", va="center",
  68.            bbox = percent_box)
  69. pylab.text(max(boys_bfa.day)-10, boys_bfa.p50[-1:], "50%", size=10,
  70.            ha="right", va="center",
  71.            bbox = percent_box)
  72. pylab.text(max(boys_bfa.day)-10, boys_bfa.p85[-1:], "85%", size=10,
  73.            ha="right", va="center",
  74.            bbox = percent_box)
  75. pylab.text(max(boys_bfa.day)-10, boys_bfa.p97[-1:], "97%", size=10,
  76.            ha="right", va="center",
  77.            bbox = percent_box)
  78.  
  79. pylab.xlim(0, max(boys_bfa.day))
  80. pylab.ylim(min(min(girls_bfa.p3), min(boys_bfa.p3))*0.8,
  81.            max(max(boys_bfa.p97), max(boys_bfa.p97))*1.2)
  82. pylab.title(u"Boys")
  83. #~ pylab.xlabel(u"Days")
  84. pylab.ylabel(u"BMI (kg/m²)")
  85.  
  86. # Plot lenght heigh boy data
  87. ax3 = pylab.subplot(323)
  88. ax3.grid(True)
  89.  
  90. pylab.fill_between(boys_lhfa.day, boys_lhfa.p3, 0, color= cl(1), alpha=0.5)
  91. pylab.plot(boys_lhfa.day, boys_lhfa.p3, color= cl(1))
  92. pylab.plot(boys_lhfa.day, boys_lhfa.p15, color= cl(2))
  93. pylab.plot(boys_lhfa.day, boys_lhfa.p50, color= cl(3))
  94. pylab.plot(boys_lhfa.day, boys_lhfa.p85, color= cl(2))
  95. pylab.plot(boys_lhfa.day, boys_lhfa.p97, color= cl(1))
  96. pylab.fill_between(boys_lhfa.day, boys_lhfa.p97,
  97.                    max(max(girls_lhfa.p97), max(boys_lhfa.p97))*1.2,
  98.                    color= cl(1), alpha=0.5)
  99.  
  100. # Add percentage for lenght / height boy  data
  101. pylab.text(max(boys_lhfa.day)-10, boys_lhfa.p3[-1:], "3%", size=10,
  102.            ha="right", va="center",
  103.            bbox = percent_box)
  104. pylab.text(max(boys_lhfa.day)-10, boys_lhfa.p15[-1:], "15%", size=10,
  105.            ha="right", va="center",
  106.            bbox = percent_box)
  107. pylab.text(max(boys_lhfa.day)-10, boys_lhfa.p50[-1:], "50%", size=10,
  108.           ha="right", va="center",
  109.           bbox = percent_box)
  110. pylab.text(max(boys_lhfa.day)-10, boys_lhfa.p85[-1:], "85%", size=10,
  111.            ha="right", va="center",
  112.            bbox = percent_box)
  113. pylab.text(max(boys_lhfa.day)-10, boys_lhfa.p97[-1:], "97%", size=10,
  114.            ha="right", va="center",
  115.            bbox = percent_box)
  116.  
  117. pylab.xlim(0, max(boys_lhfa.day))
  118. pylab.ylim(min(min(girls_lhfa.p3), min(boys_lhfa.p3))*0.8,
  119.                max(max(girls_lhfa.p97), max(boys_lhfa.p97))*1.2)
  120. #~ pylab.xlabel(u"Days")
  121. pylab.ylabel(u"Height (cm)")
  122.  
  123. # Plot weight boy data
  124. ax5 = pylab.subplot(325)
  125. ax5.grid(True)
  126.  
  127. pylab.fill_between(boys_wfa.day, boys_wfa.p3, 0, color= cl(1), alpha=0.5)
  128. pylab.plot(boys_wfa.day, boys_wfa.p3, color= cl(1))
  129. pylab.plot(boys_wfa.day, boys_wfa.p15, color= cl(2))
  130. pylab.plot(boys_wfa.day, boys_wfa.p50, color= cl(3))
  131. pylab.plot(boys_wfa.day, boys_wfa.p85, color= cl(2))
  132. pylab.plot(boys_wfa.day, boys_wfa.p97, color= cl(1))
  133. pylab.fill_between(boys_wfa.day, boys_wfa.p97,
  134.                    max(max(girls_wfa.p97), max(girls_wfa.p97))*1.2,
  135.                    color= cl(1), alpha=0.5)
  136.  
  137. # Add percentage for weight boy data
  138. pylab.text(max(boys_wfa.day)-10, boys_wfa.p3[-1:], "3%", size=10,
  139.            ha="right", va="center",
  140.            bbox = percent_box)
  141. pylab.text(max(boys_wfa.day)-10, boys_wfa.p15[-1:], "15%", size=10,
  142.            ha="right", va="center",
  143.            bbox = percent_box)
  144. pylab.text(max(boys_wfa.day)-10, boys_wfa.p50[-1:], "50%", size=10,
  145.            ha="right", va="center",
  146.            bbox = percent_box)
  147.  
  148. pylab.text(max(boys_wfa.day)-10, boys_wfa.p85[-1:], "85%", size=10,
  149.            ha="right", va="center",
  150.            bbox = percent_box)
  151. pylab.text(max(boys_wfa.day)-10, boys_wfa.p97[-1:], "97%", size=10,
  152.            ha="right", va="center",
  153.            bbox = percent_box)
  154.  
  155. pylab.xlim(0, max(boys_wfa.day))
  156. pylab.ylim(min(min(girls_wfa.p3), min(boys_wfa.p3))*0.8,
  157.            max(max(girls_wfa.p97), max(girls_wfa.p97))*1.2)
  158. pylab.xlabel(u"Days")
  159. pylab.ylabel(u"Weight (kg)")
  160.  
  161. #
  162. # Girls
  163. #
  164.  
  165. # Get a girly color list
  166. cl = getColor('PuRd', 3)
  167.  
  168. # Plot body mass index girl data
  169. ax2 = pylab.subplot(322)
  170. ax2.grid(True)
  171.  
  172. pylab.fill_between(girls_bfa.day, girls_bfa.p3, 0, color= cl(1), alpha=0.5)
  173. pylab.plot(girls_bfa.day, girls_bfa.p3, color= cl(1))
  174. pylab.plot(girls_bfa.day, girls_bfa.p15, color= cl(2))
  175. pylab.plot(girls_bfa.day, girls_bfa.p50, color= cl(3))
  176. pylab.plot(girls_bfa.day, girls_bfa.p85, color= cl(2))
  177. pylab.plot(girls_bfa.day, girls_bfa.p97, color= cl(1))
  178. pylab.fill_between(girls_bfa.day, girls_bfa.p97,
  179.                    max(max(girls_bfa.p97), max(boys_bfa.p97))*1.2,
  180.                    color= cl(1), alpha=0.5)
  181.  
  182. # Add percentage for BMI girl data
  183. pylab.text(max(girls_bfa.day)-10, girls_bfa.p3[-1:], "3%", size=10,
  184.            ha="right", va="center",
  185.            bbox = percent_box)
  186. pylab.text(max(girls_bfa.day)-10, girls_bfa.p15[-1:], "15%", size=10,
  187.            ha="right", va="center",
  188.            bbox = percent_box)
  189. pylab.text(max(girls_bfa.day)-10, girls_bfa.p50[-1:], "50%", size=10,
  190.            ha="right", va="center",
  191.            bbox = percent_box)
  192. pylab.text(max(girls_bfa.day)-10, girls_bfa.p85[-1:], "85%", size=10,
  193.            ha="right", va="center",
  194.            bbox = percent_box)
  195. pylab.text(max(girls_bfa.day)-10, girls_bfa.p97[-1:], "97%", size=10,
  196.            ha="right", va="center",
  197.            bbox = percent_box)
  198.  
  199. pylab.xlim(0, max(girls_bfa.day))
  200. pylab.ylim(min(min(girls_bfa.p3), min(boys_bfa.p3))*0.8,
  201.            max(max(girls_bfa.p97), max(boys_bfa.p97))*1.2)
  202. pylab.title(u"Girls")
  203. # No label
  204. #~ pylab.xlabel(u"Days")
  205. #~ pylab.ylabel(u"Body mass index (kg/m²)")
  206.  
  207. # Plot lenght heigh girl data
  208. ax4 = pylab.subplot(324)
  209. ax4.grid(True)
  210.  
  211. pylab.fill_between(girls_lhfa.day, girls_lhfa.p3, 0, color= cl(1), alpha=0.5)
  212. pylab.plot(girls_lhfa.day, girls_lhfa.p3, color= cl(1))
  213. pylab.plot(girls_lhfa.day, girls_lhfa.p15, color= cl(2))
  214. pylab.plot(girls_lhfa.day, girls_lhfa.p50, color= cl(3))
  215. pylab.plot(girls_lhfa.day, girls_lhfa.p85, color= cl(2))
  216. pylab.plot(girls_lhfa.day, girls_lhfa.p97, color= cl(1))
  217. pylab.fill_between(girls_lhfa.day, girls_lhfa.p97,
  218.                    max(max(girls_lhfa.p97), max(boys_lhfa.p97))*1.2,
  219.                    color= cl(1), alpha=0.5)
  220.  
  221. # Add percentage for lenght / height girl data
  222. pylab.text(max(girls_lhfa.day)-10, girls_lhfa.p3[-1:], "3%", size=10,
  223.            ha="right", va="center",
  224.            bbox = percent_box)
  225. pylab.text(max(girls_lhfa.day)-10, girls_lhfa.p15[-1:], "15%", size=10,
  226.            ha="right", va="center",
  227.            bbox = percent_box)
  228. pylab.text(max(girls_lhfa.day)-10, girls_lhfa.p50[-1:], "50%", size=10,
  229.            ha="right", va="center",
  230.            bbox = percent_box)
  231. pylab.text(max(girls_lhfa.day)-10, girls_lhfa.p85[-1:], "85%", size=10,
  232.            ha="right", va="center",
  233.            bbox = percent_box)
  234. pylab.text(max(girls_lhfa.day)-10, girls_lhfa.p97[-1:], "97%", size=10,
  235.            ha="right", va="center",
  236.            bbox = percent_box)
  237.  
  238. pylab.xlim(0, max(girls_lhfa.day))
  239. pylab.ylim(min(min(girls_lhfa.p3), min(boys_lhfa.p3))*0.8,
  240.            max(max(girls_lhfa.p97), max(boys_lhfa.p97))*1.2)
  241. # No label
  242. #~ pylab.xlabel(u"Days")
  243. #~ pylab.ylabel(u"Length/height (cm)")
  244.  
  245. # Plot weight girl data
  246. ax6 = pylab.subplot(326)
  247. ax6.grid(True)
  248.  
  249. pylab.fill_between(girls_wfa.day, girls_wfa.p3, 0, color= cl(1), alpha=0.5)
  250. pylab.plot(girls_wfa.day, girls_wfa.p3, color= cl(1))
  251. pylab.plot(girls_wfa.day, girls_wfa.p15, color= cl(2))
  252. pylab.plot(girls_wfa.day, girls_wfa.p50, color= cl(3))
  253. pylab.plot(girls_wfa.day, girls_wfa.p85, color= cl(2))
  254. pylab.plot(girls_wfa.day, girls_wfa.p97, color= cl(1))
  255. pylab.fill_between(girls_wfa.day, girls_wfa.p97,
  256.                    max(max(girls_wfa.p97), max(boys_wfa.p97))*1.2,
  257.                    color= cl(1), alpha=0.5)
  258.  
  259. # Add percentage for weight girl data
  260. pylab.text(max(girls_wfa.day)-10, girls_wfa.p3[-1:], "3%", size=10,
  261.            ha="right", va="center",
  262.            bbox = percent_box)
  263. pylab.text(max(girls_wfa.day)-10, girls_wfa.p15[-1:], "15%", size=10,
  264.            ha="right", va="center",
  265.            bbox = percent_box)
  266. pylab.text(max(girls_wfa.day)-10, girls_wfa.p50[-1:], "50%", size=10,
  267.            ha="right", va="center",
  268.            bbox = percent_box)
  269. pylab.text(max(girls_wfa.day)-10, girls_wfa.p85[-1:], "85%", size=10,
  270.            ha="right", va="center",
  271.            bbox = percent_box)
  272. pylab.text(max(girls_wfa.day)-10, girls_wfa.p97[-1:], "97%", size=10,
  273.            ha="right", va="center",
  274.            bbox = percent_box)
  275.  
  276. pylab.xlim(0, max(girls_wfa.day))
  277. pylab.ylim(min(min(girls_wfa.p3), min(boys_wfa.p3))*0.8,
  278.            max(max(girls_wfa.p97), max(boys_wfa.p97))*1.2)
  279. pylab.xlabel(u"Days")
  280. # No label
  281. #~ pylab.ylabel(u"Weight (kg)")
  282.  
  283.  
  284. pylab.subplots_adjust(hspace=0.001, wspace=0.001)
  285.  
  286. #
  287. # Personalise graph
  288. #
  289.  
  290. # Main title
  291. pylab.figtext(0.5, 0.960, 'Standard Grow Curves',
  292.               ha='center', color='black', weight='bold', size='large')
  293.  
  294. # Hide non wished tick labels
  295. xticklabels = (ax1.get_xticklabels() + ax2.get_xticklabels()
  296.              + ax3.get_xticklabels() + ax4.get_xticklabels())
  297. pylab.setp(xticklabels, visible=False)
  298. yticklabels = (ax2.get_yticklabels()
  299.              + ax4.get_yticklabels()
  300.              + ax6.get_yticklabels())
  301. pylab.setp(yticklabels, visible=False)
  302.  
  303. #
  304. # Finally display graph
  305. #
  306. pylab.show()

Télécharger

info portfolio

Résultat du script
 
A propos de INTRW

Journal web de Benoît LAURENT, entre photos, voyages et logiciels libres selon les périodes.

Fils de nouvelles RSS
Thèmes