问题
I am plotting a large number of bars and therefore I only mark the every nth xtic (as seen in the .dat file with " "). Here are the first 15 lines of my input ".dat" file:
1 150
" " 260
" " 340
" " 410
5 270
" " 280
" " 260
" " 370
" " 220
10 230
" " 340
" " 300
" " 200
" " 240
15 160
Plotting this data works fine. However, in my actual .dat file I have about 500k values to plot (500k boxes). Therefore I wanted to color every 10k box with a certain color (or in the example above, every 5th). This is the code used:
set terminal postscript eps color
set output "genetic.eps"
set ylabel "Number of Exons"
set style fill solid 1.0 noborder
plot "genes.dat" using 2:xticlabels(1) smooth frequency with boxes notitle
Thanks!
回答1:
In your case, the smooth frequency isn't actually doing anything (that only works when multiple points have the same x-coordinate, but by not specifying one, the default is to use the line number).
We can get arid of the smooth frequency option and use the variable line color. If we wish to set every 5th box red, and the others blue, we can do
plot "genes.dat" u 0:2:(int($0)%5==0?(255<<16):255):xticlabels(1) with boxes lc rgbcolor variable
In the plot specification, we use
- 0 - 0 column is the line number, this will be the x-value
- 2 - the 2nd column will be the y-value
(int($0)%5==0?(255<<16):255)
- Determine if line number is a multiple of 5
- If it is, set color to 255 shifted by 16 bits (full red)
- If not, set color to 255 (full blue)
xticlabels(1)
- the xtic labels
The lc rgbcolor variable
means to set the line color using an rgbcolor value (a 24 bit integer of the form RRGGBB in hex form) based on a third column.
See help linecolor
for more details.
In the provided example, I first ran set yrange[0:*]
in order to avoid the default yrange value calculation from chopping off the first box.
Line numbers start at 0, and as 0 is divisible by 5, the first box is red, as is the sixth, and so on. If we wish to have the 5th, 10th, and so on boxes red, use int($0+1)
in the color specification in the plot command.
Note: This all works in gnuplot 5. Earlier versions do not support bit shifting, so we must replace the shift expression with the actual result of the computation, which is 16711680 in this case.
回答2:
Try this
set ylabel "Number of Exons"
set style fill solid 1.0 noborder
plot "test" using ($0+1):($2) w boxes lc 'blue' t '',\
'test' u (($0*5)+5):($2):(1) every 5::4 w boxes lc 'red' t ''
It gives you the following out put
$0 stands for the line number (starting at 0)every 5::4
stands for every 5th line starting after line 4
when we do not use every line, we have to prevent bars stretching over the full width. We can do this by providing a 3rd coordinate: the x-width :(1)
来源:https://stackoverflow.com/questions/35201727/gnuplot-color-every-nth-box-in-histogram