Gnuplot: Linear scaling data from a column to a specific range - gnuplot

I am making a circle chart using gnuplot. The first two columns plot a point on the XY plane, and I want the third to dictate the size of the circle. The third column has data between 1000-5000, so I have been using:
plot 'foo.csv' u 1:2:($3/100) w circles
which works ok, but I am wondering if there is a better way.
I made the same chart using d3.js, where I used the scaleLinear() function to map the data from column three (the domain, 0-max value) to the range (1-10). Is there anything similar in gnuplot, where I can map my data to a specific range like this?

The "stats" command can the information required for scaling.
If you want to scale input range [0:max] to effective [0:10] this would do the trick:
stats 'foo.csv' using 3 nooutput
plot 'foo.csv' using 1:2:($3 * 10./STATS_max) w circles
If the linear range does not necessarily start at zero then it may be worth defining a scaling function first.
stats 'foo.csv' using 3 nooutput
scale(y) = 10. * (y - STATS_min) / (STATS_max - STATS_min)
plot 'foo.csv' using 1:2:(scale($3)) with circles

Related

How to adjust xrange to min and max of the points actually plotted in gnuplot?

Normally the xrange and yrange are adjusted to fit the largest and smallest value in the datafile, when plotting according to
plot "my_file.dat" u 1:2
Suppose I want to apply a cutoff to the y-values, so that I only include the points whose y-value is larger than, say 1. If I do
plot "my_file.dat" u 1:($2>1?$2:NaN)
I indeed select only those points. The problem, however, is that the xrange is still adjusted to the range of column 1 of the entire datafile, rather than to only those points which are actually plotted. How can I fix this? My gnuplot version is 5.2.
You also have to filter the undesired x-values in a similar way:
plot 'my_file.dat' u ($2 > 1 ? $1 : NaN):($2 > 1 ? $2: NaN)
If you want to draw a line between these points, a different way of filtering is necessary: see Plotting lines with missing datapoints for multidimensional data

Plotting surface with gnuplot, but I only have a Z-data datafile

I have a data file containing z values (m x n = 2068 x 100), but I fail to find a way to make a surface plot in gnuplot out of that.
In MATLAB the command is straight forward: just surf(M).
The values correspond to readouts over time from a spectrometer, i.e.
wavelength scan1 scan2 scan3 scan4
772.7 3.9609 3.9623 3.9593 3.9643
772.8 2.4688 2.4749 2.4669 2.4689
772.9 2.7233 2.7250 2.7240 2.7270
I understand that gnuplot expects the data to be presented in x,y,z fashion, but my data does not provide that. I'm sorry that I can find no other way to describe what I'm after...
Essentially: the x values are in the first row, but the y values should be the index of the column if that makes sense.
Any help would be much appreciated.
Your data format is halfway between the two formats gnuplot knows about. splot $DATA matrix treats all values as z values (no x or y specified). splot $DATA matrix nonuniform expects the first column to contain y values and the first row to contain x values. You have one column of coordinate data but not one row of coordinate data.
It looks like your x values are evenly spaced, so it would produce a valid surface to ignore them:
splot 'matrix.dat' matrix skip 1 every 1::1
matrix tells gnuplot it is an array of z values
skip 1 tells it to skip the first row, which contains labels rather than z
every 1::1 tells it to read every column starting with column 1 (i.e. skip column 0)
However a much better approach is to draw a separate line for each scan rather than trying to treat it as a surface:
set ylabel "Scan" offset 5
set xlabel "Wavelength" offset 0,-2
set xtics .1 offset 0,-1
unset ytics
unset ztics
splot for [col=2:*] 'matrix.dat' using 1:(col):col title columnhead

plot data with errorbars and sort them with gnuplot

I have a file statistics.dat with measurements I made. It has the format
node Mean StdDev
1 11862.4 142.871
2 11722.4 141.330
[...]
I can make a sorted graph with plot '< sort -n -k2 statistics.dat' u 2
Now I want to add error bars to my plot. When I use plot 'statistics.dat' u 1:2:3 w errorbars The sorting is obviously lost since gnuplot uses the x- and y-value as coordinates.
Is there a way to plot a sorted graph with errorbars?
Or do I need to sort my data and add new x-values according to the sorded position?
Reading the x coordinate from column 1 is counter-productive in this case. To use the ordering of the sorted file, use column 0 (line number):
plot '< sort -n -k2 statistics.dat' u 0:2:3 w errorbars
Depending on the nature of your data and what you are trying to show in the plot, it may be better to simply use the mean value in column 2 as the x coordinate (no extra sorting required):
plot 'statistics.dat' using 2:2:3 with errorbars
If the original file order has some intrinsic significance you might also want to encode the original order somehow, possibly as x tic labels, possibly as a color range:
plot 'statistics.dat' using 2:2:3:1 with errorbars lc palette

Rotating a plot in gnuplot

1-How can I rotate my plot so y would be the new x axis and vice versa?
2- Change the maximum value of y axis from 60 to 100.
The plot is created by this script in the terminal :
set palette grey
plot 'color_map.dat' matrix with image
You can exchange the x and y axes with the using modifier. (Just like for ordinary plots, except that for matrix data, columns 1 and 2 are not in your data file, but are inferred.)
plot 'color_map.dat' matrix using 2:1:3 with image
If you actually just want to change the maximum value on the y (new x) axis, you would use set xrange[:100]. But it sounds like you might actually want to scale the data itself, in which case you could use
plot 'color_map.dat' matrix using ($2/60.*100):1:3 with image
Try help plot matrix for more details.

plot multiple curves with color based on a color map in GNUPLOT

Here is the problem I am having with GNUPLOT: I have data files with two columns each (one for the voltage the other for the current) and each obtained for a certain temperature (which is specified in their name something like iv_300K.dat, iv_290K.dat etc.).
Now what I want is to plot each data file on the same graph and each plot to have a colour based on the file name (I would like to show you a figure I made with Mathematica but it seems that my reputation is too low...)
So lets say I have iv_300K.dat, iv_250K.dat and iv_160K.dat I would like to have three curves coloured first red, second green-ish and third blue, but based on the temperature information in the file name.
I am thinking something similar to what I did in Mathematica:
ColorData["DarkRainbow"][Rescale[T, {160, 350}]]
Where "DarkRainbow" is a colormap and Rescale[x,{min,max}]
gives x rescaled to run from 0 to 1 over the range min to max (according the Mathematica documentation).
So Rescale[250,{160,350}] = 0.473684
At the moment in GNUPLOT I am using the following for testing purposes:
plot for [i=350:160:-10] 'iv_'.i.'.K.dat' using 1:2 with lines title sprintf("".i." K")
but I can't get the colours to map the temperature.
Any help is appreciated!
Use linecolor palette frac to select a color from a palette based on an value in the range [0:1]:
set cbrange [160:350]
set style data lines
plot for [i=350:160:-10] 'iv_'.i.'.K.dat' using 1:2 linecolor palette frac (i-160.0)/(350.0-160.0) title sprintf("%dK", i)

Resources