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.
Related
I am try to plot a graph in GNUPLOT which has both positive/negative values. It is plotting in 1st quadrant.I mean x axis start from some negative value, same with y axis. I want to plot on both sides, like we see a 4 quadrants. Like in middle should be 0, on left negative values and on right positive value, for both x and y axis. Please help.
Thanks in advance.
Please make sure that you've done a reasonable search before asking. There are a lot of examples around. Although, sometimes it might not be easy to find the exact graph configuration you are looking for. Basically, everything is in the manual, although it can be confusing for a gnuplot beginner.
You can get help for almost every keyword via typing help <keyword> in the gnuplot console, e.g. help xtics, help zeroaxis, help border, etc.
If I understood your question correctly, you are probably looking for something like this:
Code:
### plot a function in a 4-quadrant graph
reset session
set size square
set xrange[-10:10]
set yrange[-10:10]
set border 0
set xzeroaxis lt -1
set yzeroaxis lt -1
set xtics axis nomirror
set ytics axis nomirror
set mxtics 5
set mytics 5
set grid x y mx my
set xtic add ('' 0) # remove 0 from xtics
set ytic add ('' 0) # remove 0 from ytics
set key top left
f(x) = 0.02*x**3 - 0.5*x
plot f(x) w l lc "red"
### end of code
Result:
Simple question - the range drawn on a plot can be changed with the set xrange [x_min:x_max] command.
Does this command also limit the range used when fitting a function using the data fitting tools in gnuplot? Is there a way to manually specify the ranged used for function fits? (One guess might be the command every? Do I need to over-ride xrange using every?)
The reason I ask is that I am using xrange to plot outputs zoomed in on the low value x region to view transient behaviour more clearly, but I think this may be "slicing off" values from the function fitting at larger x values outside the xrange region selected?
This is an old question, but the current answer is incorrect: the current settings of xrange does affect the range used for fitting if no explicit range is given as part of the fit command. This can be easily seen by a simple example: if you have a datafile test.dat that contains
1 1
2 2
3 3
4 4
5 6
6 8
7 10
8 12
and use a linear fit, you get
fit a+b*x "test.dat" via a,b
plot "test.dat" w p, a+b*x w l
and fit parameters (a,b)=(-1.42, 1.59). However, if you first set the xrange you get
set xrange [4:8]
fit a+b*x "test.dat" via a,b
plot "test.dat" w p, a+b*x w l
and fit parameters (a,b)=(-4,2).
This is at least the current behavior of gnuplot 5.2, but this old thread from 2009 suggests that this has been the behavior for quite some time.
set xrange [x_min:x_max] does not affect the range used when fitting a function.
With the fit command (the same holds for plot) you can explicitly restrict the range to fit for a variable with the following syntax:
[{dummy_variable=}{<min>}{:<max>}]
For instance you can restrict range for the x axis with:
fit [min:max] f(x) "filename"
I am trying to plot a simple graph, x and y coordinates, however how my axis labels are not quite as I wish.
How can I change the '1e+08' and so on to their true value, 100000000
How can I shift the labels of the xtics down so they don't obscure the graph?
Can I print the coordinates in the graph of certian points, for example the dip between 2014-05-10-04-00 and 2014-05-10-08-00?
How can one add more intervals in the labels of the x axis?
Here is the input I'm using:
reset
clear
set output "BytesOverTime.png"
set title "Evolution of Bytes over Time"
set ylabel 'Bytes'
set xlabel 'Time'
set key off
set term png
set xdata time
set timefmt "%Y-%m-%d-%H-%M"
set format x "%Y-%m-%d-%H-%M"
set xtics rotate by 90 nomirror
set datafile separator ' '
set logscale y
plot "timeBytesAverage.out" using 1:2
And the file if its needed to test:
http://pastebin.com/raw.php?i=qqpeJj4V
How can I change the '1e+08' and so on to their true value, 100000000
set format y "%f"
or, as this will give you decimal numbers:
set format y "%.0f"
You may also have a look at
set format y "%.0s%cByte"
which will create MByte, GByte, ... (See right y-axis in my plot)
However, this will be to the base of 1000, not 1024.
How can I shift the labels of the xtics down so they don't obscure the graph?
set xtics rotate by 90 nomirror right
will right-align (left-flush) the tic marks
Can I print the coordinates in the graph of certian points, for example the dip between 2014-05-10-04-00 and 2014-05-10-08-00?
If you know the coordinates, yes:
set xtics add ("high" "2014-05-09-23-30", "low" "2014-05-10-22-30" )
The general syntax is:
set xtics add ( <label1> <position1>, <label2> <position2> , ...)
This will add the list of labels to the automatically generated labels. If you omit the word add, there will be no automatically generated labels, just those in your list.
How can one add more intervals in the labels of the x axis?
set xtics 3600
will generate a label every 3600 seconds, i.e. every hour. This does not work for log scale axes.
set mxtics 2
will cause the gap between two major (labeled) tics being divided into two smaller gaps, i.e. one minor tic mark between two major ones. (However, it seems not to be necessary here, as gnuplot decides to use 2 on it's own)
And here is the result:
Note that I also added
set y2tics
set y2range[1E8:1E12]
set format y2 "%.0s%cByte"
set logscale y2
to demonstrate this special format on the right y-axis.
And by the way: The format of the labels on the y-axis is independent from the format in the data file.
set timefmt "%Y-%m-%d-%H-%M" defines the format inside the file
set format x "%Y-%m-%d-%H-%M defines the format in the plot. So ou may do something like set format x "%Y-%m-%d %H:%M which is more readable.
I have non-contiguous date/time data (eg weekend data are missing - I don't have rows in data file for them) and I'd like to not to draw them. Graphically, it would be like cutting out a vertical slice of a plot. I'm aware that the X scale would not be linear and am perfectly happy with this.
Here it is what I want to get rid of:
The gnuplot script is auto-generated so it doesn't have to be very neat if it can't be. Currently I'm doing:
set xdata time
set timefmt "%d/%m/%Y-%H:%M:%S"
unset mx2tics
unset mxtics
set xtics border in scale 1,0.5 nomirror rotate font "Times-Roman,12" "$time_min", $xtics, "$time_max"
set xrange ["$time_minb" : "$time_maxb"]
set grid xtics back
Where obviously $var is a proper value of some variable $var. What I'd like to retain: some small (1-2 candles) margin on the left and on the right (between box border and data), labeled ticks every 10 candles.
Ideally all ticks at the borders of time intervals that would be put together in X axis would be marked. Also in the perfect world those labels would be slightly drawn aside to not to overlap each other. But I'm not very picky, I could bear even overlapping of the 2 labels on the joint of 2 intervals if only "empty piece" of a plot is removed.
BTW: I have gnuplot 4.6 but can update to 5.0 if it's necessary.
As I understand the question, it is not about breaking the axis. This would also be possible but might get complicated if you have more than one break.
Actually, there are later questions about the similar topic, e.g.: Remove weekend gaps in gnuplot for candlestick chart
The basic idea is to plot the data against the row index (pseudocolumn 0) instead of time (column 1) in order to make discontinuous data "continuous" and "manually" add the xtic labels.
The accepted solution to the above question, however, is using xticlabels and defines a function which shows, e.g. every 5th xticlabel. This looks OK for the illustrated case. However, although, only every Nth ticlabel will be shown, a tic will be created nevertheless at every data entry which will not look good for data with larger time range.
Hence, another approach would be plotting the data twice: first time for the data and the second time plotting NaN, i.e. nothing but the tics, where you can easily select the spacing via every (check help every). Although the data looks continuous, it might useful to indicate the breaks somehow, e.g. with extra tics or vertical lines. Alternatively, depending on the data, each start of a "continuous" subset could be marked with a date label followed by regular minor tics without labels.
Script: (works with gnuplot>=5.2.0, with some adaptions probably with earlier versions).
### plot dis-continuous time "continuous"
reset session
# create some random test data
set table $Data
myTimeFmt = "%Y-%m-%d"
t0 = time(0)
y0 = 100
f(t) = strftime(myTimeFmt,t1=t0+$0*3600*24)
plot '+' u (f(0)):(y0=y0+rand(0)*2-1) w table
t0 = t1 + (rand(0)*30+30)*3600*24
replot
t0 = t1 + (rand(0)*50+50)*3600*24
replot
unset table
set format x "%Y\n%m-%d" timedate
set grid x,y
set ytics 2
set key noautotitle
set multiplot layout 2,1
plot $Data u (timecolumn(1,myTimeFmt)):2 w l lc "red"
myXTic(col) = strftime("%Y\n%m-%d",timecolumn(col,myTimeFmt))
N = 30
plot $Data u 0:2 w l lc "web-green", \
'' u ($0*N):(NaN):xtic(myXTic(1)) every N
unset multiplot
### end of script
Result:
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)