z-range threshold for matrix heat map in Gnuplot - matrix

Background:
I have a cross-correlation matrix of rows i and columns j with only z values ranging between -1 and 1. I'm able to output a nice heat map using the following script:
unset key
set tic scale 1
set xtics out
set ytics out
set palette defined (-1 "red", 0 "white", 1 "blue")
set cbrange [-1:1]
set cblabel "Correlation Function"
unset cbtics
set size ratio 1
set xrange [0:588]
set yrange [0:588]
set view map
splot 'file.dat' matrix with image
The Problem:
I want to 'screen out' certain z values, such that -0.50 ≤ z ≤ 0.50 are not taken into account for plotting.

Had a think about this some more, and took onboard the advice offered by Christoph. To achieve what I wanted I changed line from:
set palette defined (-1 "red", 0 "white", 1 "blue")
...to:
set palette defined (-1 "red", -0.5, "white", 0.5 "white", 1 "blue")
Worked like a charm.

Related

How to plot histogram using gnuplot

I have a file contains wire length ranging from 300-1000. How do I use GNUPLOT to make a histogram with length on X-axis (With interval of 100) and number of wires on Y-axis?
For example:
250 wire_0
350 wire_1
360 wire_2
800 wire_3
I want a bar with height of 1 representing wire_0, a bar with height of 2 representing wire_1 and wire_2 and a bar with height of 1 representing wire_3.
Edit #1
Based on both this and this answers, your could try:
binwidth = 100
bin(x, width) = width*floor(x/width)
set tics out nomirror
set style fill transparent solid 0.5 border lt -1
set xrange [0:1000]
set xtics binwidth
set boxwidth binwidth
set yrange [0:3]
$data <<EOD
250 wire_0
350 wire_1
360 wire_2
800 wire_3
EOD
plot $data u (bin($1,binwidth)):(1.0) smooth freq with boxes notitle
Result
Edit #2
If you need the of values above the boxes, you can use.
set table $data2
plot $data u (bin($1,binwidth)):(1.0) smooth freq with boxes;
unset table
plot $data2 u 1:2 w boxes notitle , "" u 1:2:2 w labels offset 0,1 notitle
Result

gnuplot: compact axis label format

Tell me how in gnuplot to properly form the inscriptions in the axis (as shown in the picture).
1) I do not know the values of the y axis
2) I need to set the exponent (power 10) in the axis label automatically
You need to know the order of magnitude before you are plotting. You can get this via stats. Then divide your y-values by a factor which (in the example below) is chosen such that the axis tics should show values between 0 and 100.
The code:
### automatic prefactor in axis scaling and axis label
reset session
# generate some random data
set samples 20
RandomMagnitude = floor(rand(0)*20-10)
RandomValue = rand(0)
set table $Data
plot '+' u 0:(RandomValue*10**RandomMagnitude/($0)) with table
unset table
# get the maximum via stats
stats $Data u 2 nooutput
Max = STATS_max
PrefactorLog = ceil(log10(Max))-2
Prefactor = 10**PrefactorLog
set ylabel sprintf("Y-title, x 10^{%d} units",PrefactorLog)
set format y "%g"
set boxwidth 0.7 relative
plot $Data u 1:($2/Prefactor) with boxes fs solid 1.0 fc rgb "red" ti sprintf("Max value %.2e", Max)
### end of code
The result:

Trouble with plot matrix with image and xrange

