Plot coordinate pairs: Difference between revisions

m
(added Ol)
 
(54 intermediate revisions by 24 users not shown)
Line 2:
 
;Task:
Plot a function represented as &nbsp;&nbsp; <big><big> `x', &nbsp; `y' </big></big> &nbsp;&nbsp; numerical arrays.
 
Post the resulting image for the following input arrays (taken from [[Time_a_function#Python|Python's Example section on ''Time a function'']]):
x = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
y = {2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0};
 
This task is intended as a subtask for [[Measure relative performance of sorting algorithms implementations]].
<br><br>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program areaPlot64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ HAUTEUR, 22
.equ LARGEUR, 50
.equ MARGEGAUCHE, 10
 
/*******************************************/
/* Structures */
/********************************************/
/* structure for points */
.struct 0
point_posX:
.struct point_posX + 8
point_posY:
.struct point_posY + 8
point_end:
/*******************************************/
/* Initialized data */
/*******************************************/
.data
szMessError: .asciz "Number of points too large !! \n"
szCarriageReturn: .asciz "\n"
szMessMovePos: .ascii "\033[" // cursor position
posY: .byte '0'
.byte '6'
.ascii ";"
posX: .byte '0'
.byte '3'
.asciz "H*"
szMessEchelleX: .asciz "Y^ X="
szClear1: .byte 0x1B
.byte 'c' // other console clear
.byte 0
szMessPosEch: .ascii "\033[" // scale cursor position
posY1: .byte '0'
.byte '0'
.ascii ";"
posX1: .byte '0'
.byte '0'
.asciz "H"
 
//x = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
//y = {2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0};
/* areas points */
tbPoints: .quad 0 // 1
.quad 27 // Data * 10 for integer operation
.quad 1 // 2
.quad 28
.quad 2 // 3
.quad 314
.quad 3 // 4
.quad 381
.quad 4 // 5
.quad 580
.quad 5 // 6
.quad 762
.quad 6 // 7
.quad 1005
.quad 7 // 8
.quad 1300
.quad 8 // 9
.quad 1493
.quad 9 // 10
.quad 1800
/*******************************************/
/* UnInitialized data */
/*******************************************/
.bss
sZoneConv: .skip 30
/*******************************************/
/* code section */
/*******************************************/
.text
.global main
main: // entry of program
ldr x0,qAdrtbPoints // area address
mov x1,10 // size
mov x2,LARGEUR
mov x3,HAUTEUR
bl plotArea
b 100f
 
100: // standard end of the program
mov x0, 0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
 
qAdrsZoneConv: .quad sZoneConv
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrtbPoints: .quad tbPoints
/************************************/
/* create graph */
/************************************/
/* x0 contains area points address */
/* x1 contains number points */
/* x2 contains graphic weight */
/* x3 contains graphic height */
/* REMARK : no save x9-x20 registers */
plotArea:
stp x2,lr,[sp,-16]! // save registers
stp x3,x4,[sp,-16]! // save registers
cmp x1,x2
bge 99f
mov x9,x0
mov x4,x1
ldr x10,qAdrposX
ldr x11,qAdrposY
mov x12,#0 // indice
mov x13,point_end // element area size
mov x17,0 // Y maxi
mov x19,-1 // Y Mini
1: //search mini maxi
madd x14,x12,x13,x0 // load coord Y
ldr x15,[x14,point_posY]
cmp x15,x17
csel x17,x15,x17,hi // maxi ?
cmp x15,x19
csel x19,x15,x19,lo // mini ?
add x12,x12,#1
cmp x12,x1 // end ?
blt 1b // no -> loop
// compute ratio
udiv x15,x17,x3 // ratio = maxi / height
add x15,x15,1 // for adjust
ldr x0,qAdrszClear1 // clear screen
bl affichageMess
udiv x20,x2,x4 // compute interval X = weight / number points
mov x12,0 // indice
2: // loop begin for display point
madd x14,x12,x13,x9 // charge X coord point
ldr x16,[x14,point_posX]
mul x16,x20,x12 // interval * indice
add x0,x16,MARGEGAUCHE // + left margin
mov x1,x10 // conversion ascii and store
bl convPos
 
ldr x18,[x14,point_posY] // charge Y coord point
udiv x18,x18,x15 // divide by ratio
sub x0,x3,x18 // inversion position ligne
mov x1,x11 // conversion ascii and store
bl convPos
 
ldr x0,qAdrszMessMovePos // display * at position X,Y
bl affichageMess
add x12,x12,1 // next point
cmp x12,x4 // end ?
blt 2b // no -> loop
// display left scale
// display Y Mini
mov x0,0
ldr x1,qAdrposX1
bl convPos
mov x0,HAUTEUR
ldr x1,qAdrposY1
bl convPos
ldr x0,qAdrszMessPosEch
bl affichageMess
mov x0,x19
ldr x1,qAdrsZoneConv
bl conversion10
ldr x0,qAdrsZoneConv
bl affichageMess
// display Y Maxi
mov x0,0
ldr x1,qAdrposX1
bl convPos
mov x0,0
ldr x1,qAdrposY1
bl convPos
ldr x0,qAdrszMessPosEch
bl affichageMess
mov x0,x17
ldr x1,qAdrsZoneConv
bl conversion10
ldr x0,qAdrsZoneConv
bl affichageMess
// display average value
mov x0,0
ldr x1,qAdrposX1
bl convPos
mov x0,HAUTEUR/2
add x0,x0,#1
ldr x1,qAdrposY1
bl convPos
ldr x0,qAdrszMessPosEch
bl affichageMess
lsr x0,x17,#1
ldr x1,qAdrsZoneConv
bl conversion10
ldr x0,qAdrsZoneConv
bl affichageMess
 
// display X scale
mov x0,0
ldr x1,qAdrposX1
bl convPos
mov x0,HAUTEUR+1
ldr x1,qAdrposY1
bl convPos
ldr x0,qAdrszMessPosEch
bl affichageMess
ldr x0,qAdrszMessEchelleX
bl affichageMess
 
 
mov x12,0 // indice
mov x19,MARGEGAUCHE
10:
udiv x20,x2,x4
madd x0,x20,x12,x19
ldr x1,qAdrposX1
bl convPos
mov x0,HAUTEUR+1
ldr x1,qAdrposY1
bl convPos
ldr x0,qAdrszMessPosEch
bl affichageMess
madd x14,x12,x13,x9 // load X coord point
ldr x0,[x14,point_posX]
ldr x1,qAdrsZoneConv
bl conversion10
ldr x0,qAdrsZoneConv
bl affichageMess
add x12,x12,1
cmp x12,x4
blt 10b
 
ldr x0,qAdrszCarriageReturn
bl affichageMess
 
mov x0,0 // return code
b 100f
99: // error
ldr x0,qAdrszMessError
bl affichageMess
mov x0,-1 // return code
100:
ldp x3,x4,[sp],16 // restaur 2 registers
ldp x2,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
qAdrszMessMovePos: .quad szMessMovePos
qAdrszClear1: .quad szClear1
qAdrposX: .quad posX
qAdrposY: .quad posY
qAdrposX1: .quad posX1
qAdrposY1: .quad posY1
qAdrszMessEchelleX: .quad szMessEchelleX
qAdrszMessPosEch: .quad szMessPosEch
qAdrszMessError: .quad szMessError
/************************************/
/* conv position in ascii and store at address */
/************************************/
/* x0 contains position */
/* x1 contains string address */
convPos:
stp x2,lr,[sp,-16]! // save registers
stp x3,x4,[sp,-16]! // save registers
mov x2,10
udiv x3,x0,x2
add x4,x3,48 // convert in ascii
strb w4,[x1] // store posX
msub x4,x3,x2,x0
add x4,x4,48
strb w4,[x1,1]
100:
ldp x3,x4,[sp],16 // restaur 2 registers
ldp x2,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
 
</syntaxhighlight>
{{Output}}
<pre>
1800 *
 
 
*
 
 
*
 
 
*
 
900
*
 
*
 
 
*
*
 
 
27 * *
Y^ X= 0 1 2 3 4 5 6 7 8 9
 
</pre>
 
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
 
DEFINE PTR="CARD"
DEFINE BUF_SIZE="100"
DEFINE REAL_SIZE="3"
 
TYPE Settings=[
INT xMin,xMax,xStep,yMin,yMax,yStep
INT xLeft,xRight,yTop,yBottom
INT tickLength]
 
BYTE ARRAY xs(BUF_SIZE),ys(BUF_SIZE)
BYTE count=[0]
 
PTR FUNC GetXPtr(BYTE i)
RETURN (xs+3*i)
 
PTR FUNC GetYPtr(BYTE i)
RETURN (ys+3*i)
 
PROC AddPoint(CHAR ARRAY xstr,ystr)
REAL POINTER p
 
p=GetXPtr(count) ValR(xstr,p)
p=GetYPtr(count) ValR(ystr,p)
count==+1
RETURN
 
PROC InitData()
AddPoint("0.0","2.7")
AddPoint("1.0","2.8")
AddPoint("2.0","31.4")
AddPoint("3.0","38.1")
AddPoint("4.0","58.0")
AddPoint("5.0","76.2")
AddPoint("6.0","100.5")
AddPoint("7.0","130.0")
AddPoint("8.0","149.3")
AddPoint("9.0","180.0")
RETURN
 
INT FUNC GetXPos(Settings POINTER s INT x)
INT res
 
res=x*(s.xRight-s.xLeft)/(s.xMax-s.xMin)+s.xLeft
RETURN (res)
 
INT FUNC GetYPos(Settings POINTER s INT y)
INT res
 
res=y*(s.yTop-s.yBottom)/(s.yMax-s.yMin)+s.yBottom
RETURN (res)
 
INT FUNC GetXPosR(Settings POINTER s REAL POINTER x)
REAL nom,denom,div,tmp
INT res
 
IntToReal(s.xRight-s.xLeft,tmp)
RealMult(tmp,x,nom)
IntToReal(s.xMax-s.xMin,denom)
RealDiv(nom,denom,div)
res=RealToInt(div)+s.xLeft
RETURN (res)
 
INT FUNC GetYPosR(Settings POINTER s REAL POINTER y)
REAL nom,denom,div,tmp
INT res
 
IntToReal(s.yBottom-s.yTop,tmp)
RealMult(tmp,y,nom)
IntToReal(s.yMax-s.yMin,denom)
RealDiv(nom,denom,div)
res=-RealToInt(div)+s.yBottom
RETURN (res)
 
BYTE FUNC AtasciiToInternal(CHAR c)
BYTE c2
 
c2=c&$7F
IF c2<32 THEN
RETURN (c+64)
ELSEIF c2<96 THEN
RETURN (c-32)
FI
RETURN (c)
 
PROC CharOut(INT x,y CHAR c)
BYTE i,j,v
PTR addr
 
addr=$E000+AtasciiToInternal(c)*8;
FOR j=0 TO 7
DO
v=Peek(addr)
i=8
WHILE i>0
DO
IF v&1 THEN
Plot(x+i,y+j)
FI
 
v=v RSH 1
i==-1
OD
addr==+1
OD
RETURN
 
PROC TextOut(INT x,y CHAR ARRAY text)
BYTE i
 
FOR i=1 TO text(0)
DO
CharOut(x,y,text(i))
x==+8
OD
RETURN
 
PROC DrawAxes(Settings POINTER s)
INT i,x,y
CHAR ARRAY t(10)
 
Plot(s.xLeft,s.yTop)
DrawTo(s.xLeft,s.yBottom)
DrawTo(s.xRight,s.yBottom)
 
FOR i=s.xMin TO s.xMax STEP s.xStep
DO
x=GetXPos(s,i)
Plot(x,s.yBottom)
DrawTo(x,s.yBottom+s.tickLength)
StrI(i,t)
TextOut(x-t(0)*4,s.yBottom+s.tickLength+1,t)
OD
 
FOR i=s.yMin TO s.yMax STEP s.yStep
DO
y=GetYPos(s,i)
Plot(s.xLeft-s.tickLength,y)
DrawTo(s.xLeft,y)
StrI(i,t)
TextOut(s.xLeft-s.tickLength-1-t(0)*8,y-4,t)
OD
RETURN
 
PROC DrawPoint(INT x,y)
Plot(x-1,y-1) DrawTo(x+1,y-1)
DrawTo(x+1,y+1) DrawTo(x-1,y+1)
DrawTo(x-1,y-1)
RETURN
 
PROC DrawSeries(Settings POINTER s)
INT i,x,y,prevX,prevY
REAL POINTER p
 
FOR i=0 TO count-1
DO
p=GetXPtr(i) x=GetXPosR(s,p)
p=GetYPtr(i) y=GetYPosR(s,p)
DrawPoint(x,y)
IF i>0 THEN
Plot(prevX,prevY)
DrawTo(x,y)
FI
prevX=x prevY=y
OD
RETURN
 
PROC DrawPlot(Settings POINTER s)
DrawAxes(s)
DrawSeries(s)
RETURN
 
PROC Main()
BYTE CH=$02FC,COLOR1=$02C5,COLOR2=$02C6
Settings s
 
Graphics(8+16)
Color=1
COLOR1=$0C
COLOR2=$02
 
InitData()
s.xMin=0 s.xMax=9 s.xStep=1
s.yMin=0 s.yMax=180 s.yStep=20
s.xLeft=30 s.xRight=311 s.yTop=8 s.yBottom=177
s.tickLength=3
DrawPlot(s)
 
DO UNTIL CH#$FF OD
CH=$FF
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Plot_coordinate_pairs.png Screenshot from Atari 8-bit computer]
 
=={{header|Ada}}==
Line 16 ⟶ 526:
{{libheader|GtkAda}}
[[Image:Gtkada_plot.png|thumb|right|100px|Example GtkAda plot]]
<langsyntaxhighlight lang="ada">
with Gtk.Main;
with Gtk.Window; use Gtk.Window;
Line 63 ⟶ 573:
Gtk.Main.Main;
end PlotCoords;
</syntaxhighlight>
</lang>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68|Revision 1 - extensions to standard used - PRAGMA READ and Currying}}
Line 69 ⟶ 580:
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
[[Image:Plot_coordinate_pairs-Algol68.gif|thumb|right|100px|Example Algol68 plot]]
'''File: Plot_coordinate_pairs.a68'''<langsyntaxhighlight lang="algol68">#!/usr/bin/algol68g-full --script #
# -*- coding: utf-8 -*- #
 
