PDFlib - controlling background and opacity of text color, text background and text stroke - pdflib

I'm trying to set separate values of background and opacity of text color, text background and text stroke (outline).
Code below
$p->save();
$p->setfont($font, 240);
$p->set_gstate($p->create_gstate('opacityfill=1 opacitystroke=1')); // Both fill and stroke are opaque
$p->set_graphics_option('fillcolor={rgb 0.075 0.973 0.024} strokecolor={rgb 0 0 1}');
$p->fit_textline('QfjIL', 30, 30, 'matchbox={boxheight={88% 24.5%} borderwidth=0 round=0 fillcolor={rgb 1 1 0}} charspacing=0 textrendering=2 strokewidth=10 position={left top}');
$p->restore();
results in:
Yellow background, blue letter stroke and green letter fill are opaque - as expected.
Adding opacity for fill and stroke to gstate as:
$p->save();
$p->setfont($font, 240);
$p->set_gstate($p->create_gstate('opacityfill=0.3 opacitystroke=0.3'));
$p->set_graphics_option('fillcolor={rgb 0.075 0.973 0.024} strokecolor={rgb 0 0 1}');
$p->fit_textline('QfjIL', 30, 30, 'matchbox={boxheight={88% 24.5%} borderwidth=0 round=0 fillcolor={rgb 1 1 0}} charspacing=0 textrendering=2 strokewidth=10 position={left top}');
$p->restore();
results in ALL OF background, fill and stroke using the same opacity:
Question:
How can I control separately text background opacity (yellow), letter stroke opacity (blue) and letter fill opacity (green)?

this is the expected result, as you specify the graphics state for all fill and stroke content.
You should set the opacity gstate only for the text, and a solid gstate for the matchbox.
$p->save();
$gstate_solid = $p->create_gstate('opacityfill=1 opacitystroke=1');
$gstate = $p->create_gstate('opacityfill=0.3 opacitystroke=0.3');
$p->fit_textline('QfjIL', 30, 30,
'fontname=NotoSerif-Regular encoding=unicode fontsize=240 ' .
'matchbox={boxheight={88% 24.5%} borderwidth=0 round=0 fillcolor={rgb 1 1 0} gstate=' . $gstate_solid . '} '
'charspacing=0 textrendering=2 strokewidth=10 position={left top} gstate=' . $gstate . 'fillcolor={rgb 0.075 0.973 0.024} strokecolor={rgb 0 0 1}');
$p->restore();
this give the following result, I guess this is the expected result.
you find within the PDFlib 9.2 API reference, chapter 6.2 "Matchboxes" all details about the matchbox options.

Related

How can I make a bar graph displaying y-min and y-max data along with custom colors for each bar?

I would like to make a bar graph that displays each bar as the bottom of the bar being y-min and the top of the bar being y-max. In addition I'd like each bar to have its own color fill and custom tics where xtic(1). Thank you
Similar to this below:
How to make range bar graphs in gnuplot?
ex.
#number #y-min #y-max #rgb
1 2 5 1
2 2.5 5.4 2
3 4 6 3
4 4.3 7 4
Here is what I ended up doing in case someone else needs it
set termoption font ",22"
boxwidth = 0.5
set style fill solid
set xtics nomirror
set ytics nomirror
set xtics font "{/:Bold,22}"
set ytics font "{/:Bold,22}"
set xlabel "Structure"
set xlabel font "{/:Bold,30}"
set ylabel "Potential (V vs. RHE)"
set ylabel font "{/:Bold,30}"
set xrange [-1:4.5]
set yrange [0:7]
set arrow from -1,2.5 to 4.5,2.5 nohead lc rgb 'black' dt 3
plot 'poten-range-plot' u 0:(($2+$3)/2.):(boxwidth/2.):(($3-$2)/2.):4:xtic(1) w boxxyerrorbars notitle lw 3 lc variable, 1/0 t "Pt (111)" lt 1 lc rgb 'black'

