You are on page 1of 5
i M ul a Wd y i i NUMF@CUS OPEN CODE = BETTER SCIENCE Matplotlib for beginners Mato i a rary for making 2 plots Python. is designed with te philosophy that you shuld be able to ‘esteem lots tn just 3 few commends snport ounpy a2 9p Anport ratplotlsb:pyplot as plt [Z]Prepare 1000) (Slrender ag, x « ple aubploe() senae tenon) [lobserve Matlotib offers several kindof pa (se Calle) 160) 10) A sre random natorta, | = roaon storm, | a8) X = np arange(10) ¥ © np rand. unform(1 ax baie, SnaaeeIE x. snanow(2) 2 + np-rendon untora(9, 1, (8.8) se contourf(2) 2 = np. random unifore(®, 1,4) ox.ste(2) 2 np.randon.norma(8, 1,188) ox hist(2) X= np.erange(s) YY np-randon unstore(@, 1.5) be errorbar(s. Yi) 2 = np. candon normal(®, 3, (180,2)) ax boxplot(2) © BB A [372] Tweak ‘Yousan mo prety ruch anthingina lt neg 15 colors marr, ne with and styles, oks and teks 0 tiles, ee X= np Linspace(2, 10,100) Y= mp-2in(3) x plot(X, ¥, color="bleck") X = np.Lanepaee(@, 10,108) aaa Y= mp sin) i Be plee(t, ¥, tinesty X = np-Linspece(®, 18,108) Y# npsin(3) axcplot(x, Y, 2inewdthes) X = np.Lénepace(®, 10,108) Y= np-2in3) Su plet(X, Y, marker="o") Organize ‘Youean plot severaldataon the te same fgurebucyoucan aso pita gue sever! subplot (need Aves) WAN X = np.Linspace(0, 10,108) M1, Vh'= mp sinGX), mo.£08(X) ax plot(x 11, Y2) fig. (axl. a2) = plt.subplotst(2.1)) sel ‘plot(%, Vt, eolore"c1") ‘2 plot(x, 12) eoler="ca") fig, (ax). od) = plt.eubplots((1,2)) xd plot(t1, x, eoler="c1") 432 ploti¥2, X) solor=-ca") Label (everything) ax plot(x. ¥) fear Hatattaaioe) y |x set-ylabel (one) ped Tane") es Fgutes ae shown wih a graphical user irterace that a Toms to 200m and pan the fgur to navigate between the ‘Seren ews and show the value under the mouse ‘Save (bitmap or vector format) tig fig pi-200) sa ay-tret-tigure. pg? veFaalmy-First-Tigure bat") Matplotlib for intermediate users ‘A matpoti gues composed ofa hierarchy of ements Tieks &labels ‘hat fore te actual igure. Each erent canbe modi from rol. tsckersapore pulesplelacator a= WL soy te ‘s ¢ oS] See foo O00 9° = fig a6e.gradepec(s, 3) x = Fig ada aubptot(asi®, °1) fe et fececolor("essaart’) plt-subpote() top] set color (None {4 apineel “right -set-eolor(Nene”) —— i (OCIS) Text & Oomaments ter dnport saiatroratier se oF ior loetor 9 2)) nor -fomatter(S()) rraCone a achsnior, roca) Lines & markers X= np-Linspace(@.1, 1099.94, 1600) Y= np-2in03) ecplot(X, ¥, “Cros, markeverys26, mece"t 8°) 9") 4. FAL betweenx((-1,11,(@1.[2¢0p.24)) fsectext(@, 1, Perdog S\Phis") Legend sx plot(X, np-sing), ci ‘xplot(x, np.coa(x), “C1", Lebele"cosine”) ‘x legend(obox. to-ancnor=(@,1.1.-1), nea rodec'expend”, 10ec"iower lft") tabet=*Sine") WIIG Annotation exomotate( A", (XL2501 2801), (x12521.-1), (RN ‘any alo canbe use but Mepotib offers ses of clos: Size & DPI \Coradera square guetobe included natwo columns paper wth emmargnscn each se and acolurm separa fon of tom. The width ofa fgure fe(21-292- 2 = Bom ‘Ono inch being 2 540m, fue 828 shoulbe 3.153.181, fag = ple tagure(tigeize-(3.15,4.18), 491-50) plt soverig( fiqure-pat”, dpi-600) Matplotlib tips & tricks Transparency Seater pla om be eheroe by sing spare San Soe oa stars at cere ee ee ae rene i Les. oP SSNer die car" Rasterization Ifyour four ts made of fot aches elements such asa huge seater you can rasterze them o save memory and oop other elements vector erat 1 rang sormlet, 4 18) Offline rendering Use the Ag backend to render igure ety inn aay q ewes enero. bert) Range of continuous colors You can use colormap to pick ange of continuous cbs. lige ne see rs Toten) Yor iin 1.2.4. 6.811 ‘Text outline Use text eutinet make text more visible. Sect enh, front Multiline plot ‘Youcan pot several ine a one wing None a separate. n(n, tne) ill ly il, Dotted lines Tohaverounded sted ines, use acustom Tinestyle and modify dash capseye Combining axes ‘You can use overaid ases wth ferent projections, Colorbar adjustment ‘Youcan adjust coiorbar aspect when adding ‘Taking advantage of typography Youcan use a condensed face sich as Roboto Condensed to save space ance bel manclornee( hatte Sree) Getting rid of margins (Once your fous fished, you can call tight_Layout() 1o rrnowe white margins there ae eran margins yousanuse theceferop uty (comes with TeX ne), Hatching ‘Youcan achieve nice sual eft wth hk hatch pattems, 2 i i Read the documentation aipltio comes wth an extensive docimenaton expan ing every deals ofeach command and = general 2csom- panted by examples wih Together wth the huge one (Galery this cocumenaton ea galsmne

You might also like