关注this question,我现在有一个带有自定义颜色条的热图。现在我需要在轮廓线上加上标签。我已经创建了轮廓线,但我无法弄清楚如何放置它们。
这是我目前的情节:
生成情节的代码:
reset
#Function map values on desired ranges. The values set to 90,95,99 are for later labelling
step(x) = (x < 20.0 ? 0 : (x < 50.0 ? 10 : (x < 75.0 ? 20 : (x < 90.0 ? 80 : (x < 95.0 ? 90 : (x < 97.5 ? 95 : (x < 99.0 ? 97 : (x < 99.5 ? 99 : (x < 99.9 ? 100 : 101 )))))))))
# enable 3D data read from a scattered data set in file
#71,46,10 are the number of different values for each axis X,Y,Z in "HeatMap_Test.txt" data file
#If values of dgrid3d are not set accordingly, weird contour values will be generated
set dgrid3d 71,46,10
set contour base # enable contour drawing
set cntrlabel font ",7"
set view 0,0 # if you want to see the graph from above (2D plot)
unset surface # do not plot the surface, only the contour
set cntrparam levels discrete 90,95,99 #set contours only on values 90,95 and 99
set table "contours.dat" #Name of the output file to write the table
splot "HeatMap_Test.txt" u 2:1:(step($3)) with lines notitle
unset table #Write table to file
reset
#Map the Z values to desired ranges
step(x) = (x < 20.0 ? 0 : (x < 50.0 ? 1 : (x < 75.0 ? 2 : (x < 90.0 ? 3 : (x < 95.0 ? 4 : (x < 97.5 ? 5 : (x < 99.0 ? 6 : (x < 99.5 ? 7 : (x < 99.9 ? 8 : 9 )))))))))
set palette maxcolors 10
set palette defined (0 "#333399", 1 "#3333f7", 2 "#3373ff", 3 "#33c6ff", 4 "#5affd2", 5 "#9cff8f", 6 "#dfff4c", 7 "#ffc733", 8 "#ff7a33", 9 "#e53333")
set cbrange [-0.5:9.5]
set cbtics nomirror
set cbtics ( ">99.9" 9, ">99.5" 8, ">99.0" 7, ">97.5" 6, ">95.0" 5, ">90.0" 4, ">75.0" 3, ">50.0" 2, ">20.0" 1, ">10.0" 0 )
set xrange [-30:40]
set yrange [ 25:70]
set xtics 5
set ytics 5
#set title " %"
set grid front linetype -1
set grid xtics lt 0 lw 1 lc rgb "#000000"
set grid ytics lt 0 lw 1 lc rgb "#000000"
plot "HeatMap_Test.txt" u 2:1:(step($3)) notitle pt 5 ps 2 lc palette, "world_10m.txt" notitle with lines ls 1 lc -1, "contours.dat" u 1:2 w l lw 2 lc 0 notitle
以下是数据文件:HeatMap_Test.txt,world_10m.txt
代码适用于GNUplot 4.6和5(我在Linux下工作)
为了获得我想要的情节,我仍然需要做三件事:
1)标记轮廓线(从内线到外线有99,95和90)
2)使边线适合绘图(图中热图越过边界)
3)将标题(在这种情况下为单个'%')放在颜色条的顶部。一个脏的方法是设置一个普通的标题并放置空格(代码中有一条注释行来做),但我认为必须有更好的东西。
我对轮廓进行标注的想法如下图所示:
感谢您的帮助
答案 0 :(得分:1)
让我们以数字 3 开头:彩条的标题。
您想在问号栏的顶部设置cblabel
。虽然我不认为这可以自动完成,但您可以使用这样的offset x,y
:
set cblabel "%" norotate offset -6, 9
仍然是一个黑客,但我认为这比滥用标题更好。
现在编号 2 :使边框适合图。
越过边界线的原因是(隐式)命令plot with points pointsize 2
。刚好在边框上绘制的点将重叠。我建议用这样的splot pm3d
替换点:
set pm3d explicit
set view map
splot "HeatMap_Test_2.txt" u 2:1:(0):(step($3)) notitle w pm3d lc palette , \
"world_10m.txt" u 1:2:(0) notitle with lines ls 1 lc rgb "#ffffff" , \
"contours.dat" u 1:2:(0) w l lw 2 lc rgb "#000000" notitle
为了使这个3D斑点起作用,我做了以下几点:
(0)
来自:
...
25.00 38.00 15.9
25.00 39.00 5.3
25.00 40.00 1.6
26.00 -30.00 0.0
26.00 -29.00 0.3
26.00 -28.00 0.7
...
为:
...
25.00 38.00 15.9
25.00 39.00 5.3
25.00 40.00 1.6
26.00 -30.00 0.0
26.00 -29.00 0.3
26.00 -28.00 0.7
...
像这样:
step(x) = (x < 20.0 ? 0 : \
(x < 50.0 ? 10 : \
(x < 75.0 ? 20 : \
(x < 90.0 ? 80 : \
(x < 95.0 ? 89.999 : \
(x < 97.5 ? 94.999 : \
(x < 99.0 ? 97 : \
(x < 99.5 ? 98.999 : \
(x < 99.9 ? 100 : 101 )))))))))
现在,有了pngcairo终端和Gnuplot 4.6,我们应该到达这里:
数字 1 :标记轮廓线
我没有尝试过,但也许this post可以帮到你。
答案 1 :(得分:1)
我终于明白了,感谢@ maij的回答,使用了他提供的link。
问题编号 3 ,我在@ maij行使用了以下参数:
set cblabel "%" norotate offset -8, 12
问题编号 2 ,在绘制热图时,我不得不更改
plot "HeatMap_Test.txt" u 2:1:(step($3)) notitle pt 5 ps 2 lc palette
到
plot "HeatMap_Test.txt" u 2:1:(step($3)) with image
最后,问题编号 1 ,要将标签放在等高线中,我必须使用与之前相同的link中提供的外部awk脚本。我将脚本命名为 draw_contourlines_label.sh ,脚本内容为:
#!/bin/bash
awk -v d=$2 -v w=$3 -v os=$4 'function abs(x) { return (x>=0?x:-x) }
{
if($0~/# Contour/) nr=0
if(nr==int(os+w/2) && d==0) {a[i]=$1; b[i]=$2; c[i]=$3;}
if(nr==int(os+w/2)-1 && d==0) {i++; x = $1; y = $2;}
if(nr==int(os+w/2)+1 && d==0) r[i]= 180.0*atan2(y-$2, x-$1)/3.14
if(abs(nr-os-w/2)>w/2 && d==1) print $0
nr++
}
END { if(d==0) {
for(j=1;j<=i;j++)
printf "set label %d \"%g\" at %g, %g centre front rotate by %d\n", j, c[j], a[j], b[j], r[j]
}
}' "$1"
此脚本具有以下参数:
#1 - &gt;使用GNUplot生成的轮廓数据文件
#2 - &gt;旗。如果是&#39; 0&#39;,则打印GNUplot的脚本以在图中添加标签。如果它是&#39; 1&#39;,取出轮廓线的点,使等高线图的线不会越过标签
#3 - &gt;格式化标签的空格数
#4 - &gt;偏移以移动标签的空白区域(因此它们不居中)
我使用wxt终端的结果是:
结果是:
生成图表的代码是:
reset
#Function map values on desired ranges. The values set to 90,95,99 are for later labelling
step90(x) = (x < 20.0 ? 0 : (x < 50.0 ? 10 : (x < 75.0 ? 20 : (x < 90.0 ? 80 : (x < 95.0 ? 90 : (x < 97.5 ? 95 : (x < 99.0 ? 97 : (x < 99.5 ? 99 : (x < 99.9 ? 100 : 101 )))))))))
# enable 3D data read from a scattered data set in file
#71,46,10 are the number of different values for each axis X,Y,Z in "HeatMap_Test.txt" data file
#If values of dgrid3d are not set accordingly, weird contour values will be generated
set dgrid3d 71,46,10
set contour base # enable contour drawing
set view 0,0 # if you want to see the graph from above (2D plot)
unset surface # do not plot the surface, only the contour
set cntrparam levels discrete 90,95,99 #set contours only on values 90,95 and 99
set table "contours.dat" #Name of the output file to write the table
splot "HeatMap_Test.txt" u 2:1:(step90($3)) notitle
unset table #Write table to file
reset
#Map the Z values to desired ranges
step(x) = (x < 20.0 ? 0 : (x < 50.0 ? 1 : (x < 75.0 ? 2 : (x < 90.0 ? 3 : (x < 95.0 ? 4 : (x < 97.5 ? 5 : (x < 99.0 ? 6 : (x < 99.5 ? 7 : (x < 99.9 ? 8 : 9 )))))))))
set palette maxcolors 10
set palette defined (0 "#333399", 1 "#3333f7", 2 "#3373ff", 3 "#33c6ff", 4 "#5affd2", 5 "#9cff8f", 6 "#dfff4c", 7 "#ffc733", 8 "#ff7a33", 9 "#e53333")
set cbrange [-0.5:9.5]
set cbtics nomirror
set cbtics ( ">99.9" 9, ">99.5" 8, ">99.0" 7, ">97.5" 6, ">95.0" 5, ">90.0" 4, ">75.0" 3, ">50.0" 2, ">20.0" 1, ">10.0" 0 )
set cblabel "%" norotate offset -8, 12
set xrange [-30:40]
set yrange [ 25:70]
set xtics 5
set ytics 5
set grid front linetype -1
set grid xtics lt 0 lw 1 lc rgb "#000000"
set grid ytics lt 0 lw 1 lc rgb "#000000"
load "<./draw_contourlines_label.sh contours.dat 0 4 0"
plot "HeatMap_Test.txt" u 2:1:(step($3)) with image, "world_10m.txt" notitle with lines ls 1 lc -1, "<./draw_contourlines_label.sh contours.dat 1 3 0" u 1:2 w l lw 2 lc 0 notitle
注意:如果文件&#34; contours.dat&#34;使用GNUplot 4.6.4计算,标签不会以相同的方式出现。计算文件后,版本4.6+显示相同的图。
我们也可以使用splot函数使用@ maij方法。要做到这一点,我们首先需要改变步骤函数,因为否则会产生奇怪的线条。我将该函数重命名为step3D,但它正好在上面发布的@maij:
step(x) = (x < 20.0 ? 0 : \
(x < 50.0 ? 10 : \
(x < 75.0 ? 20 : \
(x < 90.0 ? 80 : \
(x < 95.0 ? 89.999 : \
(x < 97.5 ? 94.999 : \
(x < 99.0 ? 97 : \
(x < 99.5 ? 98.999 : \
(x < 99.9 ? 100 : 101 )))))))))
使用splot,还需要将输入文件格式化为块。我使用了这个简单的awk脚本,它可以直接放在splot命令中:
awk 'NF>=1 && $1!~/#/ && $1!=prev {print \"\"} {prev=$1;print}' HeatMap_Test.txt
在GNUplot 5+中,当绘制colobar百分比时出现半切割,x和y标签太分离,所以我不得不手动调整它们以适应屏幕:
set rmargin at screen 0.8
set lmargin at screen 0.08
set tmargin at screen 0.95
set xtics offset 0, screen 0.036
set ytics offset screen 0.008, 0
使用相同的命令绘制轮廓线,因此我们得到的结果是相似的(由于范围,只有轮廓线有点变化):
生成图表的代码是:
reset
#Function map values on desired ranges. The values set to 90,95,99 are for later labelling
step3D(x) = (x < 20.0 ? 0 : (x < 50.0 ? 10 : (x < 75.0 ? 20 : (x < 90.0 ? 80 : (x < 95.0 ? 89.999 : (x < 97.5 ? 94.999 : (x < 99.0 ? 97 : (x < 99.5 ? 98.999 : (x < 99.9 ? 100 : 101 )))))))))
# enable 3D data read from a scattered data set in file
#71,46,10 are the number of different values for each axis X,Y,Z in "HeatMap_Test.txt" data file
#If values of dgrid3d are not set accordingly, weird contour values will be generated
set dgrid3d 71,46,10
set contour base # enable contour drawing
set view 0,0 # if you want to see the graph from above (2D plot)
unset surface # do not plot the surface, only the contour
set cntrparam levels discrete 90,95,99 #set contours only on values 90,95 and 99
set table "contours.dat" #Name of the output file to write the table
splot "HeatMap_Test.txt" u 2:1:(step3D($3)) notitle
unset table #Write table to file
reset
#Map the Z values to desired ranges
step(x) = (x < 20.0 ? 0 : (x < 50.0 ? 1 : (x < 75.0 ? 2 : (x < 90.0 ? 3 : (x < 95.0 ? 4 : (x < 97.5 ? 5 : (x < 99.0 ? 6 : (x < 99.5 ? 7 : (x < 99.9 ? 8 : 9 )))))))))
set palette maxcolors 10
set palette defined (0 "#333399", 1 "#3333f7", 2 "#3373ff", 3 "#33c6ff", 4 "#5affd2", 5 "#9cff8f", 6 "#dfff4c", 7 "#ffc733", 8 "#ff7a33", 9 "#e53333")
set cbrange [-0.5:9.5]
set cbtics nomirror
set cbtics ( ">99.9" 9, ">99.5" 8, ">99.0" 7, ">97.5" 6, ">95.0" 5, ">90.0" 4, ">75.0" 3, ">50.0" 2, ">20.0" 1, ">10.0" 0 )
set cblabel "%" norotate offset -8, 11.5
set xrange [-30:40]
set yrange [ 25:70]
set xtics 5
set ytics 5
set grid front linetype -1
set grid xtics lt 0 lw 1 lc rgb "#000000"
set grid ytics lt 0 lw 1 lc rgb "#000000"
set rmargin at screen 0.8
set lmargin at screen 0.08
set tmargin at screen 0.95
set xtics offset 0, screen 0.036
set ytics offset screen 0.008, 0
set pm3d explicit
set view map
load "<./draw_contourlines_label.sh contours.dat 0 4 0"
splot "< awk 'NF>=1 && $1!~/#/ && $1!=prev {print \"\"} {prev=$1;print}' HeatMap_Test.txt" u 2:1:(0):(step($3)) notitle w pm3d lc palette ,"world_10m.txt" u 1:2:(0) notitle with lines ls 1 lc rgb "#ffffff", "<./draw_contourlines_label.sh contours.dat 1 3 0" u 1:2:(0) w l lw 2 lc rgb "#000000" notitle
注意:如果您是Windows用户,也可以使用Cygwin的GNUplot版本执行此绘图