Outils du site


wiki:asymptote:exemples:graph

Modules graph et graph_pi

Cliquer sur les figures pour faire apparaître le code associé.

Représenter une fonction avec graph.asy (1)

void **xaxis**(picture pic=currentpicture, Label L="", axis axis=YZero, real xmin=-infinity, real xmax=infinity, pen p=currentpen, ticks ticks=NoTicks, arrowbar arrow=None, margin margin=NoMargin, bool above=false)
void **yaxis**(picture pic=currentpicture, Label L="", axis axis=XZero, real ymin=-infinity, real ymax=infinity, pen p=currentpen, ticks ticks=NoTicks, arrowbar arrow=None, margin margin=NoMargin, bool above=false, bool autorotate=true)

cf7319398b5de6155488e3de055583cf.png

import graph;
unitsize(x=1cm, y=0.5cm);
xlimits(-1.2, 5.5);
ylimits(-2.4, 3.5);
xaxis("$x$", Ticks(NoZero),Arrow(2mm));
yaxis("$y$" , Ticks(NoZero,Step=2,step=1),Arrow(2mm));
real f(real x) {return sqrt(x);}
path Cf=graph(f,0,5.5);
draw(Cf);
labelx("$O$",0,SW);
dot((0,0));

Représenter une fonction avec graph.asy (2)

void **xequals**(picture pic=currentpicture, Label L="", real x, bool extend=false, real ymin=-infinity, real ymax=infinity, pen p=currentpen, ticks ticks=NoTicks, arrowbar arrow=None, margin margin=NoMargin, bool above=true)
void **yequals**(picture pic=currentpicture, Label L="", real y, bool extend=false, real xmin=-infinity, real xmax=infinity, pen p=currentpen, ticks ticks=NoTicks, arrowbar arrow=None, margin margin=NoMargin, bool above=true)

d6804f265f80248b78760d67bff8e9f4.png

import graph;
unitsize(x=1cm, y=.5cm);

xlimits(-1, 5);
ylimits(-2, 3);

xaxis(BottomTop, Ticks("%",extend=true, pTick=linewidth(.5pt), ptick=dotted));
yaxis(LeftRight, Ticks("%",extend=true, pTick=linewidth(.5pt), ptick=dotted));
xequals(Label("$y$",align=2NW), 0, ymin=-3, ymax=4, p=linewidth(.5pt), Arrow(2mm));
yequals(Label("$x$",align=2SE), 0, xmin=-1.5, xmax=5.5, p=linewidth(.5pt), Arrow(2mm));
labelx(Label("$1$",UnFill), 1);
labely(Label("$1$",UnFill), 1);
dot("$O$",(0,0),2SW);

real f(real x) {return sqrt(x);}
path Cf=graph(f,0,5.5);
draw(Cf, bp+deepmagenta);

Représenter une fonction avec graph_pi.asy (1)

graph_pi.asy, extension de Philippe Ivaldi, est téléchargeable ici : http://www.piprime.fr/asymptote/unofficial-asymptote-packages/

void **graphicrules**(picture pic=currentpicture, real unit=1cm, real xunit=unit != 0 ? unit : 0, real yunit=unit != 0 ? unit : 0, real xmin=-infinity, real xmax=infinity, real ymin=-infinity, real ymax=infinity, bool crop=NoCrop, bool xcrop=crop, bool ycrop=crop)
void **grid**(picture pic=currentpicture, real xmin=pic.userMin.x, real xmax=pic.userMax.x, real ymin=pic.userMin.y, real ymax=pic.userMax.y, real xStep=1, real xstep=.5, real yStep=1, real ystep=.5, pen pTick=currentpen, pen ptick=grey, bool above=false)
void **cartesianaxis**(picture pic=currentpicture, Label Lx=Label("$x$",align=2S), Label Ly=Label("$y$",align=2W), real xmin=-infinity, real xmax=infinity, real ymin=-infinity, real ymax=infinity, real extrawidth=1, real extraheight=extrawidth, pen p=currentpen, ticks xticks=Ticks("%",pTick=nullpen, ptick=grey), ticks yticks=Ticks("%",pTick=nullpen, ptick=grey), bool viewxaxis=true, bool viewyaxis=true, bool above=true, arrowbar arrow=Arrow)

