I have data file like following:
#X1 y1 x2 y2 Number
123 567 798 900 1
788 900 87 89 2
....
and I draw my graph using following command
plot '~/Desktop/pointcolor.txt' using 1:2:($3-$1):($4-$2):5 with vectors palette nohead notitle
here is the result.
As you can see in above image, the result is to compressed, so I want to draw one line per ten lines which have same Number in data file.
Edit1:
I try #ewcz solution, as follow:
stat="0 0 0 0 0 0 0" # How should I define array as [gnuplot][2] didn't support it?
plot "< awk '{if(( (stat[$5]++)%10==0 )) print}'~/Desktop/pointt.txt" using 1:2:($3-$1):($4-$2):5 with vectors palette nohead notitle
but I get this error:
gnuplot> load "gnuplot.cmd"
awk: line 1: syntax error at or near
gnuplot> plot "< awk '{if(( (stat[$5]++)%10==0 )) print}'~/Desktop/pointcolor.txt" using 1:2:($3-$1):($4-$2):5 with vectors palette nohead notitle
^
"gnuplot.cmd", line 6: warning: Skipping data file with no valid points
gnuplot> plot "< awk '{if(( (stat[$5]++)%10==0 )) print}'~/Desktop/pointcolor.txt" using 1:2:($3-$1):($4-$2):5 with vectors palette nohead notitle
^
"gnuplot.cmd", line 6: x range is invalid
Edit2:
The solution:
plot "< awk '{if((stat[$5]++)%10==0) print}' ~/Desktop/pointt.txt" using 1:2:($3-$1):($4-$2):5 with vectors palette nohead notitle
In order to achieve this, one has to use (most likely) some kind of preprocessing of the input. Fortunately, Gnuplot allows to plot a result of a command in a straightforward way. In your case, one could modify the plot command as:
plot "< gawk '{if((stat[$5]++)%10==0) print;}' ~/Desktop/pointcolor.txt" ...
Here, the id of the row (based on the fifth column) is stored in an array stat. If this id is divisible by 10, the row is printed and thus consequently plotted (i.e., other input rows are discarded).
Related
I have the following gnu plot:
# automobile_input.txt
#
set term png
set output "automobile.png"
#
# Fields in each record are separated by commas.
#
set datafile separator ","
set title "Price versus Curb weight"
set xlabel "Price in dollars"
set ylabel "Curb weight in pounds"
set grid
plot 'x' using 1:2
quit
x is a file containing numbers such as
1,2
0.5,4
etc.
I would like to make a few changes to this plot.
At the top of the plot there is "x using 1:2" and I would like to remove that.
Finally, the most important thing: I would like to add another file, y, in the same format, which will be also plotted on the same plot, only with a different sign and color (instead of pluses in red), for example, blue triangles. I would rather also the pluses be circles.
Omit the data series title by using notitle in your plot line. Adding another curve would be done like this:
plot 'x' using 1:2 notitle, \
'y' using 1:2 notitle
The data series points will adjust automagically. To manually specify the format, you might plot with something like this:
plot 'x' using 1:2 with points pointtype 6 linecolor rgb 'red' title "Data X", \
'y' using 1:2 with points pointtype 8 linecolor rgb 'blue' title "Data Y"
You'll usually see scripts online that abbreviate these command to look like:
plot 'x' w p pt 6 lc rgb 'red' title "Data X", \
'y' w p pt 8 lc rgb 'blue' title "Data Y"
I am using Gnuplot to draw step functions from an input file:
plot 'myFile' using 1:2 with steps
I want to fill underneath the plot. something like
plot 'myFile' using 1:2 with filledcurves
But Gnuplot fill underneath the diagram by drawing a line between consecutive points.
How can I fill in underneath the step function?
Use the fillsteps plotting style, which is the same as steps, but the area between the curve and y=0 is filled:
set samples 11
set xrange [0:10]
plot '+' with fillsteps fs solid 0.3 noborder lt 1,\
'+' with steps lt 1 lw 4
I am getting data from a script stuff.pl and want to plot dynamically into one graph. So I am going to use a loop in Gnuplot (v4.6 patchlevel 3) which leads me to following problem:
Using the file TEST.gp:
xCol=2; yCol=3
set term x11 1
plot '< stuff.pl' u xCol:yCol
xCol=4; yCol=5
set term x11 1
set autoscale y
replot '< stuff.pl' u xCol:yCol
pause 1
and run it via gnuplot TEST.gp my graph isn't scaled proper. The plot is just showing the second graph (scales to its values).
If I use
plot '< stuff.pl' u 2:3
replot '< stuff.pl' u 4:5
, which should behave the same imo, the scaling works.
I am not understanding this behaviour.
The replot calls the previous plot command and then adds another plot. In the previous plot command the variables haven't been replaced. When the replot calls the previous plot command, the last values of xCol and yCol are used for both plots!
You can either use two different variables:
xCol1 = 2; yCol1 = 3
plot '< stuff.pl' u xCol1:yCol1
xCol2 = 4; yCol2 = 5
replot '< stuff.pl' u xCol2:yCol2
or you can use macros, which are replaced
set macros
cols='2:3'
plot '< stuff.pl' u #cols
cols='4:5'
replot '< stuff.pl' u #cols
Creating a normiles stack diagram works perfectly fine, now I want to use a different color for the boxes fitting in a specific range.
For this, I adopted the following:
set palette maxcolors 2
set palette defined ( 0 '#C0504D', 1 '#00B059')
plot dataFileCity using (rounded(stringcolumn(1) eq city ? $2 : NaN)):
(100 / (bin_width * STATS_records)):($2 > 1300 ? 0 : 1)
smooth frequency with boxes palette
If column 2 has a value highter than 1300 I would like to have a different color.
Which is based on:
Normalized histograms in gnuplot with added function plot
And
Color bars in different colors for some specific values in Gnuplot
However, I am afriad that the smooth frequency makes the thing not work. How can I pass the value such that is creates the a different color?
I know this is nearly 8 years old, but I had the same issue and based on Christoph's comment above I was able to figure out a way.
Below is the graph I wanted:
However selecting certain rows only by way of the ternary and NaN does not play nice with smooth freq, and my histogram was wrong (seemed bins were drawn over one another and frequencies weren't as high as they should've been).
This did not work:
plot \
'filename' using ($1 >= 0 ? $1 : NaN) notitle smooth freq with boxes fillcolor rgb "green", \
'filename' using ($1 < 0 ? $1 : NaN) notitle smooth freq with boxes fillcolor rgb "red"
In the manual for gnuplot 5.4.2, this section describes an experimental feature which, combined with set table, allowed me to achieve the above graph.
[EXPERIMENTAL] To select only a subset of the data points for tabulation you can provide an input filter condition (if ) at the end of the command. Note that the input filter may reference data columns that are not part of the output. This feature may change substantially before appearing in a released version of gnuplot.
plot <file> using 1:2:($4+$5) with table if (strcol(3) eq "Red")
-- p207 gnuplot v5.4.2 manual
So the approach is:
Use set table $my_data_block_green to set the next plot command to output to the $my_data_block_green data block. We'll create one data block for each colour and this is the first.
Use plot <file> with table if (<condition_for_green>) to write to the green data block only rows matching <condition_for_green>.
Use set table $my_data_block_red (as in point 1).
Use plot <file> with table if (<condition_for_red>) to write to the red data block only rows matching <condition_for_red>.
Cease writing plot commands to tables with unset table.
Plot as normal, referencing the data blocks instead of <file>.
Relevant code (not the full code for graph above):
set table $db1
plot <filename> using 7:(1) with table if ($7 >= 0)
set table $db2
plot <filename> using 7:(1) with table if ($7 < 0)
unset table
plot \
'$db1' using $1:.. .. fillcolor rgb "green", \
'$db2' using $1:.. .. fillcolor rgb "red"
Hope that saves someone a few mins.
Adding to #TKF's answer... There is no need to split the smooth freq data into two tables.
Instead, plot it into one table and set the color via lc rgb variable and by defining an appropriate function.
The following example works for gnuplot>=5.2, with some modifications also with earlier versions.
Code:
### histogram with split colors
reset session
# create some random test data
set print $Data
do for [i=1:2000] {
print sprintf("%g",int(invnorm(rand(0))*100))
}
set print
stats $Data u 1 nooutput
xmin = STATS_min
xmax = STATS_max
N = 20
myWidth = (xmax-xmin)/N
bin(col) = myWidth*floor(column(col)/myWidth)+myWidth/2.
set boxwidth myWidth
set key noautotitle
set style fill solid 0.3
set grid x,y
set table $Histo
plot $Data u (bin(1)) smooth freq
unset table
myColor(col) = column(col)<0 ? 0xff0000 : 0x00cc00
plot $Histo u 1:2:(myColor(1)) w boxes lc rgb var
### end of code
Result:
How do you plot bar charts in gnuplot with text labels?
Simple bar graph:
set boxwidth 0.5
set style fill solid
plot "data.dat" using 1:3:xtic(2) with boxes
data.dat:
0 label 100
1 label2 450
2 "bar label" 75
If you want to style your bars differently, you can do something like:
set style line 1 lc rgb "red"
set style line 2 lc rgb "blue"
set style fill solid
set boxwidth 0.5
plot "data.dat" every ::0::0 using 1:3:xtic(2) with boxes ls 1, \
"data.dat" every ::1::2 using 1:3:xtic(2) with boxes ls 2
If you want to do multiple bars for each entry:
data.dat:
0 5
0.5 6
1.5 3
2 7
3 8
3.5 1
gnuplot:
set xtics ("label" 0.25, "label2" 1.75, "bar label" 3.25,)
set boxwidth 0.5
set style fill solid
plot 'data.dat' every 2 using 1:2 with boxes ls 1,\
'data.dat' every 2::1 using 1:2 with boxes ls 2
If you want to be tricky and use some neat gnuplot tricks:
Gnuplot has psuedo-columns that can be used as the index to color:
plot 'data.dat' using 1:2:0 with boxes lc variable
Further you can use a function to pick the colors you want:
mycolor(x) = ((x*11244898) + 2851770)
plot 'data.dat' using 1:2:(mycolor($0)) with boxes lc rgb variable
Note: you will have to add a couple other basic commands to get the same effect as the sample images.
plot "data.dat" using 2: xtic(1) with histogram
Here data.dat contains data of the form
title 1
title2 3
"long title" 5
I would just like to expand upon the top answer, which uses GNUPlot to create a bar graph, for absolute beginners because I read the answer and was still confused from the deluge of syntax.
We begin by writing a text file of GNUplot commands. Lets call it commands.txt:
set term png
set output "graph.png"
set boxwidth 0.5
set style fill solid
plot "data.dat" using 1:3:xtic(2) with boxes
set term png will set GNUplot to output a .png file and set output "graph.png" is the name of the file it will output to.
The next two lines are rather self explanatory. The fifth line contains a lot of syntax.
plot "data.dat" using 1:3:xtic(2) with boxes
"data.dat" is the data file we are operating on. 1:3 indicates we will be using column 1 of data.dat for the x-coordinates and column 3 of data.dat for the y-coordinates. xtic() is a function that is responsible for numbering/labeling the x-axis. xtic(2), therefore, indicates that we will be using column 2 of data.dat for labels.
"data.dat" looks like this:
0 label 100
1 label2 450
2 "bar label" 75
To plot the graph, enter gnuplot commands.txt in terminal.
I recommend Derek Bruening's bar graph generator Perl script. Available at http://www.burningcutlery.com/derek/bargraph/
You can directly use the style histograms provide by gnuplot. This is an example if you have two file in output:
set style data histograms
set style fill solid
set boxwidth 0.5
plot "file1.dat" using 5 title "Total1" lt rgb "#406090",\
"file2.dat" using 5 title "Total2" lt rgb "#40FF00"