I have this code:
set title "Ex1.txt"
set key title "Legenda"
set key inside right top vertical Right reverse enhanced autotitle box opaque
set key noinvert samplen 1 spacing 1 width 0 height 0
set style fill transparent solid 0.50 noborder
set parametric
set trange[0:]
set xrange[0:]
set yrange[0:]
set grid
set terminal pngcairo
set output 'ev.png'
plot [0:][0:] t, -2.0*t+(2000.0) with filledcurves x1, t, -0.6666666666666666*t+(1333.3333333333333) with filledcurves x1, t, -0.8333333333333334*t+(0.0),t, -0.8333333333333334*t+(1333.3333333333333),t, -0.8333333333333334*t+(1416.6666666666667),
unset output
unset terminal
unset parametric
exit
But when I run this script the windows, in the x "range", goes from 0 to 5. It really is supposed to start at zero but if it goes only until 5 I can barely distinguish the lines.
Here you can see the output of the code as it is:
If I change the x scale to [0:700] it goes like this:
As you can see much better because we can distinguish all lines. The problem is that I can't specify the maximum range because the equations might be different, because this is an output of a java program that I have, for example in this case 700 works but in another exercise the optimal value could be 300. Is there a way to make the gnuplot to know the max range of the x axis without the autoscale as it is (because it stops at x=5)?
Thanks in advance
No, there is no way to do that. gnuplot uses a default fixed range for all functions (unless set to another value by a range command), and it has no idea what you might find interesting (do you want to see where the curves intersect, or do you want to see where they intersect the axes - gnuplot wouldn't know), so can't highlight any such features.
Additionally, although it does have some features to analyze curves and such, it is far from being a mathematical workspace, and would have no way to find such interesting points. gnuplot is designed to graph data, not manipulate it.
If you have to drive this from another program, you are going to have to have that program do the analysis and figure out what range to use. I have several python programs that use gnuplot to graph data, but the python code figures out what ranges need to be and adds the command to the gnuplot call.
Related
I am a gnuplot-newbie and am stuck with the following situation. Based on this I have a gnuplot script as follows:
clear
reset
set key off
set border 3
set style fill solid 1.0 noborder
bin_width = 0.01;
set boxwidth bin_width absolute
bin_number(x) = floor(x/bin_width)
rounded(x) = bin_width * ( bin_number(x) + 0.5 )
plot '1000randomValuesBetween0and1.dat' using (rounded($1)):(1) smooth frequency
Which was a good first step; but I would like to have a smooth curve through the points that are generated by counting the frequency. with filledcurves lacked what I wanted in 2 ways. First it is not smoothed (I would prefer something like bezier which is not usable after with); second the filling is done in a rather unexpected way which doesn't fit my needs (for me unexpected). See this picture .
To give a little bit more context: I ultimately want to use this to generate
violin plots with gnuplot without having to do the binning beforehand so I can just give my script a single-column data-file and am ready to go.
EDIT: I tried adapting the "normal" density plot from this demo as another first step, but I failed; I read in the documentation that bandwidth should be 1/#points so it
should be 0.001 in my case meaning I tried this:
set border 3 front lt black linewidth 1.000 dashtype solid
set style increment default
set style data filledcurves
set xtics border in scale 0,0 nomirror norotate autojustify
set xtics norangelimit 0.00000,0.5,1.0
set title "Same data - kernel density"
set title font ",15" norotate
plot 'random01.dat' using 1:(1) smooth kdensity bandwidth 0.001 with filledcurves above y lt 9
which results in this picture:.
Setting no bandwith or lower/higher values didn't solve the issue.
The plot specifies using 1:(1) because I just have a single column so according to the doc the first value should be this column and as the second value would specify a weighting which should be 1/#points according to doc.
EDIT2: Setting bandwidth to the ideal value or not setting it at all always yields the same result which doesn't change anything except the scale of the y-axis with changing the weighting.
My data are 1000 values in a range between 0 and 1 (created randomly for testing purposes).
Here the new plot
EDIT3: zooming out may show another aspect of the problem as the plot seems to extend outside the interval of the given values (I checked the values and there are no examples <0 or >1). Here's the graph:
The demo 'violinplot.dem' included with the gnuplot distribution package and also available online shows how to do what you want using the combination "smooth kdensity" and "with filledcurve" applied to unbinned data.
Online version here: violin plot demo
Notes:
You mis-read the documentation. 1/N is not the recommended bandwidth, it is the normalized uniform weight. The plot you showed initially looks like the bandwidth was set far too low. What is the range of values in your data?
I suggest letting the program calculate the "ideal" bandwidth for you and then adjusting it afterwards if you think it is too large. The ideal value is stored in GPVAL_KDENSITY_BANDWIDTH. Increasing the bandwidth will make the envelope smoother; decreasing it will emphasize local spikes.
is there a way to avoid the drawing of the near asymptote line in the function 1/(2-x), for example, without usage of conditional plotting? The idea is to draw iterated functions based in this one and, since asymptote changes, using conditional plotting isn't a good solution.
You can plot with points at a very high sampling rate:
set yrange [-10:10]
set samples 100000
plot 1/(2-x) with points
If the singularity occurs at different values of x you can use conditional plotting on y:
f(x)=1/(2-x)
set samples 1000
plot (abs(f(x)) < 10 ? f(x) : 1/0) with lines
I have been wondering about this for a while, and it might already be implemented in gnuplot but I haven't been able to find info online.
When you have a data file, it is possible to exchange the axes and assign the "dummy variable", say x, (in gnuplot's help terminology) to the vertical axis:
plot "data" u 1:2 # x goes to horizontal axis, standard
plot "data" u 2:1 # x goes to vertical axis, exchanged axes
However, when you have a function, you need to resort to a parametric function to do this. Imagine you want to plot x = y² (as opposite to y = x²), then (as far as I know) you need to do:
set parametric
plot t**2,t
which works nicely in this case. I think however that a more flexible approach would be desirable, something like
plot x**2 axes y1x1 # this doesn't work!
Is something like the above implemented, or is there an easy way to use y as dummy variable without the need to set parametric?
So here is another ugly, but gnuplot-only variant: Use the special filename '+' to generate a dynamic data set for plotting:
plot '+' using ($1**2):1
The development version contains a new feature, which allows you to use dummy variables instead of column numbers for plotting with '+':
plot sample [y=-10:10] '+' using (y**2):(y)
I guess that's what come closest to your request.
From what I have seen, parametric plots are pretty common in order to achieve your needs.
If you really hate parametric plots and you have no fear for a VERY ugly solutions, I can give you my method...
My trick is to use a data file filled with a sequence of numbers. To fit your example, let's make a file sq with a sequence of reals from -10 to 10 :
seq -10 .5 10 > sq
And then you can do the magic you want using gnuplot :
plot 'sq' u ($1**2):($1)
And if you uses linux you can also put the command directly in the command line :
plot '< seq -10 .5 10' u ($1**2):($1)
I want to add that I'm not proud of this solution and I'd love the "axis y1x1" functionality too.
As far as I know there is no way to simply invert or exchange the axes in gnuplot when plotting a function.
The reason comes from the way functions are plotted in the normal plotting mode. There is a set of points at even intervals along the x axis which are sampled (frequency set by set samples) and the function value computed. This only allows for well-behaved functions; one y-value per x-value.
I have a problem with gnuplot. I've searched and I don't find the correct solution. I'm plotting some data arranged in three columns with the command splot, and the steps in x and y are different. The plot I get with:
set view map
splot 'data.dat' using 1:2:3 with points palette
is:
and I would like the white space to be filled, making each tile size adapt, avoiding interpolation.
Some ideas are given here Reduce distance between points in splot.
I've tryed http://gnuplot.sourceforge.net/demo/heatmaps.html too, but with image doesn't seem to work :(
I should avoid pointsize as my grid changes from time to time.
You can try
set pm3d map interpolate 1,1 corners2color c1
splot 'data.dat' using 1:($2-5e-5):3
This uses no interpolation, and the color of each polygon depends on the value of corner 'c1'. You may need to test if this is the correct one, or if you need 'c2', 'c3', or 'c4'.
Another solution to my problem, better than this one for some terminals at least, is given in the answers to my other question about maps appearance in pdfcairo terminal, where the solution comes when using plot with image insted of this splot. I tried to use that before, as I mention here, but maybe it also needed this specific data format.
I'm trying to present data in a boxplot with a few additions.
On top of the boxplot, i want to also print all the data points, since there aren't that many.
There will be many boxplots side by side, and the data points will correspond, so each data point in one plot will be represented in another boxplot, however their order can change. That's why I want to color the points.
I got this so far:
plot data using (1):($1) with boxplot,\
data using (1):($1) with points lc variable
[more plots...]
This needs an extra column in each datafile, that specifies the linecolor. Which works fine, if I had such a column, or if I could care to add it.
Is there another way to iterate through the linestyles (or colors), so it plots the first point with style 1, the second with style 2 etc.?
It seems like a real easy problem, that's either solved by some command I can't seem to find, or maybe by taking the linestyles from a different file, which would be the same for all plots (if that works in gnuplot).
Furthermore, I'd like to know if the boxplot command has the additional feature of being able to plot the average as well (or do I absolutely need the stats command from gnuplot 4.6, or some kind of hack).
Sometimes it's just nice to be able to simply add the average in a boxplot.
Is there another way to iterate through the linestyles (or colors), so it plots the first point with style 1, the second with style 2 etc.?
Yes. Gnuplot provides a number of pseudo-columns. To get more information, see
help datafile using pseudocolumn
But the gist of it is that you can use column(0) for this. I believe that iteration starts at 0 though. Since there isn't a ls 0, you'll need to add 1.
plot data using (1):($1) with boxplot,\
data using (1):($1):(column(0)+1) with points lc variable
Furthermore, I'd like to know if the boxplot command has the additional feature of being able to plot the average as well (or do I absolutely need the stats command from gnuplot 4.6, or some kind of hack).
I believe that you need either gnuplot 4.6 or some kind of hack. One such hack (which will work using gnuplot 4.4, but not earlier) could be:
sum=0.0
npt=0
compute_sum_npt(x)=(npt=npt+1,sum=sum+x,NaN)
set term unknown
plot data u 1:(compute_sum_npt($1))
avg=sum/npt
set term ...
set output ...
plot data using (1):($1) with boxplot,\
data using (1):($1):(column(0)+1) with points lc variable,\
avg w lines ls -1
If your version of gnuplot is earlier than 4.4, you'll need to use a shell command to compute the average. Something like awk should suffice.