I have trouble with xrange. When I put 'set autoscale xfix' image is ok but information axis not. When i put xrange [-1:1] i get info ok but images is damage. Second trouble is flip image. In my data stored in file upper left corner is -1 on image is +1, why?
my data is:
-0.999770 -0.998743 0.946455 0.999678 0.999777
-0.699447 -0.999784 -0.999565 -0.076214 0.999467
0.999921 -0.717181 -0.999790 -0.999734 -0.959481
0.999943 0.999920 -0.733798 -0.999793 -0.999786
0.999943 0.999943 0.999920 -0.749453 -0.999794
my code is:
set terminal png transparent enhanced font "arial,10" fontscale 1.0 size 600, 400
set output 'out.png'
set xtics 0.25
set ytics 0.25
set xrange [-1:1]
set yrange [-1:1]
set cbrange [-1:1]
plot 'data.txt' matrix with image
My image is -1 to 1 step 0.5
if I add set xrange [-1:1] and set yrange [-1:1] I get
$ gnuplot -V
gnuplot 5.0 patchlevel 3
the thing is that your data file is interpreted as a uniform matrix. In this case:
gnuplot> help matrix
Gnuplot can interpret matrix data input in two different ways.
The first of these assumes a uniform grid of x and y coordinates and assigns
each value in the input matrix to one element M[i,j] of this uniform grid.
The assigned x coordinates are the integers [0:NCOLS-1].
The assigned y coordinates are the integers [0:NROWS-1].
So this means that the datapoints in the first row of your file will have y-coordinate set to 0, second row 1, etc. However, since the y-axis by default points upwards, the resulting image is therefore flipped. Also, these points define the centers of the elementary color squares/boxes in the plot. So that's way the "effective x/y-range" is in your case [-0.5:4.5].
To "fix" the y-axis, you could use
set yr [] reverse
Here, [] specifies that the axis is still autoscaled.
Finally, to rescale your image from [0,4] into the [-1,1] range, you could use:
fn(x) = x/2. - 1
plot 'data.txt' matrix u (fn($1)):(fn($2)):3 w image
So in total:
set terminal png transparent enhanced font "arial,10" fontscale 1.0 size 600, 400
set output 'out.png'
set xtics 0.25
set ytics 0.25
set xrange [-1:1]
set yrange [-1:1] reverse
set cbrange [-1:1]
fn(x)=x/2-1
plot 'data.txt' matrix u (fn($1)):(fn($2)):3 w image
EDIT:
One could also adapt the script above to deal with a matrix of a priori unknown size:
set terminal png transparent enhanced font "arial,10" fontscale 1.0 size 600, 400
set output 'out.png'
set xrange [-1:1]
set yrange [-1:1] reverse
set cbrange [-1:1]
fName = 'data.txt'
stats fName nooutput
N = STATS_records - 1
set xtics 1./N
set ytics 1./N
fn(x)=(2*x/N)-1
plot fName matrix u (fn($1)):(fn($2)):3 w image
Here, the stats command first scans the file and stores the number of records into a special variable STATS_records. The function fn then rescales the range [0:STATS_records-1] on [-1:1]. Also, the x/y-tics are adapted automatically.

Double x-axis on a spider plot with Gnuplot

I'm making a spider (or chart, polar, etc.) plot using Gnuplot. The code works properly, but in the output figure I get a duplicate positive x-axis (looking at the figure, you can observe that the line is darker) and also a duplication of labels. How can I solve that?
The code is this:
#!/usr/bin/env gnuplot
unset border
set polar
set angles degrees
set term pngcairo enhanced size 800,800
set output 'graph_eff_10_3_accuracy_spider.png'
set xtics axis
set ytics axis
set grid polar 90
set style line 10 lt 1 lc 0 lw 0.3 # line style for the grid
set grid ls 10
set xrange[-1.5:1.5]
set yrange[-1.5:1.5]
set size square
set lmargin 12
set rmargin 12
set key font ',12'
set title font ',20'
set_label(x, text) = sprintf("set label '%s' at (1.8*cos(%f)), (1.7*sin(%f)) center", text, x, x) #this places a label on the outside
eval set_label(0, "Answer 1")
eval set_label(90, "Answer 2")
eval set_label(180, "Answer 3")
eval set_label(270, "Answer 4")
set linetype 1 lc rgb 'blue' lw 2 pt 7 ps 2 # right, post
set linetype 2 lc rgb 'red' lw 2 pt 7 ps 2 # wrong, post
plot "answers_in_post.csv" using 1:2 title '(IN, R2)' with lp lt 1
And the output figure is this:
That is the raxis which is added in polar mode. Just do a
unset raxis
set rtics scale 0 format ''
and you should be fine.

gnuplot add scale and offset to xtics?

I'm plotting a 600px*600px image in gnuplot, and I would like to have the xtics correspond to a different coordinate system than the pixel system. as it is, the xtics go 0, 100, 200, ..., 500, 600. I would like to make them be at the same place, but have different values. Is there a way to make it so that the tics are uniformly modified, like, xtic[i] = (xtic[i]*c1) + c2?
EDIT: Here's my code. Also, I should clarify that what I'm trying to do is have the xtics and ytics correspond to longitude and latitude specifically. So for instance, I would like to add in a transformation such that xtic=0 -> xtic=$minlat, and xtic=$maxx -> xtic=$maxlat.
#!/bin/sh
inputfilename=$1
outputfilename=$2
minlat=$3
maxlat=$4
minlon=$5
maxlon=$6
imagexsize=$7
imageysize=$8
maxx=$(($imagexsize - 1))
maxy=$(($imageysize - 1))
windowxsize=$(($imagexsize+5+5))
windowysize=$(($imageysize+5+5))
imagename=${inputfilename%.*}
gnuplot <<EOF
set terminal png size $windowxsize,$windowysize
unset key
unset colorbox
set output "$outputfilename"
set lmargin 5
set bmargin 5
set rmargin 5
set tmargin 5
set size square
set xrange [0:${maxx}]
set yrange [0:${maxy}]
set palette grey
set label "$imagename" at screen 0.3,0.95
plot "$inputfilename" binary array=${imagexsize}x${imageysize} format='%ushort' with image
EOF
You can accomplish this quite easily by adding a 'using' argument to your 'plot' command.
Here's an example with an added offset of 10 & scale of 0.5:
plot '-' using (($1+10)/2):(($2+10)/2) with linespoints
1 2
3 4
5 6
e

Resources