title: Contour lines with labels in heatmap with GNUplot
body: <p>Following <a href="https://stackoverflow.com/questions/40887385/irregular-percentile-tics-in-colorbar-in-gnuplot">this question</a>, I have now a heatmap with custom colorbar. Now I need to put contour lines with labels. I have created the contour lines, but I can't figure out how to put them.</p>
  
  <p>This is the plot I currently have:</p>
  
  <p><a href="https://i.stack.imgur.com/KFTaq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KFTaq.png" alt="enter image description here"></a></p>
  
  <p>Code to generate the plot:</p>
  
  <pre><code>reset
  #Function map values on desired ranges. The values set to 90,95,99 are for later labelling
  step(x) = (x &lt; 20.0  ? 0 :  (x &lt; 50.0  ? 10 :  (x &lt; 75.0  ? 20 : (x &lt; 90.0  ? 80 : (x &lt; 95.0  ? 90 : (x &lt; 97.5 ? 95 :  (x &lt; 99.0 ? 97 : (x &lt; 99.5  ? 99 : (x &lt; 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 &lt; 20.0  ? 0 :  (x &lt; 50.0  ? 1 :  (x &lt; 75.0  ? 2 : (x &lt; 90.0  ? 3 : (x &lt; 95.0  ? 4 : (x &lt; 97.5 ? 5 :  (x &lt; 99.0 ? 6 : (x &lt; 99.5  ? 7 : (x &lt; 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 ( "&gt;99.9" 9, "&gt;99.5" 8, "&gt;99.0" 7, "&gt;97.5" 6, "&gt;95.0" 5, "&gt;90.0" 4, "&gt;75.0" 3, "&gt;50.0" 2, "&gt;20.0" 1, "&gt;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
  </code></pre>
  
  <p>Here are the data files: <a href="http://www.gage.es/sites/default/files/gLAB/src/HeatMap_Test.txt" rel="nofollow noreferrer">HeatMap_Test.txt</a>, <a href="http://www.gnuplotting.org/data/world_10m.txt" rel="nofollow noreferrer">world_10m.txt</a></p>
  
  <p>The code works in GNUplot 4.6 and 5 (I am working under Linux)</p>
  
  <p>To get the plot I want, I still have to do three things:</p>
  
  <p>1) Label the contourlines (with 99, 95 and 90 from the inner to the outer lines)</p>
  
  <p>2) Make the borderlines fit the plot (in the figure the heatmap goes over the borders)</p>
  
  <p>3) Put a title (in this case a single '%') just on top of the colorbar. A dirty way to do it is to set a normal title and put spaces (there is a commented line in the code to do it), but I think there must be something better.</p>
  
  <p>My idea of labelling the contour is like in this image:</p>
  
  <p><a href="https://i.stack.imgur.com/KCO1x.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KCO1x.png" alt="enter image description here"></a></p>
  
  <p>Thank you for your help</p>