Line 105 ⟶ 616:
);
 
PR READ "postlude/exception.a68" PR</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
Line 111 ⟶ 622:
{{works with|AutoHotkey_L}}(AutoHotkey1.1+)
{{libheader|GDIP}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">#SingleInstance, Force
#NoEnv
SetBatchLines, -1
Line 179 ⟶ 690:
Exit:
Gdip_Shutdown(pToken)
ExitApp</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
[[Image:Coordinate_pair_bbc.gif|right]]
<langsyntaxhighlight lang="bbcbasic"> DIM x(9), y(9)
x() = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
y() = 2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0
Line 212 ⟶ 723:
DRAW 100*x(i%),4*y(i%)
ENDIF
NEXT</langsyntaxhighlight>
 
=={{header|C}}==
Line 219 ⟶ 730:
 
{{libheader|libplot}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Line 335 ⟶ 846:
pl_flushpl();
pl_closepl();
}</langsyntaxhighlight>
 
No one would use the previous code to produce a plot (that looks [http://i40.tinypic.com/f2t0l0.png this way]; instead, normally we produce data through a program, then we plot the data using e.g. [[Plot x, y arrays#gnuplot|gnuplot]] or other powerful tools; the result (with gnuplot and without enhancement) could look [http://i41.tinypic.com/2qivbsn.png like this] instead.
Line 341 ⟶ 852:
===Writing EPS===
[[File:plot-2d-c.png|center]]Following code creates a plot in EPS format, with auto scaling and line/symbol/color controls. Plotting function loosely follows Matlab command style. Not thorough by any means, just to give an idea on how this kind of things can be coded.
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <math.h>
#include <string.h>
Line 471 ⟶ 982:
plot(x, y, N, "r-o");
return 0;
}</langsyntaxhighlight>
 
=={{header|C++}}==
[[File:plot_cpp.png|300px]]
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <string>
Line 699 ⟶ 1,210:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
{{libheader|incanter}}
<langsyntaxhighlight lang="clojure">(use '(incanter core stats charts))
(def x (range 0 10))
(def y '(2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0))
(view (xy-plot x y))
</syntaxhighlight>
</lang>
 
{{Out}}
[http://i.imgur.com/0RQaxNl.png]
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| Boost.Process}}
{{Trans|Go}}
Boost.Process is part of [https://github.com/MaiconSoft/DelphiBoostLib DelphiBoostLib].
<syntaxhighlight lang="delphi">
program Plot_coordinate_pairs;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils,
Boost.Process;
 
var
x: TArray<Integer>;
y: TArray<Double>;
 
begin
x := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
y := [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0];
 
var plot := TPipe.Create('gnuplot -p', True);
plot.WriteA('unset key; plot ''-'''#10);
for var i := 0 to High(x) do
plot.WriteA(format('%d %f'#10, [x[i], y[i]]));
plot.writeA('e'#10);
 
writeln('Press enter to close');
Readln;
plot.Kill;
plot.Free;
readln;
end.</syntaxhighlight>
 
=={{header|EasyLang}}==
[https://easylang.online/show/#cod=hVBBasMwELzrFQO9taBKVhxbh/YjRoeQKK3AlsEWqdzXd1dx2iQt1GCYndndWU3uHF7QQUGjgsEGNbZo0MLCiWVVK9nQ38JouYFppUbdSoVmKytopWQNbRQRemOlgW4ZO/EAse/9bhJ9iP4jHNI7qFUM48nTFGxTBIb1GdmaUPI5zeHTw4hI5r2PyJ0TA2EljuOEQEgjjYgCQDhi6YLDKwYu6eNOpriUQt64azGnhfQBh3CCXfdNu/jm6XrFI5fzajyR9Liy3/fdsKW3otCuSX5AKcmK7OeUyTB30f3jySOWNpU8LpZXJOX1R6u+s8xkuR978rBK3SX/O73zaRQf73vm8bKVpWWNsdBLeR1pP5dl8OvEFw== Run it]
 
<syntaxhighlight>
x[] = [ 0 1 2 3 4 5 6 7 8 9 ]
y[] = [ 2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0 ]
#
clear
linewidth 0.5
move 10 97
line 10 5
line 95 5
textsize 3
n = len x[]
m = 0
for i = 1 to n
if y[i] > m
m = y[i]
.
.
linewidth 0.1
sty = m div 9
for i range0 10
move 10 5 + i * 10
line 95 5 + i * 10
move 2 4 + i * 10
text i * sty
.
stx = x[n] div 9
for i range0 10
move i * 9 + 10 5
line i * 9 + 10 97
move i * 9 + 10 1
text i * stx
.
color 900
linewidth 0.5
for i = 1 to n
x = x[i] * 9 / stx + 10
y = y[i] / sty * 10 + 5
line x y
.
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Resulting image [http://www.echolalie.org/echolisp/images/plot-coordinates.png here].
<langsyntaxhighlight lang="scheme">
(lib 'plot)
 
Line 724 ⟶ 1,313:
(plot-grid 1 20)
(plot-text " Rosetta plot coordinate pairs" 0 10 "white")
</syntaxhighlight>
</lang>
 
=={{header|Erlang}}==
Using [https://github.com/psyeugenic/eplot Eplot] to produce PNG.
 
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( plot_coordinate_pairs ).
 
Line 743 ⟶ 1,331:
PNG = egd_chart:graph( [{File, lists:zip(Xs, Ys)}] ),
file:write_file( File ++ ".png", PNG ).
</syntaxhighlight>
</lang>
 
The result looks like [https://github.com/ebengt/rosettacode/blob/master/graphs/plot_coordinate_pairs.png this].
Line 750 ⟶ 1,338:
Using the [http://www.ffconsultancy.com/products/fsharp_for_visualization/ F# for Visualization] library:
[[Image:FSViz.png|300px|thumb|right|alt text]]
<langsyntaxhighlight lang="fsharp">#r @"C:\Program Files\FlyingFrog\FSharpForVisualization.dll"
 
let x = Seq.map float [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9|]
Line 757 ⟶ 1,345:
open FlyingFrog.Graphics
 
Plot([Data(Seq.zip x y)], (0.0, 9.0))</langsyntaxhighlight>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2019-01-23}}
<syntaxhighlight lang="factor">USING: accessors assocs colors.constants kernel sequences ui
ui.gadgets ui.gadgets.charts ui.gadgets.charts.lines ;
 
chart new { { 0 9 } { 0 180 } } >>axes
line new COLOR: blue >>color
9 <iota> { 2.7 2.8 31.4 38.1 58 76.2 100.5 130 149.3 180 } zip
>>data add-gadget "Coordinate pairs" open-window</syntaxhighlight>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Plot_coordinate_pairs}}
 
'''Solution'''
 
[[File:Fōrmulæ - Plot coordinate pairs 01.png]]
 
[[File:Fōrmulæ - Plot coordinate pairs 02.png]]
 
=={{header|FreeBASIC}}==
{{trans|Liberty BASIC}}
===Text mode===
<syntaxhighlight lang="freebasic">Dim As Integer i, x(9), y(9)
For i = 0 To 9
x(i) = i
Next i
 
y(0) = 2.7
y(1) = 2.8
y(2) = 31.4
y(3) = 38.1
y(4) = 58.0
y(5) = 76.2
y(6) = 100.5
y(7) = 130.0
y(8) = 149.3
y(9) = 180.0
 
Locate 22, 4
For i = 0 To 9
Locate 22, ((i * 4) + 2) : Print i
Next i
 
For i = 0 To 20 Step 2
Locate (21 - i), 0 : Print (i * 10)
Next i
 
Color 14
For i = 0 To 9
Locate (21 - (y(i)/ 10)), (x(i) * 4) + 2 : Print "."
Next i
Sleep</syntaxhighlight>
 
 
=={{header|gnuplot}}==
[[Image:Plotxy-gnuplot.png|thumb|right|200px|Example gnuplot output]]
<langsyntaxhighlight lang="gnuplot">unset key # Only one data set, so the key is uninformative
 
plot '-' # '-' can be replaced with a filename, to read data from that file.
Line 774 ⟶ 1,417:
8 149.3
9 180.0
e</langsyntaxhighlight>
 
<br clear=right>
Line 783 ⟶ 1,426:
 
A program can of course supply commands and data to gnuplot as prepared files. For the spirit of controlling plotting with the native language however, this example shows how commands and data can be prepared programatically and supplied to gnuplot through stdin.
<langsyntaxhighlight lang="go">package main
 
import (
Line 812 ⟶ 1,455:
w.Close()
g.Wait()
}</langsyntaxhighlight>
===gonum/plot===
{{libheader|gonum/plot}}
[[File:GoPoints.png|right|Go plot]]
<langsyntaxhighlight lang="go">package main
 
import (
Line 847 ⟶ 1,490:
log.Fatal(err)
}
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Line 853 ⟶ 1,496:
[[File:GroovyPlotDemo.png|300px|thumb|right|Screenshot of groovy solution]]
 
<langsyntaxhighlight lang="groovy">import groovy.swing.SwingBuilder
import javax.swing.JFrame
import org.jfree.chart.ChartFactory
Line 880 ⟶ 1,523:
widget(chart())
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
gnuplot is a package from [http://hackage.haskell.org/packages/hackage.html HackageDB].
<langsyntaxhighlight lang="haskell">import Graphics.Gnuplot.Simple
 
pnts = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
 
doPlot = plotPathStyle [ ( Title "plotting dots" )]
(PlotStyle Points (CustomStyle [])) (zip [0..] pnts)</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">REAL :: n=10, x(n), y(n)
 
x = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Line 899 ⟶ 1,542:
WINDOW(WINdowhandle=wh, Width=-300, Height=-300, X=1, TItle='Rosetta')
AXIS(WINdowhandle=wh, Title='x values', Yaxis, Title='y values')
LINE(X=x, Y=y, SymbolDiameter=2)</langsyntaxhighlight>
[[File:HicEst_plot_coordinate_pairs.png]]
 
=={{header|Icon}} and {{header|Unicon}}==
[[File:Plotpoints-unicon.gif|thumb|right|Sample Output]]
<langsyntaxhighlight Iconlang="icon">link printf,numbers
 
procedure main()
Line 989 ⟶ 1,632:
every q[i := 1 to *q] := pix - q[i]
return P
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 997 ⟶ 1,640:
=={{header|J}}==
{{libheader|plot}}
<langsyntaxhighlight lang="j">require 'plot'
X=: i.10
Y=: 2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0
'dot; pensize 2.4' plot X;Y</langsyntaxhighlight>
[http://www.jsoftware.com/jwiki/RosettaCode/ExamplePlot1 Output of plot.]
 
Line 1,008 ⟶ 1,651:
=={{header|Java}}==
 
<langsyntaxhighlight Javalang="java"> import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
Line 1,097 ⟶ 1,740:
}
}
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
Using R (non-interactive mode)
Line 1,103 ⟶ 1,747:
 
jq is designed to interoperate with other tools, and in this section we illustrate how jq can be used with R in a simple pipeline: jq will produce a stream of CSV data that will be piped into R operating in non-interactive mode. Assuming the jq and R programs are respectively in plot.jq and plot.R, the pipeline would look like this:
<langsyntaxhighlight lang="sh">jq -n -M -r -f plot.jq | R CMD BATCH plot.R</langsyntaxhighlight>
The above would produce the plot as a .pdf file.
 
'''plot.jq'''
<langsyntaxhighlight lang="jq"># NOTE: This definition of transpose can be omitted
# if your version of jq includes transpose as a builtin.
#
Line 1,126 ⟶ 1,770:
def plot(x;y): "A,B", ( [x,y] | transpose | map( @csv ) | .[]);
 
plot(x;y)</langsyntaxhighlight>
'''plot.R'''
<langsyntaxhighlight Rlang="r">mydata <- read.table( file("stdin"), header=TRUE, sep=",")
 
x = mydata$A # x-axis
Line 1,135 ⟶ 1,779:
main="Scatterplot Example",
xlab="x-axis label", # x-axis label
ylab="y-axis label" ) # y-axis label</langsyntaxhighlight>
 
=={{header|Julia}}==
Using Plots library with PlotlyJS as backend:
 
<langsyntaxhighlight lang="julia">using Plots
plotlyjs()
 
Line 1,147 ⟶ 1,791:
 
p = scatter(x, y)
savefig(p, "/tmp/testplot.png")</langsyntaxhighlight>
 
=={{header|Kotlin}}==
{{libheader|JFreeChart}}
{{trans|Groovy}}
<langsyntaxhighlight lang="scala">// Version 1.2.31
 
import org.jfree.chart.ChartFactory
Line 1,190 ⟶ 1,834:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,196 ⟶ 1,840:
Similar to Groovy entry.
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
1) define X & Y:
 
{def X 0 1 2 3 4 5 7 8 9}
-> X
{def Y 2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0}
-> Y
 
2) define a function returning a sequence of SVG points
 
{def curve
{lambda {:curve :kx :ky}
{S.map {{lambda {:curve :kx :ky :i}
{* :kx {S.get :i {{car :curve}}}}
{* :ky {S.get :i {{cdr :curve}}}} } :curve :kx :ky}
{S.serie 0 {- {S.length {X}} 1}} }}}
 
3) draw a polyline in a SVG context
 
{svg {@ width="580" height="300" style="background:#eee"}
{g {AXES 580 300}
{polyline {@ points="{curve {cons X Y} 30 0.9}"
stroke="#000" fill="transparent" stroke-width="1"}} }}
 
where
{def AXES
{lambda {:w :h}
{@ transform="translate({/ :w 2},{/ :h 2}) scale(1,-1)"}
{line {@ x1="-{/ :w 2}:w" y1="0"
x2="{/ :w 2}" y2="0"
stroke="red" fill="transparent"}}
{line {@ x1="0" y1="-{/ :h 2}"
x2="0" y2="{/ :h 2}"
stroke="green" fill="transparent"}} }}
 
4) the result can be seen in http://lambdaway.free.fr/lambdawalks/?view=plot4
</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
First version writes directly to LB's console window.
<syntaxhighlight lang="lb">
<lang lb>
'Plotting coordinate pairs MainWin - Style
For i = 0 To 9
Line 1,234 ⟶ 1,918:
 
End
</syntaxhighlight>
</lang>
The second version uses the more typical graphic window approach, and is written to enable easy adaptation to other data sets.
<syntaxhighlight lang="lb">
<lang lb>
nomainwin
 
Line 1,300 ⟶ 1,984:
sy = offsetY-y*scaleY 'y is inverted
end function
</syntaxhighlight>
</lang>
[http://www.diga.me.uk/PlotCoordPairs.gif LB screen]
 
=={{header|LiveCode}}==
Displaying the plot with vector graphics
<syntaxhighlight lang="livecode">
on plotGraphic
local tCoordinates
local x = "0, 1, 2, 3, 4, 5, 6, 7, 8, 9"
local y = "2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0"
if there is a graphic "graph" then delete graphic "graph"
repeat with i = 1 to the number of items of x
put item i of x into item 1 of line i of tCoordinates
put item i of y into item 2 of line i of tCoordinates
end repeat
create graphic "graph"
set the style of graphic "graph" to "polygon"
set the points of graphic "graph" to tCoordinates
repeat with i = 1 to the number of lines of tCoordinates
put the top of grc "graph" + the height of grc "graph" - item 2 of line i of tCoordinates into item 2 of line i of tCoordinates
end repeat
set the points of graphic "graph" to tCoordinates
set the height of graphic "graph" to 200
set the width of graphic "graph" to 300
set the loc of grc "graph" to the loc of this card
end plotGraphic
</syntaxhighlight>
[https://www.mediafire.com/view/xhi8oq1fawagzbt Result with Vector Graphics]
 
Displaying the plot with the Line Graph widget
<syntaxhighlight lang="livecode">
on plotLineGraph
local tCoordinates
local x = "0, 1, 2, 3, 4, 5, 6, 7, 8, 9"
local y = "2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0"
if there is a widget "graph" then delete widget "graph"
repeat with i = 1 to the number of items of x
put item i of x into item 1 of line i of tCoordinates
put item i of y into item 2 of line i of tCoordinates
end repeat
create widget "graph" as "com.livecode.widget.linegraph"
set the graphData of widget "graph" to tCoordinates
set the height of widget "graph" to 250
set the width of widget "graph" to 350
set the loc of widget "graph" to the loc of this card
end plotLineGraph
</syntaxhighlight>
[https://www.mediafire.com/view/8wj0l5l5blmd9dg/line_graph.jpg Result with Line Graph Widget]
 
=={{header|Lua}}==
{{libheader|LÖVE}}
 
<syntaxhighlight lang="lua">
w_width = love.graphics.getWidth()
w_height = love.graphics.getHeight()
 
x = {0,1,2,3,4,5,6,7,8,9}
y = {2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0}
origin = {24,24}
points = {}
x_unit = w_width/x[10]/2
y_unit = w_height/10
 
--add points to an array properly formatted for the line function
for i=1,10,1 do
table.insert(points, (x[i]*x_unit) + origin[1])
table.insert(points, (w_height-(y[i]*2)) - origin[2])
end
 
 
function love.draw()
--draw axes and grid
love.graphics.setColor(0, 0.8, 0)
--draw x axis
love.graphics.line(origin[1], w_height-origin[2], w_width, w_height-origin[2])
--draw y axis
love.graphics.line(origin[1], w_height-origin[2], origin[1], origin[2])
--draw grid
for i=1,20,1 do
love.graphics.line(origin[1], (w_height-origin[2])-(i*y_unit), w_width, (w_height-origin[2])-(i*y_unit))
love.graphics.line(origin[1]+(i*x_unit), origin[2], origin[1]+(i*x_unit), w_height-origin[2])
end
--draw line plot
love.graphics.setColor(0.8, 0, 0)
love.graphics.line(points)
--draw labels
love.graphics.setColor(0.8, 0.8, 0.8)
for i=0,9,1 do
--draw x axis labels
love.graphics.print(i, (x_unit*i) + origin[1], love.graphics.getHeight()-origin[2])
--draw y axis labels
love.graphics.print(i*y_unit/2, origin[1], ((love.graphics.getHeight()-i*y_unit)-origin[2]))
end
end
</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Last statements used for copy the console screen to clipboard
 
Result image [https://4.bp.blogspot.com/-8XdIVaW79lU/W_iCUBmj92I/AAAAAAAAHbA/TxMl_P22yckQ1Wdi4zsu6k_QVNRZpqJCgCLcBGAs/s1600/graph222.png here]
 
<syntaxhighlight lang="m2000 interpreter">
Module Pairs {
\\ written in version 9.5 rev. 13
\\ use Gdi+ antialiasing (not work with Wine in Linux, but we get no error)
smooth on
Const center=2, right=3, left=1, blue=1, angle=0, dotline=3
Const size9pt=9, size11pt=11
Cls ,0 ' use current background color, set split screen from line 0
Cursor 0,3
Report center, "Coordinate pairs"
x = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
y = (2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0)
dx=scale.x/2/len(x)
dy=dx 'ratio 1:1
graH=dy*len(x)
Basex=scale.x/4
Basey=(scale.y+graH)/2
Move Basex, Basey
\\ draw use relative coordinates
Draw 0,-graH
\\ Step just move graphic cursor
Step 0, graH
Draw scale.x/2
Step -scale.x/2
\\ scX is 1, not used
max=Y#max()
\\ Auto scale for Y, using 0 for start of axis Y
scY=-graH/((max+5^log(max) ) div 100)/100
\\ make vertical axis using dots with numbers center per dx
j=1
For i=basex+dx to basex+dx*x#max() Step dx
Move i, basey
Step 0, twipsy*10
Legend format$("{0}",array(x,j)), "courier", size9pt, angle, center
Width 1, dotline { draw 0, -graH-twipsy*10,7}
j++
Next i
\\ the same for horizontal axis
HalfTextHeight=Size.y("1","courier", size9pt)/2
For i=basey-dy to basey-dy*x#max() Step dy
Move basex, i
Step -twipsx*10
Width 1, dotline { draw scale.x/2+twipsx*10,,7}
Move basex-100, i+HalfTextHeight
Legend format$("{0}",(i-basey)/scY), "courier", size9pt, angle, left
Next i
ex=each(x) : ey=each(y)
\\ start from first point. We use Draw to for absolute coordinates
Move array(x,0)*dx+Basex, array(y,0)*scy+Basey
While ex, ey {
Width 2 {
Draw to array(ex)*dx+Basex, array(ey)*scy+Basey, blue
}
}
\\ second pass for marks and labels
ex=each(x) : ey=each(y)
While ex, ey {
Move array(ex)*dx+Basex, array(ey)*scy+Basey
Step -75, -75
Pen 12 {draw 150: draw 0,150 : draw -150 : draw 0,-150}
Pen 13 {
Step 200, -200
Legend format$("({0}-{1})",array(ex),array(ey) ), "courier bold", size11pt, angle, right
}
}
\\ screenshot to clipboard
Screenshot$=""
Move 0,0
Copy scale.x, scale.y to Screenshot$
Clipboard Screenshot$
a$=key$
}
Pairs
</syntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">x := Vector([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]):
y := Vector([2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]):
plot(x,y,style="point");</langsyntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">x={0,1,2,3,4,5,6,7,8,9};
y={2.7,2.8,31.4,38.1,58.0,76.2,100.5,130.0,149.3,180.0};
ListPlot[{x, y} // Transpose]</langsyntaxhighlight>
{{out}}
[http://i43.tinypic.com/2a689yw.png]
 
=={{header|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
>> y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0];
>> plot(x,y,'.-')</langsyntaxhighlight>
 
=={{header|Maxima}}==
 
<syntaxhighlight lang="maxima">
<lang maxima>(%i1) ".." (m, n) := makelist (i, i, m, n); infix ("..")$
".." (m, n) := makelist (i, i, m, n); infix ("..")$
(%i2) x: 0 .. 9$ y:[2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]$
x: 0 .. 9$
(%i3) plot2d(['discrete, x, y], [style, [points,5,1,1]], [gnuplot_term, png], [gnuplot_out_file, "qsort-range-10-9.png"])$</lang>
y:[2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]$
wxplot2d(['discrete, x, y], [style, [points,5,1,1]], [gnuplot_term, png], [gnuplot_out_file, "qsort-range-10-9.png"])$
</syntaxhighlight>
[http://img28.picoodle.com/img/img28/4/2/7/f_qsortrange1m_1b7f493.png qsort-range-10-9.png]
 
=={{header|Nim}}==
===Using gnuplot===
{{libheader|gnuplotlib.nim}}
There exists two libraries providing a Nim interface to “gnuplot” (the GNU plotting program), which are named “gnuplot” and “gnuplotlib”. We have chosen the second one as it is be more complete and more convenient to use.
 
The library launches “gnuplot” which does the plotting. From “gnuplot”, it is possible to save the drawing into a PDF, a SVG or an image (BMP, PNG) file.
 
<syntaxhighlight lang="nim">import gnuplot
 
let
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
 
withGnuPlot:
plot(x, y, "Coordinate pairs")
</syntaxhighlight>
 
===Using ggplotnim===
{{libheader|ggplotnim}}
This library doesn’t use an external process to does the plotting. It uses a syntax mostly compliant with “ggplot2” syntax.
<syntaxhighlight lang="nim">import ggplotnim
 
let
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
 
let df = seqsToDf(x, y) # Build a dataframe.
 
df.ggplot(aes("x", "y")) +
ggtitle("Coordinate pairs") +
geomLine() +
themeOpaque() +
ggsave("coordinate_pairs.png")</syntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">#load "graphics.cma"
open Graphics
 
Line 1,348 ⟶ 2,248:
ignore(wait_next_event [Key_pressed]);
close_graph();
;;</langsyntaxhighlight>
 
Using the [http://forge.ocamlcore.org/projects/archimedes/ Archimedes] library,
one can write:
[[Image:Archimedes.png|300px|thumb|right|Archimedes plot (graphics output).]]
<langsyntaxhighlight lang="ocaml">
module A = Archimedes
 
Line 1,364 ⟶ 2,264:
A.Array.y vp y;
A.close vp
</syntaxhighlight>
</lang>
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0];
plot(x,y,"o");
pause;</langsyntaxhighlight>
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
; define input arrays
(define x '(0 1 2 3 4 5 6 7 8 9))
(define y '(2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0))
 
; render it
(import (lib glgl2))
(import (OpenGL version-1-1))
(glOrtho 0 10 0 200 0 1)
 
(gl:set-renderer (lambda (mouse)
(glClear GL_COLOR_BUFFER_BIT)
(glColor3f 0 1 0)
(glBegin GL_LINE_STRIP)
(map (lambdaglVertex2f (x y)
(glEnd)))
(print "x: " x ", y: " y)
</syntaxhighlight>
(glVertex2f x y))
x y)
(glEnd)
#null))
</lang>
 
=={{header|PARI/GP}}==
<syntaxhighlight lang ="parigp">plothraw(vx, vy)</langsyntaxhighlight>
 
=={{header|Perl}}==
 
===GD::Graph library===
 
{{libheader|GD::Graph}}
<langsyntaxhighlight lang="perl">use GD::Graph::points;
 
@data = (
Line 1,406 ⟶ 2,304:
[2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0],
);
$graph = GD::Graph::points->new(400, 300);
$gd = $graph->plot(\@data) or die $graph->error;
 
$graph = GD::Graph::points->new(400, 300);
# Save as image.
open my $fh, '>', "qsort-range-10-9.png";
$format = $graph->export_format;
binmode $fh;
open(OUF, ">qsort-range-10-9.$format");
print $fh $graph->plot(\@data)->png;
binmode OUF;
close $fh;</syntaxhighlight>
print OUF $gd->$format();
close(OUF);</lang>
 
===Imager::Plot library===
{{libheader|Imager}}
{{libheader|Imager::Plot}}
<langsyntaxhighlight lang="perl">use Imager;
use Imager::Plot;
 
Line 1,445 ⟶ 2,340:
$img->box(filled => 1, color => 'white');
$plot->Render(Image => $img, Xoff => 50, Yoff => 350);
$img->write(file => 'qsort-range-10-9.png');</langsyntaxhighlight>
 
=={{header|Perl 6Processing}}==
<syntaxhighlight lang="java">
{{works with|Rakudo|2018.03}}
//Aamrun, 26th June 2022
Generate an SVG image file.
<lang perl6>use SVG;
use SVG::Plot;
 
int x[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
my @x = 0..9;
myfloat @y[] = ({2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0)};
 
size(300,300);
say SVG.serialize: SVG::Plot.new(
surface.setTitle("Rosetta Plot");
width => 512,
height => 512,
x => @x,
x-tick-step => { 1 },
min-y-axis => 0,
values => [@y,],
title => 'Coordinate Pairs',
).plot(:xy-lines);</lang>
[[File:Coordinate-pairs-perl6.svg]]
 
stroke(#ff0000);
=={{header|Phix}}==
{{libheader|pGUI}}
Output same as BBC BASIC
<lang Phix>--
-- demo\rosetta\Plot_coordinate_pairs.exw
--
include pGUI.e
 
for(int i=0;i<x.length;i++){
Ihandle dlg, canvas
ellipse(x[i],y[i],3,3);
cdCanvas cddbuffer, cdcanvas
}
 
</syntaxhighlight>
constant x = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
y = {2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0}
 
=={{header|Phix}}==
function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
{{libheader|Phix/pGUI}}
integer {width, height} = IupGetIntInt(canvas, "DRAWSIZE")
Output same as BBC BASIC, you can run this online [http://phix.x10.mx/p2js/Plot_coordinate_pairs.htm here].
atom cx,cy,nx,ny
<!--<syntaxhighlight lang="phix">(phixonline)-->
atom {w,h} = {(width-60)/9,(height-60)/180}
<span style="color: #000080;font-style:italic;">--
cdCanvasActivate(cddbuffer)
-- demo\rosetta\Plot_coordinate_pairs.exw
cx = 30+x[1]*w
-- ======================================
cy = 30+y[1]*h
--</span>
for i=2 to length(x) do
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
cdCanvasSetForeground(cddbuffer, CD_BLACK)
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
nx = 30+(i-1)*w
<span style="color: #008080;">include</span> <span style="color: #000000;">IupGraph</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
ny = 30+(i-1)*20*h
{} = cdCanvasTextAlignment(cddbuffer, CD_NORTH)
<span style="color: #008080;">constant</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">},</span>
cdCanvasText(cddbuffer, nx, 25, sprintf("%d",(i-1)))
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2.7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2.8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">31.4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">38.1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">58.0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">76.2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">100.5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">130.0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">149.3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">180.0</span><span style="color: #0000FF;">}</span>
{} = cdCanvasTextAlignment(cddbuffer, CD_EAST)
cdCanvasText(cddbuffer, 25, ny, sprintf("%3d",(i-1)*20))
<span style="color: #008080;">function</span> <span style="color: #000000;">get_data</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">graph</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #004600;">CD_BLUE</span><span style="color: #0000FF;">}}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
cdCanvasSetForeground(cddbuffer, CD_GRAY)
cdCanvasLine(cddbuffer,30,ny,width-30,ny)
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
cdCanvasLine(cddbuffer,nx,30,nx,height-30)
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">graph</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">IupGraph</span><span style="color: #0000FF;">(</span><span style="color: #000000;">get_data</span><span style="color: #0000FF;">),</span>
cdCanvasSetForeground(cddbuffer, CD_BLUE)
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Plot coordinate pairs"`</span><span style="color: #0000FF;">)</span>
nx = 30+floor(x[i]*w)
<span style="color: #7060A8;">IupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"RASTERSIZE=320x240,MINSIZE=320x200"</span><span style="color: #0000FF;">)</span>
ny = 30+floor(y[i]*h)
<span style="color: #7060A8;">IupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"XTICK=1,XMIN=0,XMAX=9"</span><span style="color: #0000FF;">)</span>
cdCanvasLine(cddbuffer,cx,cy,nx,ny)
<span style="color: #7060A8;">IupSetAttributes</span><span style="color: #0000FF;">(</span><span style="color: #000000;">graph</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"YTICK=20,YMIN=0,YMAX=180"</span><span style="color: #0000FF;">)</span>
cx = nx
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
cy = ny
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
end for
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
cdCanvasSetForeground(cddbuffer, CD_BLACK)
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
cdCanvasLine(cddbuffer,30,30,width-30,30)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
cdCanvasLine(cddbuffer,30,30,30,height-30)
<!--</syntaxhighlight>-->
cdCanvasFlush(cddbuffer)
return IUP_DEFAULT
end function
 
function map_cb(Ihandle ih)
cdcanvas = cdCreateCanvas(CD_IUP, ih)
cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
cdCanvasSetBackground(cddbuffer, CD_WHITE)
return IUP_DEFAULT
end function
 
function esc_close(Ihandle /*ih*/, atom c)
if c=K_ESC then return IUP_CLOSE end if
return IUP_CONTINUE
end function
 
procedure main()
IupOpen()
 
canvas = IupCanvas(NULL)
IupSetAttribute(canvas, "RASTERSIZE", "340x340") -- initial size
IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
 
dlg = IupDialog(canvas)
IupSetAttribute(dlg, "TITLE", "Plot coordinate pairs")
IupSetCallback(dlg, "K_ANY", Icallback("esc_close"))
IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
 
IupMap(dlg)
IupSetAttribute(canvas, "RASTERSIZE", NULL) -- release the minimum limitation
IupShowXY(dlg,IUP_CENTER,IUP_CENTER)
IupMainLoop()
IupClose()
end procedure
 
main()</lang>
 
=={{header|PicoLisp}}==
[[Image: Plotxy-picoLisp.png|thumb|right|200px|Example picoLisp output]]
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/ps.l")
 
(scl 1)
Line 1,582 ⟶ 2,426:
 
(plot "plot.ps" 300 200 (2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0))
(call 'display "plot.ps")</langsyntaxhighlight>
 
=={{header|PostScript}}==
<syntaxhighlight lang="postscript">
<lang PostScript>
/x [0 1 2 3 4 5 6 7 8 9] def
/y [2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0] def
Line 1,597 ⟶ 2,441:
}repeat
stroke
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Structure PlotData
x.i
y.f
Line 1,690 ⟶ 2,534:
serie_y:
Data.f 2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0
EndDataSection</langsyntaxhighlight>
[[File:PureBasic PlotData.png]]
 
Line 1,700 ⟶ 2,544:
 
Interactive session:
<langsyntaxhighlight lang="python">>>> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
 
Line 1,706 ⟶ 2,550:
>>> pylab.plot(x, y, 'bo')
>>> pylab.savefig('qsort-range-10-9.png')
</syntaxhighlight>
</lang>
See some other examples:
* [http://matplotlib.org/examples/pylab_examples/simple_plot.html simple plot]
Line 1,713 ⟶ 2,557:
 
==={{libheader|VPython}}===
<langsyntaxhighlight lang="python">
from visual import *
from visual.graph import *
Line 1,742 ⟶ 2,586:
label(display=plot1.display, text="Look here",
pos=(6,100.5), xoffset=30,yoffset=-20 )
</syntaxhighlight>
</lang>
 
=={{header|R}}==
R has several different plotting paradigms. First we define the data.
<langsyntaxhighlight Rlang="r">x <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
y <- c(2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0)</langsyntaxhighlight>
===Base graphics===
<syntaxhighlight lang R="r">plot(x,y)</langsyntaxhighlight>
===Lattice/grid graphics===
{{libheader|lattice}}
<langsyntaxhighlight Rlang="r">library(lattice)
xyplot(y~x)</langsyntaxhighlight>
===Grammar of graphics===
{{libheader|ggplot2}}
<langsyntaxhighlight Rlang="r">library(ggplot2)
qplot(x,y)</langsyntaxhighlight>
 
=={{header|Racket}}==
Racket has a built-in plotting library
<langsyntaxhighlight Racketlang="racket">#lang racket
(require plot)
 
Line 1,768 ⟶ 2,612:
 
(plot-new-window? #t)
(plot (points (map vector x y)))</langsyntaxhighlight>
 
This opens a new window with this image (with interactive zooming)
Line 1,774 ⟶ 2,618:
 
And this
<langsyntaxhighlight Racketlang="racket">#lang racket
(require plot)
 
Line 1,781 ⟶ 2,625:
 
(plot-new-window? #t)
(plot (lines (map vector x y)))</langsyntaxhighlight>
 
opens a new window with this image
[[File:LinesRacket.png]]
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2018.03}}
Generate an SVG image file.
<syntaxhighlight lang="raku" line>use SVG;
use SVG::Plot;
 
my @x = 0..9;
my @y = (2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0);
 
say SVG.serialize: SVG::Plot.new(
width => 512,
height => 512,
x => @x,
x-tick-step => { 1 },
min-y-axis => 0,
values => [@y,],
title => 'Coordinate Pairs',
).plot(:lines);</syntaxhighlight>
[[File:Coordinate-pairs-perl6.svg]]
 
=={{header|REXX}}==
See &nbsp; [[Plot coordinate pairs/REXX]] &nbsp; for the <code>'''$PLOT</code>''' program.
===without point labels===
Example usage:
<langsyntaxhighlight lang="rexx">/*REXX program plots X,Y coördinate pairs of numbers with plain (ASCII) characters.*/
x = 0 1 2 3 4 5 6 7 8 9
y = 2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0
Line 1,798 ⟶ 2,663:
end /*j*/ /*$≡ 0,2.7 1,2.8 2,31.4 3,38.1 ··· */
call '$PLOT' $ /*invoke the REXX program: $PLOT */
exit rc /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,851 ⟶ 2,716:
 
===with point labels===
<langsyntaxhighlight lang="rexx">/*REXX program plots X,Y coördinate pairs of numbers with plain (ASCII) characters.*/
x = 0 1 2 3 4 5 6 7 8 9
y = 2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0
Line 1,859 ⟶ 2,724:
end /*j*/ /*$≡ 0,2.7 1,2.8 2,31.4 3,38.1 ··· */
call '$PLOT' $ '(LABELDatapoints' /*invoke the REXX program: $PLOT */
exit rc /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 1,924 ⟶ 2,789:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Plot coordinate pairs
 
Line 2,000 ⟶ 2,865:
label1 { setpicture(p1) show() }
return
</syntaxhighlight>
</lang>
Output:
 
https://www.dropbox.com/s/q6tra0cqoty4pya/Plot.jpg?dl=0
 
=={{header|RPL}}==
{{works with|HP|48G}}
[[File:PlotPairs.png|thumb|alt=HP-48G emulator screenshot|HP-48G emulator screenshot]]
≪ → x y
≪ ERASE <span style="color:grey">@ clear graphics display</span>
x 0 + ≪ MIN ≫ STREAM x ≪ MAX ≫ STREAM XRNG <span style="color:grey">@ set x range</span>
y 0 + ≪ MIN ≫ STREAM y ≪ MAX ≫ STREAM YRNG <span style="color:grey">@ set y range</span>
1 x SIZE '''FOR''' j
x j GET y j GET R→C <span style="color:grey">@ generate coordinates pair </span>
'''IF''' j 1 > '''THEN''' SWAP OVER LINE '''END''' <span style="color:grey">@ draw a line from previous pair</span>
'''NEXT''' DROP
{ (0,0) 10 "x" "y" } AXES DRAX LABEL <span style="color:grey">@ draw axes and labels </span>
{ } PVIEW <span style="color:grey">@ display graphics </span>
≫ ≫ '<span style="color:blue">PLOTXY</span>' STO
 
{0 1 2 3 4 5 6 7 8 9}
{2.7 2.8 31.4 38.1 58.0 76.2 100.5 130.0 149.3 180.0}
<span style="color:blue">PLOTXY</span>
 
=={{header|Ruby}}==
Line 2,010 ⟶ 2,894:
[[File:Ruby.plotxy.png|300px|thumb|right|gnuplot of x,y arrays]]
 
<langsyntaxhighlight lang="ruby">require 'gnuplot'
 
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Line 2,021 ⟶ 2,905:
end
end
end</langsyntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
<langsyntaxhighlight Scalalang="scala">import scala.swing.Swing.pair2Dimension
import scala.swing.{ MainFrame, Panel, Rectangle }
import java.awt.{ Color, Graphics2D, geom }
Line 2,130 ⟶ 3,014:
contents = ui
}
}</langsyntaxhighlight>
 
=={{header|Scilab}}==
<langsyntaxhighlight lang="scilab">--> x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
--> y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0];
--> plot2d(x,y)</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">require('GD::Graph::points')
 
var data = [
Line 2,150 ⟶ 3,034:
 
var format = 'png'
File("qsort-range.#{format}").write(gd.(format), :raw)</langsyntaxhighlight>
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">clear
input x y
0 2.7
Line 2,168 ⟶ 3,052:
 
lines y x
graph export image.png</langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 2,175 ⟶ 3,059:
[[File:Tcl_Plotxy.png|thumb|right|150px|Screenshot for Tcl code]]
This solution does not use existing plotting packages, but constructs the graphics from bare-metal Tk code.
<langsyntaxhighlight Tcllang="tcl">package require Tk
 
# The actual plotting engine
Line 2,230 ⟶ 3,114:
package require Img
set im [image create photo -data .c]
$im write plotxy.png -format PNG</langsyntaxhighlight>
Of course, if we were generating an encapsulated postscript version, we would be able to do that directly.
 
Line 2,238 ⟶ 3,122:
[[File:Plotxy-TI-89.png|thumb|right|200px|TI-89 screenshot]]
 
<langsyntaxhighlight lang="ti89b">FnOff
PlotsOff
NewPlot 1, 1, x, y
ZoomData</langsyntaxhighlight>
 
=={{header|Ursala}}==
Line 2,249 ⟶ 3,133:
Here's the way to do it just as a quick check (all default settings
and dots connected with straight lines).
<langsyntaxhighlight Ursalalang="ursala">#import std
#import flo
#import fit
Line 2,259 ⟶ 3,143:
#output dot'tex' latex_document+ plot
 
main = visualization[curves: <curve[points: ~&p/x y]>]</langsyntaxhighlight>
([http://i25.tinypic.com/33oi74j.jpg output])
 
Line 2,267 ⟶ 3,151:
labeled.
 
<langsyntaxhighlight Ursalalang="ursala">main =
 
visualization[
Line 2,283 ⟶ 3,167:
scattered: true,
points: ~&p/x y,
attributes: {'linecolor': 'black'}]>]</langsyntaxhighlight>
([http://i32.tinypic.com/x1x6cz.jpg output])
 
=={{header|VBA}}==
Using Excel
<syntaxhighlight lang="vb">Private Sub plot_coordinate_pairs(x As Variant, y As Variant)
Dim chrt As Chart
Set chrt = ActiveSheet.Shapes.AddChart.Chart
With chrt
.ChartType = xlLine
.HasLegend = False
.HasTitle = True
.ChartTitle.Text = "Time"
.SeriesCollection.NewSeries
.SeriesCollection.Item(1).XValues = x
.SeriesCollection.Item(1).Values = y
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "microseconds"
End With
End Sub
Public Sub main()
x = [{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}]
y = [{2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0}]
plot_coordinate_pairs x, y
End Sub</syntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
<syntaxhighlight lang="wren">import "graphics" for Canvas, ImageData, Color
import "dome" for Window
import "math" for Point
 
class PlotCoordinates {
construct new(width, height) {
Window.title = "Plot coordinates"
Window.resize(width, height)
Canvas.resize(width, height)
_width = width
_height = height
}
 
init() {
var x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
var y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
plotCoordinates(x, y)
}
 
plotCoordinates(x, y) {
var n = x.count
// draw axes
Canvas.line(40, _height - 40, _width - 40, _height - 40, Color.blue, 2)
Canvas.line(40, _height - 40, 40, 40, Color.blue, 2)
var length = 40 * n
var div = length / 10
var j = 0
for (i in 0..9) {
var p = Point.new(40 + j, _height - 40)
Canvas.print(i.toString, p.x - 4, p.y + 4, Color.white)
j = j + div
}
j = div
for (i in 1..9) {
var p = Point.new(10, _height - 40 - j)
var s = (i * 20).toString
if (s.count == 2) s = " " + s
Canvas.print(s, p.x, p.y, Color.white)
j = j + div
}
Canvas.print("X", _width - 44, _height - 36, Color.green)
Canvas.print("Y", 20, 40, Color.green)
 
// plot points
var xStart = 40
var xScale = 40
var yStart = 40
var yScale = 2
var points = List.filled(n, null)
for (i in 0...n) {
points[i] = Point.new(xStart + x[i]*xScale, _height - yStart - y[i]*yScale)
}
Canvas.circlefill(points[0].x, points[0].y, 3, Color.red)
for (i in 1...n) {
Canvas.line(points[i-1].x, points[i-1].y, points[i].x, points[i].y, Color.red)
Canvas.circlefill(points[i].x, points[i].y, 3, Color.red)
}
}
 
update() {}
 
draw(alpha) {}
}
 
var Game = PlotCoordinates.new(500, 500)</syntaxhighlight>
 
=={{header|XPL0}}==
Line 2,293 ⟶ 3,268:
[[File:PlotXPL0.png|140px|thumb|right|Output]]
 
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations
 
def ScrW=640, ScrH=480, VidMode=$101;
Line 2,326 ⟶ 3,301:
if ChIn(1) then []; \wait for key
SetVid(3); \restore text
]</langsyntaxhighlight>
 
=={{header|Yorick}}==
<langsyntaxhighlight lang="yorick">x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0];
window, 0;
plmk, y, x;
window, 1;
plg, y, x, marks=0;</langsyntaxhighlight>
<gallery>
File:Plotxy-yorick-plmk.png|Output with plmk
Line 2,345 ⟶ 3,320:
 
A program can of course supply commands and data to gnuplot as prepared files. For the spirit of controlling plotting with the native language however, this example shows how commands and data can be prepared programmatically and supplied to gnuplot through stdin.
<langsyntaxhighlight lang="zkl">#<<<
cmd:=0'|
#set term wxt # X11
Line 2,367 ⟶ 3,342:
gnuplot:=System.popen("gnuplot","w");
gnuplot.write(cmd); gnuplot.flush();
ask("Hit return to finish"); gnuplot.close();</langsyntaxhighlight>
2,042

edits