1f35f63c35fd3fcdfc9e53d6b5f32c5a.png

import graph_pi;
graphicrules(xunit=1cm, yunit=.5cm, xmin=-1, xmax=5, ymin=-2, ymax=3);
grid(ptick=dotted);
cartesianaxis(xticks=Ticks("%"), yticks=Ticks("%"), Arrow(2mm));
labelx(Label("$1$",UnFill), 1);
labely(Label("$1$",UnFill), 1);
dot("$O$",(0,0),2SW);
real f(real x) {return sqrt(x);}
path Cf=graph(f,0,5.5);
draw(Cf, bp+deepmagenta);

Représenter une fonction avec graph_pi.asy (2)

void **labeloij**(picture pic=currentpicture, Label Lo=Label("$O$",NoFill), Label Li=Label("$\overrightarrow{\imath}$",NoFill), Label Lj=Label("$\overrightarrow{\jmath}$",NoFill), pen p=scale(2)*currentpen, pair diro=SW, pair diri=labelijmargin*S, pair dirj=labelijmargin*1.5*W, filltype filltype=NoFill, arrowbar arrow=Arrow(2mm), marker marker=dot)
void **labeloIJ**(picture pic=currentpicture, Label Lo=Label("$O$",NoFill), Label LI=Label("$I$",NoFill), Label LJ=Label("$J$",NoFill), pair diro=SW, pair dirI=labelIJmargin*S, pair dirJ=labelIJmargin*W, pen p=currentpen, filltype filltype=NoFill, marker marker=dot)

15030c709a06dff12c559cd1ba8d5df3.png

import graph_pi;
graphicrules(xunit=1cm, yunit=1cm, xmin=-5, xmax=5, ymin=-3, ymax=3);
grid(ptick=dotted);
cartesianaxis(xticks=Ticks("%"), yticks=Ticks("%"), Arrow(2mm));
labeloij();
real f(real x) {return .5*x^2*sin(x);}
path Cf=graph(f,-5,5,n=400);
draw(Cf, bp+blue);
label("$\mathscr{C}_f$",(-3.5,f(-3.5)),NE,blue);
ylimits(-3.5,3.5,Crop);

Représenter une fonction avec graph_pi.asy (3)

picture **millimeterpaper**(picture pic=currentpicture, pair O=(0,0), real xmin=infinity, real xmax=infinity, real ymin=infinity, real ymax=infinity, pen p=.5bp+orange)

340b8532e371e52befbf44752941f6dd.png

import graph_pi;
graphicrules(xunit=1cm, yunit=1cm, xmin=-5, xmax=5, ymin=-3, ymax=3);
add(millimeterpaper(p=bp+orange),(0,0));
cartesianaxis(xticks=Ticks("%"), yticks=Ticks("%"), Arrow(2mm));
labeloij();
real f(real x) {return .5*x^2*sin(x);}
path Cf=graph(f,-5,5);
draw(Cf, bp+blue);
label("$\mathscr{C}_f$",(-3.5,f(-3.5)),NE,blue);
ylimits(-3.5,3.5,Crop);

Fonction non définie en un point (1)

Première méthode : on trace plusieurs morceaux de courbe

c2edcfa22d3e6921b80e057e6089efd5.png

import graph_pi;

graphicrules(xunit=1cm, yunit=1cm, xmin=-4, xmax=6, ymin=-3, ymax=5);
grid(ptick=dotted);
cartesianaxis(xticks=Ticks(ticklabel=OmitFormat(1),scale(.8)*Label(filltype=Fill(white)),NoZero),
		yticks=Ticks(ticklabel=OmitFormat(1),scale(.8)*Label(filltype=Fill(white)),NoZero), Arrow(2mm));
labeloij();

real f(real x) {return (x-2)/(x-1);}
path Cf1=graph(f,-4.5,.9);
path Cf2=graph(f,1.1,6.5);
draw(Cf1^^Cf2, bp+darkgreen);
label("$\mathscr{C}_f$",(-3.5,f(-3.5)),N,darkgreen);