Grouped bar plot with multiple labels in x-axis

I am trying to replicate something close to the following graph in gnuplot as I need to use it on a latex paper. I have tried a lot but I cannot make the two-line labels at the bottom. Could you please guide me? Also, how is it possible to have the % character as part of a label in the x-axis? Latex complains about it.
The data are in the following format (example). Each different color corresponds to different method. Blue is method 1 (m1), orange is method 2 (m2), and brown is method 3 (m3)
#% system1-m1 system1-m2 system1-m3 system2-m1 ...
0.5% 16 8 15 6
1% 15 17 16 8
2% 12 10 20 15
Thanks
Edit
My code so far is as follows:
set rmargin 0
set key outside tmargin center top horizontal width 3
set border
set grid
set boxwidth 0.8
set style fill solid 1.00
set xtics nomirror rotate by 0
set format y '%1.f'
set yrange [0 to 22]
set ylabel 'Gain (\%)'
set ytics 0, 5
set style data histograms
set label 1 at -0.3, -4 '|---------System 1------------|'
set label 2 at 2.7, -4 '|---------System 2------------|'
plot "./data/metrics.dat" using 2:xtic(1) title 'Method 1' ,\
"" using 3 title 'Method 2', \
"" using 4 title 'Method 3',
And I have modified the .dat file as
0.5 16 8 15
1.0 15 17 16
2.0 12 10 20
0.5 13 6 4
1.0 11 13 13
2.0 14 12 14
because I cannot make it print the % character. The output graph is
As you can see it is not scalable. I have to put labels by hand (trial and error) and also the labels below the x-axis do not contain the % character.
We've been close: set format x '%.1f\%%'. The following works for me with cairolatex terminal (check help cairolatex).
Code:
### percent sign for tic label in TeX
reset session
set term cairolatex
set output 'SO70029830.tex'
set title 'Some \TeX\ or \LaTeX\ title: $a^2 + b^2 = c^2$'
set format x '%.1f\%%'
plot x
set output
### end of code
Result: (screenshot)
Addition:
Sorry, I forgot the second part of your question: the labels.
Furthermore, in your graph you are using xtic(1) as tic labels, i.e. text format, so the command set format x '%.1f\%%' from my answer above will not help here. One possible solution would be to create and use your special TeX label like this:
myTic(col) = sprintf('%.1f\%%',column(col))
plot $Data using 2:xtic(myTic(1))
For the labels, I would use arrows and labels. Each histogram is placed at integer numbers starting from 0. So, the arrows have to go from x-values -0.5 to 2.5 and from 2.5 to 5.5. The labels are placed at x-value 1 and 4. There is certainly room for improvements.
Code:
### tic labels with % for TeX and lines/labels
reset session
set term cairolatex
set output 'SO70029830.tex'
$Data <<EOD
0.5 16 8 15
1.0 15 17 16
2.0 12 10 20
0.5 13 6 4
1.0 11 13 13
2.0 14 12 14
EOD
set rmargin 0
set key outside center top horizontal width 3
set border
set grid
set boxwidth 0.8
set style fill solid 1.00
set xtics nomirror rotate by 0
set format y '%1.f'
set yrange [0 to 22]
set ylabel 'Gain (\%)'
set ytics 0, 5
set style data histograms
set bmargin 4
set arrow 1 from -0.5, screen 0.05 to 2.5, screen 0.05 heads size 0.05,90
set label 1 at 1, screen 0.05 'System 1' center offset 0,-0.7
set arrow 2 from 2.5, screen 0.05 to 5.5, screen 0.05 heads size 0.05,90
set label 2 at 4, screen 0.05 'System 2' center offset 0,-0.7
myTic(col) = sprintf('%.1f\%%',column(col))
plot $Data using 2:xtic(myTic(1)) title 'Method 1' ,\
"" using 3 title 'Method 2', \
"" using 4 title 'Method 3',
set output
### enf of code
Result: (screenshot from LaTeX document)
As an alternative to the answer of #theozh there is already a build-in function called newhistogram that directly allows to place labels below the x-axis.
While working on an an answer that involves newhistogram I discovered a bug with horizontal key layout, which is now fixed thanks to Ethan. So, with the newest development version of gnuplot at hand I am able to offer a solution that allows for more finetuning like the ability to change the inter-group spacing.
set terminal cairolatex standalone colour header '\usepackage{siunitx}' size 25cm, 7cm
# generate some random data in your format
N = 7
set print $MYDATA
do for [i=1:N] {
print sprintf('0.5 %f %f %f', rand(0)*20, rand(0)*20, rand(0)*20)
print sprintf('1.0 %f %f %f', rand(0)*20, rand(0)*20, rand(0)*20)
print sprintf("2.0 %f %f %f", rand(0)*20, rand(0)*20, rand(0)*20)
}
unset print
# define the look
set style data histograms
set style fill solid 1.00
set boxwidth 0.8
set key horizontal outside t c width 1
set xr [-1:27]
set xtics nomirror
set ytics out 5 nomirror
set grid y # I don't think vertical grid lines are needed here
set ylabel 'Gain/\%'
set rmargin 0.01
set bmargin 3
As for the tic marks, I adapted #theozh's answer a bit – since you are using LaTeX already, you might as well parse the numbers through siunitx, which will ensure correct spacing between numbers and the unit:
myTic(col) = sprintf('\SI{%.1f}{\%}',column(col))
The vertical separation marks like in the screenshot you provided can be created iteratively:
do for [i=1:N+1] {set arrow i from first -1+(i-1)*4, graph 0 to first -1+(i-1)*4, screen 0 lw 2 nohead}
Now for the actual plot command:
plot newhistogram "System 1" offset 0,-0.5 lt 1, for [i=1:3] $MYDATA using (column(i+1)):xtic(myTic(1)) every ::0::2 title sprintf('Method %.0f',i), \
newhistogram "System 2" offset 0,-0.5 lt 1 at 4, for [i=1:3] $MYDATA using (column(i+1)):xtic(myTic(1)) every ::3::5 not, \
newhistogram "System 3" offset 0,-0.5 lt 1 at 8, for [i=1:3] $MYDATA using (column(i+1)):xtic(myTic(1)) every ::6::8 not, \
newhistogram "System 4" offset 0,-0.5 lt 1 at 12, for [i=1:3] $MYDATA using (column(i+1)):xtic(myTic(1)) every ::9::11 not, \
newhistogram "System 5" offset 0,-0.5 lt 1 at 16, for [i=1:3] $MYDATA using (column(i+1)):xtic(myTic(1)) every ::12::14 not, \
newhistogram "System 6" offset 0,-0.5 lt 1 at 20, for [i=1:3] $MYDATA using (column(i+1)):xtic(myTic(1)) every ::15::17 not, \
newhistogram "System 7" offset 0,-0.5 lt 1 at 24, for [i=1:3] $MYDATA using (column(i+1)):xtic(myTic(1)) every ::18::20 not
That looks very nasty, what's going on here?
newhistogram creates a new group of histogram boxes, its first argument is a string that is put below the x axis. It is also told to reset the linetype counter to 1.
Then the three columns of the data are plotted iteratively, but not all lines at once, but only the first three lines, with corresponding key entries.
Then another newhistogram is created and it is told to start at the x value 4 (which would be the default anyway). Now the next three lines are plotted, and so.
Now, every time newhistogram is called an empty line is added to key, hence making trouble with the key placement. Therefore the new keyword introduced by Ethan is
set style histogram nokeyseparators
which will disable this behaviour.
As you see, the spaces between the groups are larger than inside. You might want to change the numbers in newhistogram at ... and adjust the calculation of vertical line positions accordingly.
The plot command is of course highly repetitive, and it would be nice to make it an iterative call. Unfortunately, iterations that span multiple objects are not possible within a plot call. However, it is possible to iteratively put the plot command string together (excessively using string concatenation .) and then plot it.
A = 'newhistogram "System '
B = '" offset 0,-0.5 lt 1'
C = 'for [i=1:3] $MYDATA using (column(i+1)):xtic(myTic(1)) every ::'
myplotstring = A.'1'.B.', '.C."0::2 title sprintf('Method %.0f',i),"
do for [i=2:N] {myplotstring = myplotstring.A.i.B.'at '.(4*(i-1)).', '.C.(3*i-3).'::'.(3*i-1).' not, '}
plot #myplotstring

