plotpipe: --log axis.

This commit is contained in:
Ole Tange 2021-03-03 22:18:06 +01:00
parent 3b0b378249
commit 112ed47440

View file

@ -9,12 +9,12 @@ plotpipe - Plot CSV data from a pipe
=head1 SYNOPSIS =head1 SYNOPSIS
I<datagenerator> | B<plotpipe> [-n] [-H] [-0] [--logx] [--logy] [-C str] [-h] [-V] I<datagenerator> | B<plotpipe> [-n] [-H] [-0] [--log axis] [-C str] [-h] [-V]
=head1 DESCRIPTION =head1 DESCRIPTION
B<plotpipe> is a simple wrapper for Gnuplot to simply plot data. B<plotpipe> is a simple wrapper for Gnuplot to simply plotting data.
The input is a CSV-file. Lines starting with '#' will be used as The input is a CSV-file. Lines starting with '#' will be used as
titles on the plot. titles on the plot.
@ -48,11 +48,11 @@ B<--header>.
Show help. Show help.
=item B<--logx> =item B<--log x>
=item B<--logy> =item B<--log y>
=item B<--logxy> =item B<--log xy>
Logarithmic X/Y/X&Y axis. Logarithmic X/Y/X&Y axis.
@ -139,7 +139,18 @@ input.csv:
4 9 16 4 9 16
8 27 64 8 27 64
cat input.csv | plotpipe --nox --logy cat input.csv | plotpipe --nox --log y
=head1 EXAMPLE: XY-line plots
You are not limited to a simple graph, but can also do XY-line plots.
seq 0 0.001 6.29 |
perl -nE 'say sin($_*100)*0.3+0.5*cos($_*2),",",
sin($_*2)-cos($_*100)*0.3,",",
sin($_)+cos($_*99),",",
sin($_*3)-cos($_*101)' |
plotpipe
=head1 LIMITS =head1 LIMITS
@ -149,7 +160,7 @@ B<plotpipe> is limited by Gnuplot.
=head1 AUTHOR =head1 AUTHOR
Copyright (C) 2019-2020 Ole Tange, Copyright (C) 2019-2021 Ole Tange,
http://ole.tange.dk and Free Software Foundation, Inc. http://ole.tange.dk and Free Software Foundation, Inc.
@ -173,12 +184,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
=head1 DEPENDENCIES =head1 DEPENDENCIES
B<pipeplot> uses B<gnuplot>. B<pipeplot> uses B<gnuplot> and B<perl>.
=head1 SEE ALSO =head1 SEE ALSO
B<perl>, B<gnuplot> B<gnuplot>, B<perl>
=cut =cut
@ -193,9 +204,7 @@ sub options_hash() {
"version|V" => \$opt::version, "version|V" => \$opt::version,
"colsep|col-sep|C=s" => \$opt::colsep, "colsep|col-sep|C=s" => \$opt::colsep,
"help|h" => \$opt::help, "help|h" => \$opt::help,
"logx" => \$opt::logx, "log=s" => \$opt::log,
"logy" => \$opt::logy,
"logxy" => \$opt::logxy,
"null|0" => \$opt::null, "null|0" => \$opt::null,
"nox|n" => \$opt::nox, "nox|n" => \$opt::nox,
"header|H" => \$opt::header, "header|H" => \$opt::header,
@ -208,7 +217,7 @@ sub version() {
print join print join
("\n", ("\n",
"$Global::progname $Global::version", "$Global::progname $Global::version",
"Copyright (C) 2020 Ole Tange, http://ole.tange.dk and Free Software", "Copyright (C) 2020-2021 Ole Tange, http://ole.tange.dk and Free Software",
"Foundation, Inc.", "Foundation, Inc.",
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>", "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>",
"This is free software: you are free to change and redistribute it.", "This is free software: you are free to change and redistribute it.",
@ -219,6 +228,19 @@ sub version() {
); );
} }
sub status(@) {
my @w = @_;
my $fh = $Global::status_fd || *STDERR;
print $fh map { ($_, "\n") } @w;
flush $fh;
}
sub error(@) {
my @w = @_;
my $prog = $Global::progname || "plotpipe";
status(map { ($prog.": Error: ". $_); } @w);
}
sub help() { sub help() {
# Returns: N/A # Returns: N/A
print join print join
@ -231,7 +253,7 @@ sub help() {
'-0 Records separated by \0 instead of \n', '-0 Records separated by \0 instead of \n',
'-C str Columns separator', '-C str Columns separator',
'-V Show version', '-V Show version',
'--logx/logy/logxy Log x/y/x&y', '--log A Log axis A (x y xy)',
'-n No X value', '-n No X value',
'-s num Smooth num Y-values', '-s num Smooth num Y-values',
"", "",
@ -341,9 +363,12 @@ sub find_sep(@) {
Getopt::Long::Configure("bundling","require_order"); Getopt::Long::Configure("bundling","require_order");
my $retval = GetOptions(options_hash()); if(not GetOptions(options_hash())) {
exit(255);
}
$Global::progname = "plotpipe"; $Global::progname = "plotpipe";
$Global::version = 20201222; $Global::version = 20210222;
if($opt::version) { version(); exit 0; } if($opt::version) { version(); exit 0; }
if($opt::help) { help(); exit 0; } if($opt::help) { help(); exit 0; }
if($opt::null) { $/ = "\0"; } if($opt::null) { $/ = "\0"; }
@ -409,8 +434,7 @@ if($ncols >= 2 and not $opt::nox) {
if($opt::smooth) { if($opt::smooth) {
my (@sum,@new); my (@sum,@new);
if($#tbl < $opt::smooth) { if($#tbl < $opt::smooth) {
print STDERR "plotpipe: --smooth must be lower than the ", error("--smooth must be lower than the number of rows (".(1+$#tbl).")");
"number of rows (",1+$#tbl,")\n";
exit(255); exit(255);
} }
my $smooth = $opt::smooth-1; my $smooth = $opt::smooth-1;
@ -445,16 +469,23 @@ for(my $col = 2; $col <= $ncols; $col++) {
if($opt::header) { if($opt::header) {
$legend = qq( title "$header[$col-1]"); $legend = qq( title "$header[$col-1]");
} }
push @plotscript, qq("$filename" using 1:$col with lines $legend,); push @legend, qq("$filename" using 1:$col with lines $legend,);
} }
# Add --logx/--logy/--logxy to Gnuplot script # Add --log axis to Gnuplot script
my @logscale; my @logscale;
if($opt::logx or $opt::logxy) { if($opt::log) {
push @logscale, "set logscale x 10;"; if($opt::log eq "x") {
} push @logscale, "set logscale x 10;";
if($opt::logy or $opt::logxy) { } elsif($opt::log eq "y") {
push @logscale, "set logscale y 10;"; push @logscale, "set logscale y 10;";
} elsif($opt::log eq "xy" or $opt::log eq "yx") {
push @logscale, "set logscale x 10;";
push @logscale, "set logscale y 10;";
} else {
error("--log $opt::log is not supported. Only x y xy are supported");
exit(255);
}
} }
# Make full Gnuplot script # Make full Gnuplot script
@ -467,7 +498,7 @@ set xlabel "$header[0]";
set grid; set grid;
set key right center; set key right center;
set datafile separator "\001"; set datafile separator "\001";
plot @plotscript plot @legend
_EOS _EOS
open GNUPLOT,"|-", "gnuplot -p -e ".Q($plotscript) or die; open GNUPLOT,"|-", "gnuplot -p -e ".Q($plotscript) or die;