Skip to content

Commit

Permalink
fix ASCII plots to work in multiplot with Gnuplot 6+
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Jul 31, 2024
1 parent 97151cb commit ac3f7f2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
- add "resample" curve option for with=>'fits'
- with=>'fits' error if no FITS header
- fix ASCII plots to work in multiplot with Gnuplot 6+

2.026 2024-04-20
- fix {colorbox=>1} (#100)
Expand Down
30 changes: 16 additions & 14 deletions lib/PDL/Graphics/Gnuplot.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3128,7 +3128,7 @@ POS
$chunks->[$i]{testdata} = $testdataunit_binary x ($chunks->[$i]{tuplesize}) if $check_syntax;
} else {
# ASCII transfer has been specified - plot command is easier, but the data are in ASCII.
$pchunk = " '-' ".$plotcmds[$i];
$pchunk = " \$PGG_data_$i ".$plotcmds[$i];
$tchunk = $pchunk if $check_syntax;
$chunks->[$i]{testdata} = " 1 " x ($chunks->[$i]{tuplesize}) . "\ne\n" if $check_syntax;
}
Expand Down Expand Up @@ -3160,12 +3160,13 @@ POS
##############################
##### Send the PlotOptionsString
my @plot_chunks = [$plotOptionsString];
my $plotshow = $plotOptionsString;
my @plotshow_chunks = $plotOptionsString;
##############################
##### Finally..... send the actual plot command to the gnuplot device.
print "plot command is:\n$plotcmd\n" if $PDL::Graphics::Gnuplot::DEBUG;
push @plot_chunks, [$plotcmd];
$plotshow .= $plotcmd;
push @plotshow_chunks, $plotcmd;
my $chunk_i = 0;
for my $chunk (@$chunks) {
# Gnuplot doesn't handle bad values, but it *does* know to
# omit nans. If we're running under a PDL that uses the
Expand All @@ -3184,22 +3185,22 @@ POS
# Currently all images are sent binary
my $p = $chunk->{data}[0]->double->sever;
push @plot_chunks, [${$p->get_dataref}, {binary => 1, data => 1 }];
$plotshow .= " [ ".length(${$p->get_dataref})." bytes of binary image data ]\n";
push @plotshow_chunks, " [ ".length(${$p->get_dataref})." bytes of binary image data ]\n";
} elsif ($chunk->{binaryCurveFlag}) {
# Send in binary if the binary flag is set.
my $p = pdl(@{$chunk->{data}})->mv(-1,0)->double->sever;
push @plot_chunks, [${$p->get_dataref}, {binary => 1, data => 1 }];
$plotshow .= " [ ".length(${$p->get_dataref})." bytes of binary data ]\n";
push @plotshow_chunks, " [ ".length(${$p->get_dataref})." bytes of binary data ]\n";
} else {
# Assemble and dump the ASCII
if ($chunk->{data}[0]->$_isa('PDL')) {
# It's a collection of PDL data only.
my $p = pdl(@{$chunk->{data}})->slice(":,:"); # ensure at least 2 dims
$p = $p->mv(-1,0); # tuple dim first, rows second
$plotshow .= " [ ".$p->dim(1)." lines of ASCII data ]\n";
splice @plotshow_chunks, 1, 0, "\$PGG_data_$chunk_i << [ ".$p->dim(1)." lines of ASCII data ]\n";
# Create a set of ASCII lines. If any of the elements of a given row are NaN or BAD, blank that line.
my $outbuf = join("\n", map $_->isfinite->all ? join(" ", $_->list) : "", $p->dog) . "\n";
push @plot_chunks, _emit_ascii($this, $outbuf);
splice @plot_chunks, 1, 0, _emit_ascii($this, $chunk_i, $outbuf);
} else {
# It's a collection of array ref data only. Assemble strings.
my $data = $chunk->{data};
Expand All @@ -3217,10 +3218,11 @@ POS
}
$s .= "\n"; # add newline
}
$plotshow .= $s;
push @plot_chunks, _emit_ascii($this, $s);
splice @plotshow_chunks, 1, 0, "\$PGG_data_$chunk_i << e\n${s}e\n";
splice @plot_chunks, 1, 0, _emit_ascii($this, $chunk_i, $s);
}
}
$chunk_i++;
}
##############################
# Finally, finally ... send any required cleanup commands. This
Expand All @@ -3239,10 +3241,10 @@ POS
if $checkpointMessage;
}
push @plot_chunks, [$cleanup_cmd];
$plotshow .= $cleanup_cmd;
push @plotshow_chunks, $cleanup_cmd;
# Flag the output as rescalable if anti-aliasing is in effect
$this->{aa_ready} = 1 if $this->{aa} && $this->{aa} != 1;
(\@plot_chunks, $plotshow);
(\@plot_chunks, join('', @plotshow_chunks));
}

