Plotting with different spacing for y-axis labels in Gnuplot - gnuplot

I am trying to create a barchart using gnuplot. My requirement is that I should be able to label y-axis as 0, 1, 100, 10,000 (i.e., each tick increases by a factor of 100, except between 1 and 0). Also, this is not log-scale as I want this to start at 0. Let me know if you know how to do this.

You can use set xtics:
gnuplot> set xtics ("0" 1, "1" 2, "100" 3, "10000" 4)
gnuplot> plot "test.dat" notitle with boxes
produces the following plot:
http://marco.uctleg.net/resources/sample_xtics.png
with the following data:
1 12
2 8
3 19
4 42
EDIT: Just noticed you asked to change the y-axis. It's much the same, I'm sure you can work it out.

Marcog's answer is probably the best way to get exactly what you want,
However, if you don't want to do the reassignment of 1 means "0", 2 means "1" etc,
then you could try a simple
set ytics (0,1,100,10000)
To set the tics where you want them,
and then use the set format y to specify the format of the tics.
For example
set format y "10^{%L}"
to put the tics in nice exponential form (note this particular formatting looks pretty in postscript output, but rather ugly in the default gnuplot window).
See http://t16web.lanl.gov/Kawano/gnuplot/tics-e.html for more on the set format command (midway down the page).
All the best

Related

Is there a way to change the y axis on Gnuplot so that my image graphs from hour 16 to hour 15 instead of 0 to 24?

I'm sorry if this has already been asked, I couldn't find it anywhere, but I have an image plot on gnuplot of a three-columned data file for a y range [0:24] and I can't figure out how to use gnuplot to rearrange the image graph so my y axis runs from 16:24 and then 0:16 (in that order and on the same axis). The command I've been using is "plot [] [0:24] '/Users/eleanor/PycharmProjects/attempt2.gray' u 1:2:3 w image" but I don't know what command to use so that hour 16 is at the very bottom instead of 0, and then when y reaches 23:59 y goes to 0 next and then continues increasing up to 15:59 at the very top of the axis. I'm not sure if that makes sense or not, and I've already tried changing the y range to [16:15] and that did nothing except give me an error lol. Any tips would be very much appreciated! :)
a piece of the file im using is below (with the first column being the day of year, the second being the time in decimal hours, and the third being the data):
20 0.0 7.327484247409568
20 0.002777777777777778 8.304658863945411
20 0.005555555555555556 11.641408500506405
20 0.008333333333333333 6.543382279013497
20 0.011111111111111112 13.922090817182697
20 0.013888888888888888 10.696406455987988
20 0.016666666666666666 12.537636516165243
20 0.019444444444444445 11.816216763447612
20 0.022222222222222223 8.914413125514413
20 0.025 5.8225423124691496
20 0.027777777777777776 10.896730484548698
20 0.030555555555555555 9.097140108173859
As currently implemented, with image treats the entire block of data as a single entity. You can't chop it up into pieces within a single plot command. However if your data is dense enough, it may be that you can approximate the same effect by plotting each pixel as a colored square:
set xrange [*:*] noextend
set yrange [0:24]
plot 'datafile' using 1:(($2>16.)? ($2-16.) : ($2+8.)):3 with points pt 5 lc palette
I strongly recommend not making the range limits part of the plot command. Set them beforehand using set xrange and set yrange.
If necessary, you can adjust the size of the individual square "pixels" by using set pointsize P where P is a scale factor. It probably looks best if you make the points just large enough (or small enough) to touch each other. I think the default ones in the image I show are too large.
You can also use the boxxyerror plotting style instead of the image plotting style. Well, here's what the help for boxxyerror says
gnuplot> ? boxxyerror
The `boxxyerror` plot style is only relevant to 2D data plotting.
It is similar to the `xyerrorbars` style except that it draws rectangular areas
rather than crosses. It uses either 4 or 6 basic columns of input data.
Additional input columns may be used to provide information such as
variable line or fill color (see `rgbcolor variable`).
4 columns: x y xdelta ydelta
6 columns: x y xlow xhigh ylow yhigh
....
If you adopt the four-column plotting style above, you must specify xdelta and ydelta in addition to x and y to specify the rectangle. The xdelta and ydelta should be the half-width and half-height of each pixel. From your data, let's say xdelta is half of 1 and ydelta is half of 0.002777777777777778 hours.
Our final script will look like this.
In this script, the second column of "using" is the same as Ethan's answer.
dx = 1.0/2.0
dy = 0.002777777777777778/2.0
set xrange [-1:32]
set yrange [0:24]
set ytics ("16" 0, "20" 4, "0" 8, "4" 12, "8" 16, "12" 20, "16" 24)
set palette defined (0 "green", 0.5 "yellow", 1 "red")
unset key
plot "datafile" using 1:($2>16?($2-16):($2+8)):(dx):(dy):3 \
with boxxy palette

How to gnuplot 2D numerical data but with xtics showing alphabetical tics instead?

