in reply to Re^2: Plot Graph in Console by printing special character say * and spaces using matrix structure in Perl
in thread Plot Graph in Console by printing special character say * and spaces using matrix structure in Perl
Added: I used a 2-D array for the input data points. The actual Plot itself is a 1D array with a string which represents all possible values of the input data (101 discrete points, 0.0-10.0 in 0.1 increments). substr() is used to manipulate individual characters within the line.
You might want to think some more about perhaps some scaling on the "x" axis (the number of output lines could get quite large). Probably just make some plots with your real data and experiment to find new "requirements".
Note I added the "fill" function for you to play with - can make visualizing things easier to see a solid line instead of a single point. Have fun...
UPDATE: You might want to consider adding the data point to the graph, like this for the first point:
(200,3.5) *******************************
etc. Code to do that left as an exercise.
use strict; use warnings; use List::Util qw(min max); # Simple plotting re: node_id=11146802 use constant MAXLINES => 200; #Abort if more than 200 lines in printo +ut # @inData is the input data to plot # A 2-D array of pairs (#iterations, value) # # Valid #iterations are integers. The graph will take # maxiter-miniter+1 lines to display. # The zero is suppressed. # Valid data values are in the range of 0.0-10.0 in 0.1 increments # or rather 101 characters from left of screen to the right # indicies [0..100] my @inData = ([200,3.5],[220,5.8],[210,6.5]); my @col1 = map{$_->[0]}@inData; #just extracts col1 as simple array my $min_iter = min (@col1); #these funcs are faster than a sort my $max_iter = max (@col1); print "minimum iter=$min_iter ; maximum iter= $max_iter\n"; #you decide what you want here... die "Too many lines required! ABORT!" if ($max_iter-$min_iter+1 > MAXL +INES); my @Plot; #initialize Plot Matrix # "burns" some memory at beginning of array to avoid a linear # "offset" adjustment factor # $Plot[$_]=" " x 101 for ($min_iter..$max_iter); foreach my $row_ref (@inData) { my ($n_iter, $data) = @$row_ref; plot_point ($n_iter, $data,1); #can turn FILL OFF if desired } dumpPlot(); ############ sub plot_point { my ($n_iters, $data, $fill) = @_; $fill //= 0; #fill defaults to none my $height = get_height($data); # height range 0 - 100 die "Data Value $data out of range" if ($height > 100); ## you d +ecide what to do substr($Plot[$n_iters],$height,1) = '*'; if ($fill) { substr($Plot[$n_iters],$_,1)= '*' for (0..$height-1); #don't +need a loop but #this w +as easier } } sub get_height #converts for example: 8.4543 to 85 { my $value = shift; my $rounded_value = sprintf("%.1f",$value); #.1 precision #changed +fmt spec to f (duh!) return ($rounded_value * 10); } sub dumpPlot { print "$Plot[$_]\n" for ($min_iter..$max_iter); } __END__ note lines truncated (not full 101 characters). minimum iter=200 ; maximum iter= 220 ******************************* ************************************************************* ***************************************************
|
|---|