annnotation crash octave while print

Octave crashes without error when i`ll call print(image successfully saving)
for next code
graphics_toolkit('fltk');
h=figure('renderer', 'opengl','renderermode', 'manual', 'color', 'white',...
'inverthardcopy', 'off','units', 'inches','paperunits',
'inches','paperorientation', 'portrait',...
'visible', 'on','name', 'amplitudeImage','defaulttextcolor',
'black','position', [1 1 7 8.5],...
'paperposition', [0.5 0.5 7 8.5],'numbertitle', 'off');
ofst=0.215;
h=axes('units', 'inches', 'position', [0 0 7 8.5], 'Visible', 'off');
annotation('line',[0.01 0.01],[0.7168 0.915]-ofst,'color',[0.0 0.0
0.0],'linewidth',1)% bottom bar
annotation('line',[0.008 0.012],[0.7168 .7168]-ofst,'color',[0.0 0.0
0.0],'linewidth',1)% bottom bar
annotation('line',[0.006 0.014],[0.8159 0.8159]-ofst,'color',[0.0 0.0
0.0],'linewidth',1)% bottom bar
annotation('line',[0.008 0.012],[0.915 0.915]-ofst,'color',[0.0 0.0
0.0],'linewidth',1)% bottom bar
print mypng.jpg
I tried change graphics toolkit, units type for axes but it did not helps.
But all right if I am not using annotation.
What can I replace annotation?
Octave v5.1.0, Windows 10.

Gnuplot stats does not work as expected: max value not right

Assuming to have the following 4 datasets:
a.csv
1,1
2,3
3,5
5,6
6,9
7,9
8,10
9,12
10,13
b.csv
1,1
2,5
3,10
5,15
6,20
7,25
8,30
9,35
10,40
c.csv
1,1
2,10
3,100
5,1000
6,2000
7,5000
8,10000
9,20000
10,50000
d.csv
1,1
2,20
3,300
5,5000
6,9000
7,10000
8,15000
9,30000
10,100000
In Gnuplot I've tried to run the command stats on each of them to get the maximum value for x and y (i.e., columns 1 and 2) and to set the corresponding xrange & yrange. Unfortunately, the result is not the one I've expected.
Here is the full script:
#!/usr/bin/env gnuplot
set terminal latex
set term pngcairo enhanced size 1500,800
set output 'plot.png'
set multiplot layout 2,2
set xlabel 't' font ',16'
set ylabel '#pkt' font ',16'
set grid xtics lt 0 lw 1 lc rgb "#333333"
set grid ytics lt 0 lw 1 lc rgb "#333333"
set xtics font ',14'
set ytics font ',14'
set key font ',12'
set title font ',20'
set datafile separator ','
###
set title '(a)'
stats "a.csv" using 1:2 name "a"
set xrange [0:a_max_x]
set yrange [0:a_max_y+a_max_y*0.5]
plot "a.csv" using 1:2 title 'v1' with lines linewidth 3 linecolor rgb 'blue'
###
set title '(b)'
stats "b.csv" using 1:2 name "b"
set xrange [0:b_max_x]
set yrange [0:b_max_y+b_max_y*0.5]
plot "b.csv" using 1:2 title 'v1' with lines linewidth 3 linecolor rgb 'blue'
###
set title '(c)'
stats "c.csv" using 1:2 name "c"
set xrange [0:c_max_x]
set yrange [0:c_max_y+c_max_y*0.5]
plot "c.csv" using 1:2 title 'v1' with lines linewidth 3 linecolor rgb 'blue'
###
set title '(d)'
stats "d.csv" using 1:2 name "d"
set xrange [0:d_max_x]
set yrange [0:d_max_y+d_max_y*0.5]
plot "d.csv" using 1:2 title 'v1' with lines linewidth 3 linecolor rgb 'blue'
###
unset multiplot
and the result:
As you can see, maximum values in the plots b, c and d are not correct. Indeed, the verbose output of stats returns:
[...]
Maximum: 10.0000 [8] 13.0000 [8]
[...]
Maximum: 5.0000 [3] 15.0000 [3]
[...]
Maximum: 2.0000 [1] 10.0000 [1]
[...]
Maximum: 1.0000 [0] 1.0000 [0]
[...]
Apparently, only stats for the plot a is right. Is there anything wrong in my script?
You need you reinitialize xrange and yrange after setting them each time, because otherwise stats finds some of you points outside the range you have previously set and does not take them into account. It's the last line below:
set title '(a)'
stats "a.csv" using 1:2 name "a"
set xrange [0:a_max_x]
set yrange [0:a_max_y+a_max_y*0.5]
plot "a.csv" using 1:2 title 'v1' with lines linewidth 3 linecolor rgb 'blue'
set xrange [*:*] ; set yrange [*:*] # <--- This line after each plot will fix your issue
In your case there is no need to use stats in order to set the ranges.
Your requirements are:
Use tight limits for the xrange and the yrange. You get this with set autoscale fix.
Extend the maximum of the yrange by 50%. That is achieved with set offsets 0,0,graph 0.5,0:
#!/usr/bin/env gnuplot
set term pngcairo enhanced size 1500,800
set output 'plot.png'
set multiplot layout 2,2
set xlabel 't' font ',16'
set ylabel '#pkt' font ',16'
set grid xtics ytics lt 0 lw 1 lc rgb "#333333"
set tics font ',14'
set key font ',12'
set title font ',20'
set datafile separator ','
set style data lines
set style line 1 linewidth 3 linecolor rgb 'blue'
###
set title '(a)'
set autoscale fix
set offset 0,0,graph 0.5,0
plot "a.csv" using 1:2 title 'v1' linestyle 1
###
set title '(b)'
plot "b.csv" using 1:2 title 'v1' linestyle 1
###
set title '(c)'
plot "c.csv" using 1:2 title 'v1' linestyle 1
###
set title '(d)'
plot "d.csv" using 1:2 title 'v1' linestyle 1
###
unset multiplot
One further comment: If you're going to use a LaTeX-based terminal for your actual image, don't use latex, but rather epslatex, cairolatex, context or lua tikz, which are all much better regarding the supported features and quality.

Fill a region on a graph with no outline?

I'd like to fill a region on a graph plotted with octave, without any outline:
The fill command accepts a color argument that it respects for the filled area, but it doesn't seem to accept the 'LineColor' property to change the color of the line it draws around the filled area...
e.g.
fill([1 2 3 3 2 1], [1 0.5 1 -1 -1 -1], [0.9,0.9,0.9]); # line is black
fill([1 2 3 3 2 1], [1 0.5 1 -1 -1 -1], [0.9,0.9,0.9], 'LineColor', 'r') # hangs
I'm using octave-3.4.0 on OS X.
The patch command should do the job
verts = [0.2 0.4; ...
0.2 0.8; ...
0.8 0.8; ...
0.8 0.4];
faces = [1 2 3 4];
p = patch('Faces',faces,'Vertices',verts,'FaceColor','b','EdgeColor','none');
Of course you could also place it in one line ... ;-)