Say we got this data 'file.dat':
0.1 2
0.5 1
0.7 3
1.3 23
1.4 20
2.7 2
2.3 6
5.3 50
5.2 50
I wish to plot this as follows:
2D plot (lines is fine), like normal.
Except: xtics label shows the following names instead: "small", "medium", "big" marked exactly at x values 0, 3 and 5, respectively.
Q: How to achieve this?
Permissible actions:
I can modify existing or create new 'file.dat' files.
I can modify gnuplot commands.
set xtics ("small" 0, "medium" 3, "big" 5)
plot 'file.dat' with lines lw 2 lc rgb "black"
should work.
For more information type help xtics:
The explicit ("" , ...) form allows arbitrary tic
positions or non-numeric tic labels. In this form, the tics do not
need to be listed in numerical order. Each tic has a position,
optionally with a label. Note that the label is a string enclosed by
quotes. It may be a constant string, such as "hello", may contain
formatting information for converting the position into its label,
such as "%3f clients", or may be empty, "". See set format for more
information. If no string is given, the default label (numerical) is
used.
An explicit tic mark has a third parameter, the level. The default
is level 0, a major tic. Level 1 generates a minor tic. Labels are
never printed for minor tics. Major and minor tics may be
auto-generated by the program or specified explicitly by the user.
Tics with level 2 and higher must be explicitly specified by the user,
and take priority over auto-generated tics. The size of tics marks
at each level is controlled by the command set tics scale.
Examples:
set xtics ("low" 0, "medium" 50, "high" 100)
set xtics (1,2,4,8,16,32,64,128,256,512,1024)
set ytics ("bottom" 0, "" 10, "top" 20)
set ytics ("bottom" 0, "" 10 1, "top" 20)

gnuplot how to set tics format by power of 10

I have a plot with:
xtics = {0, 2000, 4000, ..., 20000}
and I would like them to be shown as:
0 2 4 ... 20
x 10^3
Is there any way to do so?
You can do so by setting a label at the appropriate position:
set label 1 "× 10³" right at graph 1, -0.05
This would place the text × 10³ right aligned with the right border of your plot and somewhat below it. You probably have to adjust the vertical position (−0.05) a bit depending on your plot.
However, I recommend against indicating factors like that and suggest to incorporate them in the axis label and use set xlabel "amount [10³ potatoes]" for example. The kind of labels you want to use are easy to miss and fortunately not common anymore.

Gnuplot how to lower the number of tics in x axis

The figure has too many xtics and ytics. Can I have half of them?
I know I can manually set tics in a way similar to this:
set xtics (1,2,4,8,16,32,64,128,256,512,1024)
But I feel it is not a general solution. You can not manually set tics for all figures. I have loads of them and the gnuplot code is automatically generated using Java.
Here is the code for the figure: https://dl.dropboxusercontent.com/u/45318932/gnuplot2.plt
Can you help lower down the number of x and y tics?
There is no option in gnuplot to explicitly set the number of tics you want on an axis and have gnuplot decide where to put them. (I really wish there were.)
One option you have is to use the stats command (in gnuplot 4.6+) to find out the range of the data:
ntics = 4
stats 'data.dat' using 1 name 'x' nooutput
stats 'data.dat' using 2 name 'y' nooutput
stats 'data.dat' using 3 name 'z' nooutput
set xtics x_max/ntics
set ytics y_max/ntics
set ztics z_max/ntics
You might have to adjust whether you want the tics to be at integer values or not, but that is the general idea.
There are different ways to set the number of tics depending on what exactly you want to do. For a fixed segment of length 2, starting at zero and ending at 32:
set xrange [0:32]
set xtics 0,2,32
plot sin(x)
If you want an exponential increment, try the following
set xrange [0:32]
set for [i=0:5] xtics (0,2**i)
plot sin(x)
Or you can use a logarithmic scale (in base 2 in this case):
set xrange [1:32]
set logscale x 2
plot sin(x)
You can just use for example
set xtic 10
and it will display the tics on x-axis each 10.
I had a similar problem that I wanted to handle a little more generically in case the data changes while still using somewhat round looking numbers. Therefore I made a helper function:
endsinone(n) = strstrt(gprintf("%g", incrguess), "1")
getincr(range, maxincr, guess) = range/guess < maxincr ? guess : \
(endsinone(guess) ? getincr(range, maxincr, 5*guess) : getincr(range, maxincr, 2*guess))
Then I just have to pass in the range for the axis, the most increments I want on it, and a very lower bound guess about what I would expect the smallest possible increment to be. To keep the rounded looking numbers my functions assume the guess is expressible in the form 1eN or 5eN for some value N. Ie (50 is good, so is 0.0000001, 505 is not). With this function you just have to do something like
set xtics getincr(STATS_max, 6, 1e-9)
will return an incr of less than 6 tics, and there should be several of them assuming STATS_MAX > 1e-9.

How to set axis units in gnuplot

I'm trying to generate some schematic figures using gnuplot. My x scale is of angstrom and the y scale if of mV. Currently, I have the x scale goes like:
0 1e-9 2e-9 3e-9 etc.
And my y scale goes like
-0.07 -0.06 -0.05 etc.
And I want them to be
0 10 20 30 etc.
-70.0 -60.0 -50.0 etc.
respectively.
Is there a way to do this from within the gnuplot (apart from setting the xrange an yrange parameters and multiplying the values by the appropriate amounts)?
There are two ways that I can think of:
You could make use of set xtics (see documentation here)
Then you can explicitly specify what value on your axis will receive which label. So something like this:
set xtics ("0" 0, "10" 1e-9, "20" 2e-9, ...)
should work. Proceed accordingly with the y axis (set ytics)
You could multiply your values accordingly. (Like what you have mentioned in your question)
plot "Data.dat" u ($1*1e9):($2*1e2)

Resources