Reputation: 175
I've got a data file with daily values for the amount of rain in the 4th column, for each day of the year. I'd like to plot a bar graph with each month in the x-axis, and the total monthly amount of rain in the y-axis: that is, to plot "January" (with %B or %b format) vs the sum of the 31 first values of the 4th column. Then to plot "February" vs the sum of the next 28 values of the 4th column, and so on. Do you know how to do that with gnuplot ? Besides, is it possible to write the numerical value of the monthly amounts of rain, on top of each bar ?
Upvotes: 1
Views: 184
Reputation: 26200
I can imagine and understand that for a gnuplot beginner it will not be easy to find and combine the necessary commands to realize your task. If you do a search you will most probably not find exactly your case, but there should be very similar questions and examples around. The key search would be "creating a histogram".
Check help smooth frequency
, help strftime
, help strptime
, help datablocks
, help table
, basically for every command or keyword there should be a help entry.
The following example is one way to achieve what you are asking for. It is basically binning data, like creating a histogram. Here, your bins will be the months in the following numerical format, e.g. 202109
, 202110
, 202111
, 202112
, 202201
, etc.
In the example below, some random test data (mm of rain per day) will be created in order to illustrate the result with a graph.
Example data in $Data
:
2021-12-01 66
2021-12-02 0
2021-12-03 0
2021-12-04 17
2021-12-05 52
Plot your data into a datablock $Monthly
using the option smooth frequency
. It will sum up all values per month.
The result in $Monthly
will be something like this:
202107 368
202108 622
202109 557
202110 361
202111 628
I hope you can adapt the script to your data and needs.
Edit: the previous version of the code used the plotting style with boxes
for the monthly plot. However, this style is centering the box at the beginning of the month, which is undesired here (especially when plotting together with the daily rain). The modified code is using the plotting style with boxxyerror
which plots the boxes from the beginning of the month to the beginning of the next month. Check help boxes
and help boxxyerror
.
Script:
### sum up daily rainfall per month
reset session
fmt = "%Y-%m-%d" # time format
day = 86400 # 1 day = 86400 secs
# create some random test data
set print $Data
t0 = strptime(fmt,"2021-01-01")
do for [i=0:364] {
print sprintf("%s %.0f", strftime(fmt,t0+i*day), int(rand(0)+0.2)*rand(0)*100)
}
set print
set table $Monthly
plot $Data u (int(strftime("%Y%m",timecolumn(1,fmt)))):2 smooth freq
unset table
set format x "%Y\n%b" timedate
set key out top center
set xrange noextend
set offset graph 0.01, graph 0.01, graph 0.01, 0
set grid x,y
set xtics out
NextMonth(t) = strptime("%Y%m",sprintf("%04d%02d",tm_year(t),tm_mon(t)+2))
set multiplot layout 2,1
set style fill solid 1.0 noborder
plot $Data u (t0=timecolumn(1,fmt)):2:(t0):(t0+day):(0):2 \
w boxxy lc "blue" title "Daily rain / mm"
set style fill solid 0.3 border
set xrange[GPVAL_X_MIN:GPVAL_X_MAX] # set same xrange as previous plot
plot $Monthly u (t0=timecolumn(1,"%Y%m")):2:(t0):(NextMonth(t0)):(0):2 \
w boxxy lc "blue" title "Monthly rain / mm"
unset multiplot
### end of script
Result:
Upvotes: 2