I make this simple cumulative and histogram plot of a uniform random distribution of real numbers (n=1000):
http://www.filedropper.com/random1_1: random1.dat
And the macro is:
unset key
clear
reset
n=120 #number of intervals
max=4. #max value
min=1. #min value
width=(max-min)/n #interval width
#function used to map a value to the intervals
bin(x,width)=width*floor(x/width)+width/2.0 # cosi viene centrato in mezzo
set xtics min,(max-min)/10,max
set boxwidth width
set style fill solid 0.5 border
set ylabel 'Frequency'
set y2label 'Cumulative frequency'
set y2tics 0,100,1000
set ytics nomirror
set xrange [0.9:4.1]
set yrange [0:25]
set terminal pngcairo size 800,500 enhanced font 'Verdana,14'
set output "testCum.png"
plot 'random1.dat' using (bin($1,width)):(1.0) smooth frequency with boxes title 'histogram',\
'' using (bin($1,width)):(1.0) smooth cumulative axis x1y2 w l lt 2 lw 2 lc rgb 'green' title 'cumul'
Now the output.png is:
How I can tell to Gnuplot I want to take not only the cumulative plot but also the numbers coming from it saved in a particular file.dat ?
You can save the data after applying smooth with set table .... In the simplest case, if you need only the cumulative data, just use:
set table 'random1-smoothed.dat'
plot 'random1.dat' using (bin($1,width)):(1.0) smooth cumulative
unset table
For better inclusion in your script you could also wrap the whole, existing plot command in set table:
...
set table 'random1-smoothed.dat'
plot 'random1.dat' using (bin($1,width)):(1.0) smooth frequency with boxes title 'histogram',\
'' using (bin($1,width)):(1.0) smooth cumulative axis x1y2 w l lt 2 lw 2 lc rgb 'green' title 'cumul'
unset table
set terminal pngcairo size 800,500 enhanced font 'Verdana,14'
set output "testCum.png"
replot
Related
My plot has logx axis. So I want to show the tic marks. But due to the size of the tic values they overlap. For example:
But if I reduce the tic marks by the command
set xtics (1.0e-7,1.0e-5, 5.0e-4);
It becomes like
I want those logarithmic tics(like the first picture) but only three values written(like the second picture) without compromising the size. Is it possible?
As I understand you basically want keep all the auto tics and grid lines, but remove 1e-6 and 1e-4 tic labels and add a 5e-4 tic label.
You could do it the following way. Check help xtics and help format specifiers.
Code:
### set custom tics
reset session
set xrang[1e-7:5e-4]
set logscale x
set grid x,y
set format x "%.0t x 10^{%T}"
set rmargin 5
set xtics add ('' 1e-6, '' 1e-4, 5e-4)
plot x
### end of code
Result:
I want to make a PNG image of my plot but would like to overlay this on a map. To do this we need the boundary coordinates and a simple png without any axes or tic marks ending up in the image. I tried turning of the ticmarks by unset xtics / ytics but then the value of GPVAL_X_MAX becomes the same as GPVAL_DATA_X_MAX. Is there any way to find the min/max values of the plot without the using the tics?
Example:
plot [-10:10] sin(x),atan(x),cos(atan(x));
show variables all;
Gives GPVAL_Y_MIN = -1.50, GPVAL_Y_MIN=-1.47...
Whereas:
unset xtics;
unset ytics;
plot [-10:10] sin(x),atan(x),cos(atan(x));
show variables all;
gives GPVAL_Y_MIN = -1.47, GPVAL_Y_MIN=-1.47...
edit:
Fixed, I finally just removed all style/layout of the tics and scaled their size down to zero. This way GPVAL_X_MIN etc retain the value of the actual boundaries instead of the min/max vals of the dataset.
A simple brute force solution would be to plot twice. The first time with tics enabled, just to store the value of the variables you're after, such as GPVAL_DATA_Y_MIN. You can then plot a second time with tics disabled.
set terminal pngcairo
set output 'plot.png'
plot [-10:10] sin(x),atan(x),cos(atan(x));
y_min = GPVAL_Y_MIN
data_y_min = GPVAL_DATA_Y_MIN
unset xtics
unset ytics
set output 'plot.png'
replot
print sprintf("GPVAL_Y_MIN = %f", y_min)
print sprintf("GPVAL_DATA_Y_MIN = %f", data_y_min)
Output:
GPVAL_Y_MIN = -1.500000
GPVAL_DATA_Y_MIN = -1.471128
How can I add an X grid with different spacing than the tics? My plot is a histogram showing # of patents (of a certain type) granted per year, and the year range is large (1807-1971). I'd like to tic/label each year but only add X grid lines every decade (and also use a different color for the matching decade labels).
I've been searching for an answer and trying things for hours and getting nowhere. Are either of these possible?
My current plot (with no X grid) looks like:
And the script is:
set style data histograms
set style histogram gap 1
set style fill solid
set title "Number of Prism Glass Patents Granted" font "fixed, 24" offset 0,-0.9
set xlabel "Year" font "fixed,18" offset 0,0.8
set nokey
set xtics out nomirror rotate font "fixed, 8" offset 0,0.4
set grid y
plot 'frequency.dat' using 2:xtic(1) linecolor 'blue'
I assume your data consists out of two columns: Year and number of patents.
Why do you use xitc(1), is it necessary to label every single year?
What about using minor and major xtics? I would use plotstyle with boxes.
Code:
### major and minor xtics
reset session
# generate some random data
set print $Data
do for [i=1807:1971] {
print sprintf("%d %d", i, int(rand(0)*100))
}
set print
set xlabel "Year"
set xtics out nomirror
unset x2tics
set xtics 10
set mxtics 10
set grid ytics
set grid xtics
set boxwidth 0.5
plot $Data u 1:2 with boxes fill solid 1.0 lc rgb "blue" notitle
### end of code
Result:
Addition:
Another version with grid every 10 years and label with different color. Labels are only shown when number of patents>0. Instead of using xtics it is done by plotting with labels.
Code:
### major and minor xtics
reset session
set term pngcairo size 1600,360
set output "tbGrid.png"
xmin = 1807
xmax = 1971
# generate some random data
set print $Data
do for [i=xmin:xmax] {
print sprintf("%d %d", i, int(rand(0)+0.4)*(int(rand(0)*100)))
}
set print
set xlabel "Year" offset 0,-1.5
set xrange[xmin-1:xmax+1]
set xtics 10 format "" out nomirror
set mxtics 10
set bmargin 5
set grid ytics
set grid xtics
set boxwidth 0.5
myTic(n,p) = p==0 ? "" : sprintf("%d",n)
myColor(n) = int(n)%10==0 ? 0xff0000 : 0x000000
plot $Data u 1:2 with boxes fill solid 1.0 lc rgb "blue" notitle, \
'' u 1:(0):(myTic($1,$2)):(myColor($1)) with labels \
tc rgb var rotate offset 0,-1.5 font ",8" notitle
set output
### end of code
Result:
The grid for each axis is generated from the tics for that same axis, so yes they always match. However if your plot uses only the x1 axis, you could define the range and tics for the x2 axis also and turn on the grid only for x2 and not for x1.
Recent gnuplot versions have a command set link x2 that ensures the x1 and x2 axes agree on the range and scale. If your version does not support this you can still set them to match explicitly:
set xrange [min:max]
set x2range [min:max]
set xtics <whatever> # these will label the actual plot
set x2tics <something else> # these will be used only for grid lines
set x2tics scale 0.0 format "" # show no x2 tics or labels on the plot
set grid x2 nox
plot ...
Thank you both-- here's my final plot:
...and its script:
# histogram of # of prism glass patents granted per year
###
set term png size 1800,600
xmin = 1807
xmax = 1971
set title sprintf("Prism Glass Patents Granted %d-%d", xmin, xmax) \
font "fixed, 24" offset 0,-0.5
set xlabel "Year" font "fixed,24" offset 0,-2
# x tic for each year
set xrange [xmin-1:xmax+1]
set xtics 1 out nomirror format ""
# x2 tic and gridline for each decade
set x2tics 1800,10,1970
set x2tics out font "fixed, 12" offset 0,-0.6
set grid x2 nox
set grid y # y axis is count
set boxwidth 0.5
set bmargin 5
set nokey
myTic(y,n) = n==0 ? "" : sprintf("%d",y) # only label year if count>0
myColor(y) = y==1897 ? 0x0000FF : 0x000000 # highlight 1897 (biggest year)
plot 'frequency.dat' using 1:2 with boxes fill solid lc rgb "blue", \
'' using 1:(0):(myTic($1,$2)):(myColor($1)) with labels \
tc rgb var rotate offset 0.1,-1.2 font "fixed,8"
I want to plot multiple files using a Gnuplot script.
However, I'm having some trouble to make it perfect.
My actual plots are something like that:
However, the linepoints plot is touching both the x-axis and the y-axis.
Therefore, I want to add extra space on xrange and yrange and get
which does not touch my axis.
I could to it manually using set xrange and set yrange.
However, I need to plot more than 100 different files, and it will be very time-consuming to do so.
There is some manner to automatically increase the size of xrange and yrange by some units?
My Gnuplot code is below.
#!/usr/bin/env gnuplot
set terminal epslatex size 7.5,3 standalone
set output 'pareto.tex'
set style fill solid 0.8
set ytics nomirror
set xtics nomirror
set grid lc rgb "#F2F2F2"
set xlabel 'Z_1'
set ylabel 'Z_2'
set xrange [170:215]
set yrange [7:40]
set style line 1 lt rgb "#000000" lw 12 pt 7 pointsize 3
plot "../exact.dat" using 1:2 title '$aug\,\epsilon$-CM' with linespoints ls 1
unset output
set output # finish the current output file
system('pdflatex --interaction=batchmode pareto.tex')
unset terminal
system
The command you are looking for is set offset. See the documentation for a full description.
Example:
set multiplot layout 3,1
# Default placement
plot 'silver.dat' with lines
# Additional whitespace combined with auto-extenstion to nearest ticmark
set offset 20,20,20,20
replot
# Additional whitespace with no auto-extension to nearest ticmark
set xrange [*:*] noextend; set yrange [*:*] noextend
replot
unset multiplot
Good day,
I need to create a group bar chart graph with different scales.
Consider following data example:
Metric A B
Group1 10 1500
Group2 20 4000
I am using this answer and this code:
reset
set style histogram cluster gap 1
set style data histograms
set style fill solid 1.00 border
set yrange [0:]
set ytics nomirror
set y2range [0:]
set y2tics
set key right autotitle columnheader
plot 'file.dat' u 2 every ::::0, '' u 3:xtic(1) every ::::0,\
newhistogram lt 1 at 1,\
'file.dat' u 2 every ::1::1 axes x1y1, '' u 3:xtic(1) every ::1::1 axes x1y2
The code above creates this plot:
However, what I need is:
Left Y scale to be [0:20]
All numbers from A to be plotted according to left Y scale
All numbers from B to be plotted according to right Y scale
If possible, to put labels on both left and right Y scale.
If possible, to have only one pair of A and B in the legend.
This way, the Violet color Bars will be much higher, and dependent only on column A number range.
Thank you very much!
try using 2 axis using y2:
set ytics nomirror
set y2tics
set yrange [0:20]
set y2range [0:]
set key right
label = "MyLabel"
set ylabel label
set y2label label
set style data histograms
plot 'histplot.dat' using 2 ti col axis x1y1, '' u 3:xticlabels(1) ti col axis x1y2