Plot coordinate pairs: Difference between revisions
Content deleted Content added
(32 intermediate revisions by 18 users not shown) | |||
Line 2:
;Task:
Plot a function represented as <big><big>
Post the resulting image for the following input arrays (taken from [[Time_a_function#Python|Python's Example section on ''Time a function'']]):
This task is intended as a subtask for [[Measure relative performance of sorting algorithms implementations]].
Line 13:
=={{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 */
Line 295:
.include "../includeARM64.inc"
</syntaxhighlight>
{{Output}}
<pre>
Line 323:
</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 329 ⟶ 526:
{{libheader|GtkAda}}
[[Image:Gtkada_plot.png|thumb|right|100px|Example GtkAda plot]]
<
with Gtk.Main;
with Gtk.Window; use Gtk.Window;
Line 376 ⟶ 573:
Gtk.Main.Main;
end PlotCoords;
</syntaxhighlight>
=={{header|ALGOL 68}}==
Line 383 ⟶ 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'''<
# -*- coding: utf-8 -*- #
Line 419 ⟶ 616:
);
PR READ "postlude/exception.a68" PR</
=={{header|AutoHotkey}}==
Line 425 ⟶ 622:
{{works with|AutoHotkey_L}}(AutoHotkey1.1+)
{{libheader|GDIP}}
<
#NoEnv
SetBatchLines, -1
Line 493 ⟶ 690:
Exit:
Gdip_Shutdown(pToken)
ExitApp</
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
[[Image:Coordinate_pair_bbc.gif|right]]
<
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 526 ⟶ 723:
DRAW 100*x(i%),4*y(i%)
ENDIF
NEXT</
=={{header|C}}==
Line 533 ⟶ 730:
{{libheader|libplot}}
<
#include <stdlib.h>
#include <math.h>
Line 649 ⟶ 846:
pl_flushpl();
pl_closepl();
}</
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 655 ⟶ 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.
<
#include <math.h>
#include <string.h>
Line 785 ⟶ 982:
plot(x, y, N, "r-o");
return 0;
}</
=={{header|C++}}==
[[File:plot_cpp.png|300px]]
<
#include <windows.h>
#include <string>
Line 1,013 ⟶ 1,210:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
=={{header|Clojure}}==
{{libheader|incanter}}
<
(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>
{{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
line 10
line 95
textsize 3
n = len x[]
m = 0
for i
if y[i] > m
m = y[i]
.
.
linewidth 0.1
sty = m div 9
for i
move
move
.
stx = x[n
for i
move i * 9 + 10
move i * 9 + 10
.
color 900
linewidth 0.5
for i
x = x[i] * 9 / stx + 10
y =
.
</syntaxhighlight>
=={{header|EchoLisp}}==
Resulting image [http://www.echolalie.org/echolisp/images/plot-coordinates.png here].
<
(lib 'plot)
Line 1,083 ⟶ 1,313:
(plot-grid 1 20)
(plot-text " Rosetta plot coordinate pairs" 0 10 "white")
</syntaxhighlight>
=={{header|Erlang}}==
Using [https://github.com/psyeugenic/eplot Eplot] to produce PNG.
<syntaxhighlight lang="erlang">
-module( plot_coordinate_pairs ).
Line 1,101 ⟶ 1,331:
PNG = egd_chart:graph( [{File, lists:zip(Xs, Ys)}] ),
file:write_file( File ++ ".png", PNG ).
</syntaxhighlight>
The result looks like [https://github.com/ebengt/rosettacode/blob/master/graphs/plot_coordinate_pairs.png this].
Line 1,108 ⟶ 1,338:
Using the [http://www.ffconsultancy.com/products/fsharp_for_visualization/ F# for Visualization] library:
[[Image:FSViz.png|300px|thumb|right|alt text]]
<
let x = Seq.map float [|0; 1; 2; 3; 4; 5; 6; 7; 8; 9|]
Line 1,115 ⟶ 1,345:
open FlyingFrog.Graphics
Plot([Data(Seq.zip x y)], (0.0, 9.0))</
=={{header|Factor}}==
{{works with|Factor|0.99 2019-01-23}}
<
ui.gadgets ui.gadgets.charts ui.gadgets.charts.lines ;
Line 1,125 ⟶ 1,355:
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</
=={{header|Fōrmulæ}}==
'''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]]
<
plot '-' # '-' can be replaced with a filename, to read data from that file.
Line 1,150 ⟶ 1,417:
8 149.3
9 180.0
e</
<br clear=right>
Line 1,159 ⟶ 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.
<
import (
Line 1,188 ⟶ 1,455:
w.Close()
g.Wait()
}</
===gonum/plot===
{{libheader|gonum/plot}}
[[File:GoPoints.png|right|Go plot]]
<
import (
Line 1,223 ⟶ 1,490:
log.Fatal(err)
}
}</
=={{header|Groovy}}==
Line 1,229 ⟶ 1,496:
[[File:GroovyPlotDemo.png|300px|thumb|right|Screenshot of groovy solution]]
<
import javax.swing.JFrame
import org.jfree.chart.ChartFactory
Line 1,256 ⟶ 1,523:
widget(chart())
}
}</
=={{header|Haskell}}==
gnuplot is a package from [http://hackage.haskell.org/packages/hackage.html HackageDB].
<
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)</
=={{header|HicEst}}==
<
x = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Line 1,275 ⟶ 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)</
[[File:HicEst_plot_coordinate_pairs.png]]
=={{header|Icon}} and {{header|Unicon}}==
[[File:Plotpoints-unicon.gif|thumb|right|Sample Output]]
<
procedure main()
Line 1,365 ⟶ 1,632:
every q[i := 1 to *q] := pix - q[i]
return P
end</
{{libheader|Icon Programming Library}}
Line 1,373 ⟶ 1,640:
=={{header|J}}==
{{libheader|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</
[http://www.jsoftware.com/jwiki/RosettaCode/ExamplePlot1 Output of plot.]
Line 1,384 ⟶ 1,651:
=={{header|Java}}==
<
import java.awt.event.*;
import java.awt.geom.*;
Line 1,473 ⟶ 1,740:
}
}
</syntaxhighlight>
=={{header|jq}}==
Line 1,480 ⟶ 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:
<
The above would produce the plot as a .pdf file.
'''plot.jq'''
<
# if your version of jq includes transpose as a builtin.
#
Line 1,503 ⟶ 1,770:
def plot(x;y): "A,B", ( [x,y] | transpose | map( @csv ) | .[]);
plot(x;y)</
'''plot.R'''
<
x = mydata$A # x-axis
Line 1,512 ⟶ 1,779:
main="Scatterplot Example",
xlab="x-axis label", # x-axis label
ylab="y-axis label" ) # y-axis label</
=={{header|Julia}}==
Using Plots library with PlotlyJS as backend:
<
plotlyjs()
Line 1,524 ⟶ 1,791:
p = scatter(x, y)
savefig(p, "/tmp/testplot.png")</
=={{header|Kotlin}}==
{{libheader|JFreeChart}}
{{trans|Groovy}}
<
import org.jfree.chart.ChartFactory
Line 1,567 ⟶ 1,834:
}
}
}</
{{out}}
Line 1,573 ⟶ 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">
'Plotting coordinate pairs MainWin - Style
For i = 0 To 9
Line 1,611 ⟶ 1,918:
End
</syntaxhighlight>
The second version uses the more typical graphic window approach, and is written to enable easy adaptation to other data sets.
<syntaxhighlight lang="lb">
nomainwin
Line 1,677 ⟶ 1,984:
sy = offsetY-y*scaleY 'y is inverted
end function
</syntaxhighlight>
[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}}
<
w_width = love.graphics.getWidth()
w_height = love.graphics.getHeight()
Line 1,729 ⟶ 2,084:
end
</syntaxhighlight>
=={{header|M2000 Interpreter}}==
Line 1,736 ⟶ 2,091:
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
Line 1,809 ⟶ 2,164:
}
Pairs
</syntaxhighlight>
=={{header|Maple}}==
<
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");</
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
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]</
{{out}}
[http://i43.tinypic.com/2a689yw.png]
=={{header|MATLAB}}==
<
>> y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0];
>> plot(x,y,'.-')</
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
".." (m, n) := makelist (i, i, m, n); infix ("..")$
x: 0 .. 9$
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}}==
<
open Graphics
Line 1,856 ⟶ 2,248:
ignore(wait_next_event [Key_pressed]);
close_graph();
;;</
Using the [http://forge.ocamlcore.org/projects/archimedes/ Archimedes] library,
one can write:
[[Image:Archimedes.png|300px|thumb|right|Archimedes plot (graphics output).]]
<
module A = Archimedes
Line 1,872 ⟶ 2,264:
A.Array.y vp y;
A.close vp
</syntaxhighlight>
=={{header|Octave}}==
<
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;</
=={{header|Ol}}==
<
; define input arrays
(define x '(0 1 2 3 4 5 6 7 8 9))
Line 1,896 ⟶ 2,288:
(map glVertex2f x y)
(glEnd)))
</syntaxhighlight>
=={{header|PARI/GP}}==
<syntaxhighlight lang
=={{header|Perl}}==
===GD::Graph library===
{{libheader|GD::Graph}}
<
@data = (
Line 1,909 ⟶ 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);
open my $fh, '>', "qsort-range-10-9.png";
binmode $fh;
print $fh $
close $fh;</syntaxhighlight>
===Imager::Plot library===
{{libheader|Imager::Plot}}
<
use Imager::Plot;
Line 1,947 ⟶ 2,340:
$img->box(filled => 1, color => 'white');
$plot->Render(Image => $img, Xoff => 50, Yoff => 350);
$img->write(file => 'qsort-range-10-9.png');</
=={{header|
<syntaxhighlight lang="java">
//Aamrun, 26th June 2022
int x[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
float y[] = {2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0};
size(300,300);
surface.setTitle("Rosetta Plot");
stroke(#ff0000);
for(int i=0;i<x.length;i++){
ellipse(x[i],y[i],3,3);
}
</syntaxhighlight>
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
Output same as BBC BASIC, you can run this online [http://phix.x10.mx/p2js/Plot_coordinate_pairs.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Plot_coordinate_pairs.exw
-- ======================================
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">IupGraph</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<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>
<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>
<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>
<span style="color: #7060A8;">IupOpen</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: #000000;">IupGraph</span><span style="color: #0000FF;">(</span><span style="color: #000000;">get_data</span><span style="color: #0000FF;">),</span>
<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>
<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>
<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>
<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>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<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>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
=={{header|PicoLisp}}==
[[Image: Plotxy-picoLisp.png|thumb|right|200px|Example picoLisp output]]
<
(scl 1)
Line 2,057 ⟶ 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")</
=={{header|PostScript}}==
<syntaxhighlight 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 2,072 ⟶ 2,441:
}repeat
stroke
</syntaxhighlight>
=={{header|PureBasic}}==
<
x.i
y.f
Line 2,165 ⟶ 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</
[[File:PureBasic PlotData.png]]
Line 2,175 ⟶ 2,544:
Interactive session:
<
>>> y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0]
Line 2,181 ⟶ 2,550:
>>> pylab.plot(x, y, 'bo')
>>> pylab.savefig('qsort-range-10-9.png')
</syntaxhighlight>
See some other examples:
* [http://matplotlib.org/examples/pylab_examples/simple_plot.html simple plot]
Line 2,188 ⟶ 2,557:
==={{libheader|VPython}}===
<
from visual import *
from visual.graph import *
Line 2,217 ⟶ 2,586:
label(display=plot1.display, text="Look here",
pos=(6,100.5), xoffset=30,yoffset=-20 )
</syntaxhighlight>
=={{header|R}}==
R has several different plotting paradigms. First we define the data.
<
y <- c(2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0)</
===Base graphics===
<syntaxhighlight lang
===Lattice/grid graphics===
{{libheader|lattice}}
<
xyplot(y~x)</
===Grammar of graphics===
{{libheader|ggplot2}}
<
qplot(x,y)</
=={{header|Racket}}==
Racket has a built-in plotting library
<
(require plot)
Line 2,243 ⟶ 2,612:
(plot-new-window? #t)
(plot (points (map vector x y)))</
This opens a new window with this image (with interactive zooming)
Line 2,249 ⟶ 2,618:
And this
<
(require plot)
Line 2,256 ⟶ 2,625:
(plot-new-window? #t)
(plot (lines (map vector x y)))</
opens a new window with this image
Line 2,265 ⟶ 2,634:
{{works with|Rakudo|2018.03}}
Generate an SVG image file.
<syntaxhighlight lang="raku"
use SVG::Plot;
Line 2,279 ⟶ 2,648:
values => [@y,],
title => 'Coordinate Pairs',
).plot(:lines);</
[[File:Coordinate-pairs-perl6.svg]]
Line 2,286 ⟶ 2,655:
===without point labels===
Example usage:
<
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 2,294 ⟶ 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. */</
{{out|output|text= when using the default input:}}
<pre>
Line 2,347 ⟶ 2,716:
===with point labels===
<
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 2,355 ⟶ 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. */</
{{out|output|text= when using the default input:}}
<pre>
Line 2,420 ⟶ 2,789:
=={{header|Ring}}==
<
# Project : Plot coordinate pairs
Line 2,496 ⟶ 2,865:
label1 { setpicture(p1) show() }
return
</syntaxhighlight>
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,506 ⟶ 2,894:
[[File:Ruby.plotxy.png|300px|thumb|right|gnuplot of x,y arrays]]
<
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Line 2,517 ⟶ 2,905:
end
end
end</
=={{header|Scala}}==
{{libheader|Scala}}
<
import scala.swing.{ MainFrame, Panel, Rectangle }
import java.awt.{ Color, Graphics2D, geom }
Line 2,626 ⟶ 3,014:
contents = ui
}
}</
=={{header|Scilab}}==
<
--> y = [2.7, 2.8, 31.4, 38.1, 58.0, 76.2, 100.5, 130.0, 149.3, 180.0];
--> plot2d(x,y)</
=={{header|Sidef}}==
{{trans|Perl}}
<
var data = [
Line 2,646 ⟶ 3,034:
var format = 'png'
File("qsort-range.#{format}").write(gd.(format), :raw)</
=={{header|Stata}}==
<
input x y
0 2.7
Line 2,664 ⟶ 3,052:
lines y x
graph export image.png</
=={{header|Tcl}}==
Line 2,671 ⟶ 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.
<
# The actual plotting engine
Line 2,726 ⟶ 3,114:
package require Img
set im [image create photo -data .c]
$im write plotxy.png -format PNG</
Of course, if we were generating an encapsulated postscript version, we would be able to do that directly.
Line 2,734 ⟶ 3,122:
[[File:Plotxy-TI-89.png|thumb|right|200px|TI-89 screenshot]]
<
PlotsOff
NewPlot 1, 1, x, y
ZoomData</
=={{header|Ursala}}==
Line 2,745 ⟶ 3,133:
Here's the way to do it just as a quick check (all default settings
and dots connected with straight lines).
<
#import flo
#import fit
Line 2,755 ⟶ 3,143:
#output dot'tex' latex_document+ plot
main = visualization[curves: <curve[points: ~&p/x y]>]</
([http://i25.tinypic.com/33oi74j.jpg output])
Line 2,763 ⟶ 3,151:
labeled.
<
visualization[
Line 2,779 ⟶ 3,167:
scattered: true,
points: ~&p/x y,
attributes: {'linecolor': 'black'}]>]</
([http://i32.tinypic.com/x1x6cz.jpg output])
=={{header|VBA}}==
Using Excel
<
Dim chrt As Chart
Set chrt = ActiveSheet.Shapes.AddChart.Chart
Line 2,803 ⟶ 3,191:
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</
=={{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,812 ⟶ 3,268:
[[File:PlotXPL0.png|140px|thumb|right|Output]]
<
def ScrW=640, ScrH=480, VidMode=$101;
Line 2,845 ⟶ 3,301:
if ChIn(1) then []; \wait for key
SetVid(3); \restore text
]</
=={{header|Yorick}}==
<
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;</
<gallery>
File:Plotxy-yorick-plmk.png|Output with plmk
Line 2,864 ⟶ 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.
<
cmd:=0'|
#set term wxt # X11
Line 2,886 ⟶ 3,342:
gnuplot:=System.popen("gnuplot","w");
gnuplot.write(cmd); gnuplot.flush();
ask("Hit return to finish"); gnuplot.close();</
|