ylimits(-3.5,5.5,Crop);

Fonction non définie en un point (2)

Deuxième méthode : utilisation du module contour.asy.

return:    contour guides for a real-valued function
f:         real-valued function of two real variables
a,b:       diagonally opposite vertices of rectangular domain
c:         array of contour values
nx,ny:     number of subdivisions in x and y directions(determines accuracy)
join:      interpolation operator (e.g. operator--or operator ..)
subsample: number of interior points to include in each grid square (in addition to points on edge)

guide[][] **contour**(real f(real, real), pair a, pair b, real[] c, int nx=ngraph, int ny=nx, interpolate join=operator--, int subsample=1)

De $y=\frac{x-2}{x-1}$, on tire $xy-y-x=-2$, d'où le codage suivant.

c7e2050d7d33334baaa9fcd9d31c95df.png

import graph_pi;
import contour;

graphicrules(xunit=1cm, yunit=1cm, xmin=-4, xmax=6, ymin=-3, ymax=5);
grid(ptick=dotted);
cartesianaxis(xticks=Ticks(ticklabel=OmitFormat(1),scale(.8)*Label(filltype=Fill(white)),NoZero),
		yticks=Ticks(ticklabel=OmitFormat(1),scale(.8)*Label(filltype=Fill(white)),NoZero), Arrow(2mm));
labeloij();

real f(real x, real y) {return x*y-y-x;}
draw(contour(f,(-4.5,-3.5),(6.5,5.5),new real[] {-2}), bp+darkgreen);

label("$\mathscr{C}_f$",(-3.5,1.5),N,darkgreen);

Fonction définie par points et nombres dérivés

041179b399de53ed96775786f5a8d98d.png

import graph_pi;
import interpolate;
unitsize(1cm);
graphicrules(xunit=1cm, yunit=1cm, xmin=-5, xmax=8, ymin=-2.5, ymax=7);
grid(pTick=.7*bp+.8*grey,ptick=dotted+.7*bp+.8*grey);
cartesianaxis();
labeloij();
marker croix=marker(scale(4)*rotate(45)*cross(4),linewidth(1.1*bp));
real[] xn={-5.5,-3,0,4,6,7.5};
real[] yn={-2.5,0,1,5,3,-3};
real[] dy={1.2,2/3,0,0,-2,-7};
for (int i=1; i<5; ++i) {
draw((xn[i],yn[i]),croix);
drawline((xn[i],yn[i]),(xn[i],yn[i])+(1,dy[i]),bp+.8red);
}
real f(real t)
{ return pwhermite(xn,yn,dy)(t);}
path Cf=graph(f,-5.5,7.5);
draw(Cf,1.1bp+.8blue);
label(scale(1.2)*"$\mathscr{C}_f$",(7.2,f(7.2)),SW,.8*blue);
xlimits(-5.5,8.5,Crop);
ylimits(-3,7.5,Crop);
pair pA=(0,2), pB=(7,1);
draw(pA^^pB,croix);

Diagramme en barres

Utilisation de la fonction fixedscaling intéressante dans cet exemple.

b45c16642104618261d12a84f4b9986e.png

unitsize(x=2cm, y=1mm); 
import graph;

real[] datas = {24,60,56,20};

void bargraph(real x, real y, real width = 0.30)
{
    filldraw(box((x-width,0),(x+width,y)), drawpen=blue, fillpen=lightblue);
}

real x = 0; 
for(real y : datas)
{
    bargraph(x, y);
    x = x+1;
}

fixedscaling((0,0),(datas.length-0.5,max(datas)+2)); 

xaxis(defaultpen+black, Ticks(Step = 1, Size = 2), above = true); 
yaxis(XEquals(-1/2), defaultpen+black, Ticks(Step = 10, Size = 2, NoZero), above = true);

shipout(bbox(xmargin=1mm,invisible));

wiki/asymptote/exemples/graph.txt · Dernière modification: 2017/08/01 09:28 par MB