Perl Scatter Plot example script from the perl graph gallery. Plotly makes free online charts with Perl and PDL and a GUI. Graph of a Sine Wave Perl Scatter Plot | Example Code | plotly

Fishschool Scatter Graph

Made with Perl graphing API

Back to the API Gallery

Perl Example Code


#!/usr/bin/perl

####################################
# Line and Scatter Plot Demo 
# Questions? Email feedback@plot.ly
# For more docs, see plot.ly/api
####################################

use strict;

use constant PI => 4 * atan2(1, 1);

use WebService::Plotly;
use Math::Random;
use List::MoreUtils qw(pairwise);


my $plotly = WebService::Plotly->new( un => 'username', key => 'api_key' );

my $x = [ 0 .. 1499 ];

my @random_dist = random_uniform( 1500, 0, 1 );

my @y1 = pairwise { sin( 2 * PI * $a / 1500 ) + $b - 0.5 } @$x, @random_dist;
my @y2 = map { sin( 2 * PI * $_ / 1500 ) } @$x;

my $fish = {
	x      => $x,
	y      => [@y1],
	type   => 'scatter',
	mode   => 'markers',
	marker => {
		color => 'rgb(0, 0, 255)',
		opacity => 0.5, 
	}, 
};

my $fit = {
	x       => $x,
	y       => [@y2],
	type    => 'scatter',
	mode    => 'markers',
	opacity => 0.8, 
	marker  => {
		color => 'rgb(255, 0, 0)',
	},
};

my $l = {
	autosize => 0,
	width    => 650, 
	height   => 550,
	title    => 'Fish School',
	xaxis => { 
		ticks         => '', 
		gridcolor     => 'white', 
		zerolinecolor => 'white', 
		linecolor     => 'white', 
		autorange     => 0, 
		range         => [0,1500],
	},
	yaxis => { 
		ticks         => '', 
		gridcolor     => 'white', 
		zerolinecolor => 'white', 
		linecolor     => 'white', 
		autorange     => 0, 
		range         => [-2.2,2.2] ,
	},
	plot_bgcolor => 'rgb(245,245,247)', 
	showlegend   => 0, 
	hovermode    => 'closest',
};



my $response = $plotly->plot( [ $fish, $fit ], layout => $l );

print "url is:\n\t$response->{url}\n\n";
print "filename on our server is: \n\t$response->{filename}\n";