I've got data that I want to plot in gnuplot that looks something like this:
1.08 1 4.8
1.53 2 5.9
2.11 3 5.1
2.60 4 6.0
Not that it's terribly important, the first column is the running time of a genetic algorithm, the second column is the generation number, and the third is average fitness.
If I plot it using plot datafile.dat using 1:3 with lines it looks fine, with tics appropriately spaced. However, while I want the x-tics to be labelled with the time, I want the x2-tics to be labelled with the generation number. This is easy to do with plot datafile.dat using 1:3:x2ticlabels(2) with lines, but for the x2-axis it adds a tick for every single line in the data file, instead of automatically choosing an appropriate number of tics.
My full data file has thousands of entries, which results in a solid black line at the top of the graph where the tics would be spaced, and a larger solid black line above where the labels would be placed. Even if I try to manually tell gnuplot how often to place tics on the graph using set x2tics 100 it still displays them for every entry. Is there a way to fix this? Obviously I'd prefer which tics are shown to be automatically chosen, but if I have to do it manually that's fine. The full gnuplot config file is pretty basic, just:
set logscale y 10
set x2tics
plot datafile.dat using 1:3:x2ticlabels(2) with lines
Here's a solution that will only put a label for every other line in the datafile.
plot 'test.dat' u 1:3:x2ticlabels(int($0)%2==0?stringcolumn(2):'')
One thing that is a little interesting is that the tics don't quite line up with the points. I haven't figured out why yet. With thousands of points, you're probably not likely to notice, but with your example datafile with 4 points, it's noticeable.
Unfortunately, it still draws a tic on the axis for every line in the datafile. set x2tics scale 0 will make those tics go away completely.
Related
I'm currently plotting a several plot one side to side. Since there are lots of graphs, I need to waste as little space as possible. At the moment I have this situation:
I want to "right align" the number associated to the first tic in the X axis and "left align" the number associated to the last tic in the X axis. In this way I can achieve the following (saving some pixels):
At the moment both tics and number are set automatically. Is there a way to achieve this in gnuplot? I know you can set via xtics left|center|right the alignment of all the values in the axis.
Regardless of my objective to achieve plot compactness (this may be a XY problem), the question still remains: but does exist a method to manually set the alignment of a particular tic?
Thanks for any kind reply
NOTE: I didn't wrie the version of gnuplot since using a particular version is not a requirement to solve the question.
not very clean solution, but you could for example first define the tics globally and then manually "override" the particular ticks "of interest":
set xr [0:100]
set yr [0:100]
unset key
set xtics 0,20,100
set xtics add (" 0" 0 0)
set xtics add ("100 " 100 0)
plot x w l lc rgb 'dark-red' lw 2
unfortunately, it seems that doing something like this:
set xtics 0,20,100
set xtics add right ("100" 100 0)
affects the alignment of all ticks and not just the added one...
What bugs me in changing the position of only the first and last tic marks is that the space between the labels 20 to 30 won't be the same as 30 to 40 and so on. I've had this problem in the past, and you can play with the number of tics and the range. For example:
set xtics 10
set xrange[15:105]
or if you really want to stick to the 20:100 range:
set xtics 15
set xrange[20:100]
You can also add mxtics to make it look even prettier. This is not a formal solution by any means, just a nice workaround. Hope it helps.
So, here I am trying to plot heatmaps in gnuplot. I have a matrix-formatted text file (with row and column headers), and the command I am using to plot it is
plot "file.txt" matrix rowheaders columnheaders using 1:2:3 w image notitle
The output is this graph:
Obviously, the X and Y labels are useless like this. I believe the problem here is that gnuplot is extracting all labels from the file and plotting them. How would I go about reducing the amount of clutter in here, e.g. plotting every 10th label or so?
Thanks in advance.
Or just make the picture resolution bigger... for instance like 1920,1080 or bigger... like this:
set term pngcairo size 1920,1080
or make the tics numbers like 1000000 smaller and make a label to show that the numbers written on the tics are 1000000 bigger... or both:)
Sorry for my english...
I´m new here and this is my first question, hope my Problem is described properly according to our rules in here...
I´ve got a data file (datafile.dat) which is used to create several plots (see below):
temp name1 name2
10 1000 1200
22 800 750
50 250 200
100 80 82
107 5 3
What I want to do is to create a plot with the values in the second and third column plotted with boxes. On the x-axis the names these values refer to shall be displayed. In Addition it shall be possible to give each of the boxes a specific colour. An additional Advantage would be that the solution can also be used in a Loop (because the original data file contains a lot more columns...).
In the end I want the graph to look something like this:
Desired Layout of the plot.
In order to get this I tried different things I found searching the Internet (see below). I am running gnuplot 5 on Windows with the following command file:
xticlabels
If I try this e.g. for column 2 this doesn´t work:
plot 'datafile.dat' u 2:xticlabels(columnhead(2))
Using an external utility
Didn´t work at all, failure message was produced
Stats
Looks like a pretty good solution if I store the output in a variable. But I can´t get my code working (see below):
reset
set terminal postscript eps size 15 cm, 15 cm colour enhanced dashed "Times, 22"
set output "test.pdf"
stats 'datafile.dat' using 2
b = STATS_sum
plot 'datafile.dat' u 2:xticlabels(b) every ::1
reset
What can I do to create the desired output from the data file above? I tried the Points mentioned above in many different combinations. Suggestion 1, Suggestion 2, Suggestion 3 are further Topic-related ideas to solve the Problem but I got none of these working. Can please anyone help me to get a solution? Any hints will be highly appreciated!!!
Thanks in advance!!!
Michael
EDIT: I found out that this question was already asked from someone else three years ago: Axis label and column header ...Is there maybe a solution today? Also: Question
I can see two methods for doing this. The first is more automatic, but has the disadvantage of not being able to do the colors.
Method 1
Using only one datapoint for each column (as your comment suggests you will be doing), we can almost accomplish this using the columnstacked histogram style. At this point, I'm not sure how to get different colors, as the columnstacked style applies colors to the sections of the stacks.
Using, your example data, and the first line of data, we can do
set style data histogram # we could do w histograms in the plot command instead
set style histogram columnstacked
set boxwidth 0.9 # so the boxes don't touch
set style fill solid
set key autotitle columnhead # first row contains series name
plot for[i=2:3] "datafile.dat" every ::0::0 u i
where every ::0::0 means use the 0th (first) line of data† only.
This produces
To plot columns 2 through 50, for example, just change the for[i=2:3] to for[i=2:50].
Method 2
We can do this by using the stats command to add the labels, and then do a standard plot command.
To set the tic marks, we can do
set xtics 1,1 format ""
do for[i=2:3] {
stats "datafile.dat" every ::0::0 u (a=strcol(i),1) nooutput
set xtics add (a i-1)
}
The first command here sets the xtics to occur every 1 unit starting at 1 but suppresses the labels (we will be setting our own labels).
We then loop over each column, reading the 0th line in the datafile with the stats command. When we read it, we store the columnheader in a variable a. We just return a 1 for the stats command to actually analyze. We actually don't care about the results of this command, we just need it to read the column headers. Finally, we use set xtics add to add this label as an xtic.
Next, we can do some necessary set up commands
set style fill solid
set boxwidth 0.9 # so the boxes don't touch
unset key
set yrange[0:*] # by default, the smallest boxes may be cut off
Finally, we can plot with‡
plot for[i=2:3] "datafile.dat" every ::1::1 u (i-1):i w boxes
The result is
Again, the for loops can be changed to use any number of columns. X-ranges can be adjusted if desired, and linetype commands can be used in the plot command to set colors.
† We use every ::0::0 because the set key autotitle command causes the first line with the column headers to be ignored (processed before the plot command). Thus the first (0th) line is the first line of actual data.
‡ Note that here we use every ::1::1 because the 0th line is the column header line. Without the set key autotitle command, the first line is not automatically ignored.
Currently I created graphs with small size. The spacing really important at this case. I want to add more vertical space between my xticlabels and x axis.
I have tried to set the x bar with
set xtics offset 0,graph 0.05
my gnuplot output:
The data and gnuplot script still same with my previous question here.
You can do the following:
First add a little bit of bmargin by
set bmargin 3
Since you need to add vertical space between your xticlabels and x-axis, you need to adjust the Y-offset, which can be done by the following
set xtics offset 0,-1,0
You can play around with the values to suite your need.
I could not describe how the plot looks like so I just use "strange" as I have no idea why gnuplot gives me such a plot. Here is the thing I am trying to do.
I have a data file with two columns, the first column is the file name and the second is the size of each file. Each column is more than 2 million rows. I just want to plot the distribution of file sizes. Here is my code
set terminal postscript landscape enhanced mono dashed lw 2 "Times" 18
outputfile = "sizedist.ps"
set output outputfile
binwidth = 0.05
bin(x,width)=width*floor(x/width)
plot [0:3.5][]'sizedist.out' using (bin(log10($2/1024),binwidth)):(1.0) smooth freq with boxes t "Binsize=0.05 dex"
set terminal x11
Ideally, it should be a single Gaussian-like bar plot, but it has many other plots over-layed (see my attachment). Any expert on gnuplot knows why this happened?
This happens if some of your data in the frequency plot does not have well defined values (such as NaN, inf etc.).
Since you are using a logarithmic function in the plot, you have to be careful with data that has values <=0. I guess you have files with size=0. In this cases log10 just gives you NaN and this messes up the counting procedure of the frequency plot.
Include a condition to your plot to fix this. For example:
plot [0:3.5][]'sizedist.out' using ($2>0?bin(log10($2/1024),binwidth):0):(1.0) smooth freq with boxes t "Binsize=0.05 dex"