Gnuplot: how to fill a bar with both a color background and a pattern

本小妞迷上赌 提交于 2019-12-23 11:52:11

问题


I want to fill a bar with both a color background and a pattern. Is it possible in Gnuplot?

I am using Gnuplot 4.6.5

The code I have now:

# set terminal pngcairo  transparent enhanced font "arial,10" fontscale 1.0 size 500, 350 
# set output 'histograms.2.png'
set boxwidth 0.9 absolute
set style fill   solid 1.00 border lt -1
set key inside right top vertical Right noreverse noenhanced autotitles nobox
set style histogram clustered gap 1 title  offset character 0, 0, 0
set datafile missing '-'
set style data histograms
set xtics border in scale 0,0 nomirror rotate by -45  offset character 0, 0, 0 autojustify
set xtics  norangelimit font ",8"
set xtics   ()
set title "US immigration from Northern Europe\nPlot selected data columns as histogram of clustered boxes" 
i = 22
set yrange [0:2000]
set style line 1 lc rgb 'orange';
set style line 2 lc rgb 'pink';
plot for [i=2:7] 'data.dat' using i:xtic(1) ti col ls i%2+1;

The data file:

Region  Denmark France Demark-Women France-women Demark-man France-men
1891-1900   1000 1100   500 600 500 500
1901-1910   1500 1600   1000 600 500 1000

Here are the links to download the script: https://dl.dropboxusercontent.com/u/45318932/histograms2.plt and data file: https://dl.dropboxusercontent.com/u/45318932/data.dat

The script gives me:

What I want is:

It would be much appreciated if someone can help me improve the code to produce the second figure. Thanks.


回答1:


That one is very tricky because you cannot change the background color of the fill patterns. And by default the background color of the patterns is white, and not transparent or empty.

The only terminal which can be manipulated adequately is the lua tikz terminal. Here, I first draw all the color boxes, and later in a second iteration the fill patterns. To have a new iteration, I use the newhistogram option, which however causes a gap in the legend.

To remove the white background of the fill patterns, I remove the relevant parts from the output stream with sed. Quite hacky, but it works:

set terminal lua tikz standalone size 5in, 3in color
set output '| sed ''s/\\gpfill{color=gpbgfillcolor}//g'' > histograms.tex'
set boxwidth 0.9 absolute
set style fill   solid 1.00 border lt -1
set key inside right top vertical Right noreverse noenhanced autotitles nobox
set style histogram clustered gap 1 title  offset character 0, 0, 0
set datafile missing '-'
set style data histograms
set xtics border in scale 0,0 nomirror rotate by -45  offset character 0, 0, 0 autojustify
set xtics  norangelimit font ",8"
set xtics   ()
set title "US immigration from Northern Europe\nPlot selected data columns as histogram of clustered boxes" 
i = 22
set yrange [0:2000]
set xrange [-1:2]
set style line 1 lc rgb 'orange';
set style line 2 lc rgb 'pink';
plot for [i=2:7] 'data.dat' using i:xtic(1) ti columnhead(i > 3 ? 10 : i) ls i%2+1 fillstyle solid noborder,\
     newhistogram at -1, \
     '' using 2 ti 'total' lt -1 fillstyle empty,\
     '' using 3 notitle lt -1 fillstyle empty,\
     '' using 4 title 'women' lt -1 fillstyle pattern 5,\
     '' using 5 notitle lt -1 fillstyle pattern 5,\
     '' using 6 title 'men' lt -1 fillstyle pattern 6,\
     '' using 7 notitle lt -1 fillstyle pattern 6

set output
system('pdflatex histograms.tex')

Result with 4.6.5:

Just found out, that you can specify the patterns to have no background color like

... fillstyle pattern 6 transparent

For which terminals that works depends on the gnuplot version:

plot x with filledcurves x1 fillstyle solid fc rgb '#990000',\
     x with filledcurves x1 fillstyle pattern 4 transparent lc rgb 'white'

Result (with svg terminal and version 4.6.5):



来源:https://stackoverflow.com/questions/25065288/gnuplot-how-to-fill-a-bar-with-both-a-color-background-and-a-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!