*gplot = \&plot;
Expand Down Expand Up @@ -3306,12 +3308,12 @@ sub plot
# Not in binary mode - send this chunk in ASCII. Each line gets one
# tuple, followed a line with just "e".
sub _emit_ascii {
my ($this, $chunk) = @_;
my ($this, $chunk_i, $chunk) = @_;
# Under real OSes, we can just send a schwack of stuff - there is no echo.
return [$chunk."e\n", {data => 1}]
return ["\$PGG_data_$chunk_i << e\n${chunk}e\n", {data => 1}]
if !$MS_io_braindamage;
my $pipe_stuff = !$this->{dumping} && $echo_eating;
((map $pipe_stuff ? \$_ : $_, map ["$_\n", {data => 1 }], split /\n/, $chunk), ["e\n", {data => 1 }]);
((map $pipe_stuff ? \$_ : $_, map ["$_\n", {data => 1 }], "\$PGG_data_$chunk_i <<e", split /\n/, $chunk), ["e\n", {data => 1 }]);
}

#####################
Expand Down
26 changes: 20 additions & 6 deletions t/plot.t
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ is($@, '', "ascii array-ref plot succeeded");

my $text = eval { $w->plot_generate( xvals(5), xvals(5)**2 ); };
is($@, '', "plot_generate succeeded");
like $text, qr/plot\s*'-'\s*using 1:2 notitle with lines\s*dt solid/, 'plot_generate';
like $text, qr/plot\s*\$PGG_data_\d+\s*using 1:2 notitle with lines\s*dt solid/, 'plot_generate';

eval { $w->plot( xvals(10000), xvals(10000)->sqrt ); };
is($@, '', "looong ascii plot succeeded ");
Expand Down Expand Up @@ -836,6 +836,20 @@ undef $@;
eval { $w->options(justify=>"1") };
is($@, '', "justify accepts positive numbers");

eval {
$w = gpwin('dumb', output=>$testoutput);
$w->multiplot(layout=>[1,1]);
$w->plot(
{
label => [ [ 'left-justified', at=>[0,0], 'left' ] ],
xrange => [ 0, 4 ], yrange => [ -1, 5 ]
},
{ with => 'labels' },
[ 0 ], [ -1 ], [ '' ]
);
};
is($@, '', "labels with multiplot works even in Gnuplot 6.0.1");

##############################
##############################
## Test explicit and implicit plotting in 2-D and 3-D, both binary and ASCII
Expand All @@ -848,31 +862,31 @@ $w->options(binary=>0);
eval { $w->plot(with=>'lines',xvals(5)) };
is($@, '', "ascii plot with implicit col succeeded");

like($PDL::Graphics::Gnuplot::last_plotcmd, qr/plot +'-' +using 0\:1 /,
like($PDL::Graphics::Gnuplot::last_plotcmd, qr/plot +\$PGG_data_\d+ +using 0\:1 /,
"ascii plot with implicit col uses explicit reference to column 0");

eval { $w->plot(with=>'lines',xvals(5),xvals(5)) };
is($@, '', "ascii plot with no implicit col succeeded");
like($PDL::Graphics::Gnuplot::last_plotcmd, qr/plot +'-' +using 1\:2 /s,
like($PDL::Graphics::Gnuplot::last_plotcmd, qr/plot +\$PGG_data_\d+ +using 1\:2 /s,
"ascii plot with no implicit cols uses columns 1 and 2");

eval { $w->plot(with=>'lines',xvals(5,5)) };
is($@, '', "ascii plot with threaded data and implicit column succeeded");
like($PDL::Graphics::Gnuplot::last_plotcmd, qr/plot +'-' +using 0\:1 [^u]+using 0\:1 /s,
like($PDL::Graphics::Gnuplot::last_plotcmd, qr/plot +\$PGG_data_\d+ +using 0\:1 [^u]+using 0\:1 /s,
"threaded ascii plot with one implicit col does the Right Thing");


eval { $w->plot(with=>'lines',xvals(5),{trid=>1}) };
is($@, '', "ascii 3-d plot with 2 implicit cols succeeded");
like($PDL::Graphics::Gnuplot::last_plotcmd, qr/plot +'-' +using 0:\(\$0\*0\):1/s,
like($PDL::Graphics::Gnuplot::last_plotcmd, qr/plot +\$PGG_data_\d+ +using 0:\(\$0\*0\):1/s,
"ascii plot with two implicit cols uses column 0 and zeroed-out column 0");

eval { $w->plot(with=>'lines',xvals(5),xvals(5),{trid=>1})};
isnt($@, '', "ascii 3-d plot with 1 implicit col fails (0 or 2 only)");

eval { $w->plot(with=>'lines',xvals(5),xvals(5),xvals(5),{trid=>1}) };
is($@, '', "ascii 3-d plot with no implicit cols succeeds");
like($PDL::Graphics::Gnuplot::last_plotcmd, qr/plot +'-' +using 1:2:3 /s,
like($PDL::Graphics::Gnuplot::last_plotcmd, qr/plot +\$PGG_data_\d+ +using 1:2:3 /s,
"ascii 3-d plot with no implicit cols does the Right Thing");

eval { $w->plot(with=>'lines',xvals(5,5),{trid=>1}) };
Expand Down

0 comments on commit ac3f7f2

Please sign in to comment.