Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Plot Graph in Console by printing special character say * and spaces using matrix structure in Perl

by programmingzeal (Beadle)
on Sep 10, 2022 at 10:24 UTC ( [id://11146815]=note: print w/replies, xml ) Need Help??


in reply to Re: 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

Also the error values are float values of 1 decimal place, i.e., Y(X) ranging from 0.0 till say 10.0 with 0.1 increment. But Number of iterations can be fairly large like 500 till 2000 etc. Sorry about not elaborating my requirements clearly. I think we should not change origin from top left. Let it be there. Y-axis should be a horizontal line from left to right with calibration 0.0, 0.1, 0.2, 0.3 till say 10.0. While the X-axis would be from top left to bottom left with calibration from 0, 1, 2, 3, 4, 5, 6, 7, and so on till say 1000 with increment of 1. That is the ASCII Art graph should be rotated 90 degrees clockwise from its current form. Am I clear this time ? Sorry Again!!!

For better visibility on console I need Histogram. You are right. A graph is just a plotting of x-y coordinates. In my case I have only first quadrant but I need a Histogram (For better visibility.)

  • Comment on Re^2: Plot Graph in Console by printing special character say * and spaces using matrix structure in Perl

Replies are listed 'Best First'.
Re^3: Plot Graph in Console by printing special character say * and spaces using matrix structure in Perl
by Marshall (Canon) on Sep 10, 2022 at 17:54 UTC
    So, I think that I understand your requirements - see below. The graph is turned 90 deg to the right from what would be "normal". The width on the page is fixed at 101 characters so we don't have to worry about screen size - I think that is fine even with Windows defaults. I didn't worry about minimizing Plot memory - probably makes no difference at all.

    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 ******************************* ************************************************************* ***************************************************

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146815]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2025-11-11 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (67 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.