Can't get Perl Chart (gnuplot) to mark all ticks along axis
Usage Chart::Gnuplot
in perl.
The x-axis must be for date / time.
I indicate
timeaxis => "x" (seems to work)
I use
timefmt => '%Y-%m-%d_%H:%M:%S'
to read the elements in the ref array passed to xdata in Chart::Gnuplot::DataSet->new
. (this works too)
Using
xtics => {labelfmt => "%m-%d %H", rotate => -90}
to display the labels the way I want them. (that everything seems to work too)
Indeed, everything looks good, except for the fact that it only marks a few ticks on the x-axis (date / time). I want to label them all (or whatever, or have some control over that)
I found lots of examples of how to do this for numbers (note dates) using ... start, incr, end, etc. And I tried a lot of experiments to get this to work. But I think I have exhausted everything I can find in this googling and I am still stuck :-(
So, if there is any advice on how to do this to denote all date / time ticks, I would really appreciate it.
source to share
You can use xtics => { labels=>[...] }
, but you need to respect that
In the case of these timers, the position values ββmust be specified as dates or times according to the timefmt format .
from gnuplot documentation .
Assuming the array @x
contains the dataset time values ββat timefmt , x-ticks can be forced at each of those times.
xtics => {
labels=>[map { q(').$_.q(') } @x]
}
There are many ways to add single quotes at any given time, but I think the map
above is the cleanest one.
You can of course provide your own shortcuts, just make sure they are correct and the same as timefmt . I think the translation operator in Perl q()
is the way to go.
labels=>[ q('2005-6-7_07:04:53') , q('2005-6-7_07:05:10') ]
Complete working example
Here is a complete working example modified from tit gallop examples.
#!/usr/bin/perl -w
use strict;
use Chart::Gnuplot;
# Change the date time format of the tic labels
# - the solution is the same as change the number format
# Date array
my @x = qw(
2005-6-7_07:00:00
2005-6-7_07:05:00
2005-6-7_07:10:00
2005-6-7_07:15:00
);
my @y = qw(
3562279127
3710215571
3877469703
3876354871
);
# Create the chart object
my $chart = Chart::Gnuplot->new(
output => 'test.png',
xtics => {
rotate => -90,
labelfmt => "%m-%d %H",
labels=>[map { q(').$_.q(') } @x]
},
timeaxis => "x", # declare that x-axis uses time format
);
# Data set object
my $data = Chart::Gnuplot::DataSet->new(
xdata => \@x,
ydata => \@y,
style => 'linespoints',
timefmt => '%Y-%m-%d_%H:%M:%S',
);
# Plot the graph
$chart->plot2d($data);
Without, labels
you get something like this.
With help, labels
you get something like this.
source to share