2

I have to "reinvent" a diagram like the following:

Klima

My problem is, the "filledcurves" option does not work correctly, if I use the different scaled y-axis.

set y2tics textcolor rgb "black" 
set ytics nomirror
set yrange [0:80]
set y2range [0:180]
set key off
set grid dashtype 5 # auch dt ".-." möglich
plot "klima_flach.txt" using 1:3:4 with filledcurves x1,\
     "" using 1:4 with lines axis x1y2,\
     "" using 1:3:xtic(2) with lines axis x1y1

The used data is the following:

0 0 11 50  
1 J 10 70  
2 F 11 42  
3 M 12 50  
4 A 15 50  
5 M 18 20  
6 J 22 10  
7 J 25 1  
8 A 25 20  
9 S 23 40  
10 O 20 80  
11 N 25 70  
12 D 11 60  

Any ideas, how I can get this problem solved?

By the way: A pattern as in the original diagram... possible or not?

thebluephantom
  • 16,458
  • 8
  • 40
  • 83

2 Answers2

0

The filledcurves doesn't have an option for choosing different axes for the y-values in column two and three. But you are in the lucky situation, that you have fixed y-ranges. So you can define a scaling function for one of the columns:

set y2tics textcolor rgb "black" 
set ytics nomirror
set yrange [0:80]
set y2range [0:180]
scale = 80.0/180.0
set key off
set grid dashtype 5 # auch dt ".-." möglich
plot "klima_flach.txt" using 1:3:(scale*$4) with filledcurves,\
     "" using 1:4 with lines axis x1y2,\
     "" using 1:3:xtic(2) with lines axis x1y1
Christoph
  • 47,569
  • 8
  • 87
  • 187
0

@Wolfgang Höfer, the scaling between the axes in such type of Walter/Lieth-climate diagrams is 2. Hence, your y-range should be [0:90] and hence scaling factor 90./180. Nevertheless, I assume @Christoph's answer solved your problem.

To your last question: a pattern as in your picture, i.e. a vertical hatch pattern? That's what I asked here (Hatch patterns in gnuplot) recently. Apparently, it's seems not possible in gnuplot.

Some time ago, I also "struggled" with climate diagrams, i.e. with filledcurves and even nonlinear axes. I would like to provide the code which I ended up. Maybe it will be useful to you or to others to draw such climate diagrams with gnuplot. If you are reading from a file, replace $DataIn with your filename. Suggestions and improvements are welcome.

# Walter/Lieth climate diagram with nonlinear axis
reset session
set encoding "utf8"

$DataIn <<EOD
# Mumbai/India, 18°54'N/72°49'E, 11 m
# No. Month Temperature Precipitation
1 January 23.9 3
2 February 23.9 3
3 March 26.1 3
4 April 28.1 2
5 May 29.7 18
6 June 28.9 485
7 July 27.2 617
8 August 27.0 340
9 September 27.0 264
10 October 28.1 64
11 November 27.2 13
12 December 25.6 3
EOD

# in order to be flexible for different input files 
ColTemp = 3     # col# temperature
ColPrec = 4     # col# precipitation

# get location label from first commented row starting after '# '
set datafile commentschar ""   # set the comment char to none
set datafile separator "\n"    # data will be a full line
set table $Dummy               # plot following data to a dummy table
    # plots only first line 'every ::0::0' as string to the dummy table
    # and assigns this line starting after the 3rd character to variable 'Location'
    plot $DataIn u (Location = stringcolumn(1)[3:]) every ::0::0 with table
unset table                    # stop plotting to table
set datafile commentschar "#"       # restore default commentschar
set datafile separator whitespace   # restore default separator
set label 1 at graph 0.02,0.96 Location font ",10"    # put label on graph

# set periodic boundaries, i.e. add lines of Dec and Jan again
# independent of the input format $DataIn, column1 of $Data will be the number of month
set datafile separator "\n"
set table $Data
    plot $DataIn u (0):(stringcolumn(1)) every ::11::11 with table
    plot $DataIn u ($0+1):(stringcolumn(1)) with table
    plot $DataIn u (13):(stringcolumn(1)) every ::0::0 with table
unset table
set datafile separator whitespace
# print $Data

# settings for nonlinear scale
ScaleChangeAt = 100.
ScaleChangeFactor = 5.
f1(y) = (y<=ScaleChangeAt) ? y : ((y - ScaleChangeAt)/ScaleChangeFactor + ScaleChangeAt)
f2(y) = (y<=ScaleChangeAt) ? y : ((y - ScaleChangeAt)*ScaleChangeFactor + ScaleChangeAt)
f3(y) = f1(y)/2.   # relation between axes y and y2; standard for Walter/Lieth climate diagrams
set nonlinear y2 via f1(y) inverse f2(y)


# settings for x-axis
set xrange[0.5:12.5]
set xtics 1 scale 0,1
set mxtics 2
set grid mxtics
# create months labels from local settings 
do for [i=1:12] {
    set xtics add (strftime("%b",strptime("%m",sprintf("%g",i))) i) 
}

# settings for y- and y2-axes
stats [*:*] $DataIn u ColTemp:ColPrec nooutput
Round(m,n) = int(m/n)*n + sgn(m)*n
Ymin = STATS_min_x > 0 ? 0 : Round(STATS_min_x,10)
Ymax = 50
Y2min = Ymin < 0 ? f1(Ymin)*2 : 0
Y2max = Round(STATS_max_y,10**int(log(STATS_max_y)/log(10))) # round to next 10 or 100
# print Ymin, Ymax, Y2min, Y2max

# y-axis
set ylabel "Temperature / °C" tc rgb "red"
set yrange [Ymin:f3(Y2max)]  # h(Y2max)]
set ytics 10 nomirror tc rgb "red"
# "manual" setting of ytics, up to 50°C
set ytics ("0" 0)
do for [i=Ymin:50:10] {
    set ytics add (sprintf("%g",i) i)
}

# settings for y2-axis
set y2label "Precipitation / mm" tc rgb "blue"
set y2range [Y2min:Y2max]
# "manual" setting of y2tics
set y2tics nomirror tc rgb "blue"
set y2tics ("0" 0)
set grid y2tics
do for [i=20:ScaleChangeAt:20] {
    set y2tics add (sprintf("%g",i) i)
}
do for [i=ScaleChangeAt:Y2max:20*ScaleChangeFactor] {
    set y2tics add (sprintf("%g",i) i)
}

plot \
    $Data u 1:ColTemp+1:(f3(column(ColPrec+1))) axis x1y1 w filledcurves above lc rgb "yellow" not,\
    '' u 1:ColTemp+1:(f3(column(ColPrec+1))) axis x1y1 w filledcurves below fs pattern 4 fc rgb "blue" not,\
    '' u 1:(f3(ScaleChangeAt)):(f3(column(ColPrec+1))) axis x1y1 w filledcurves below fs solid 1.0 fc rgb "blue" not,\
    '' u 1:ColTemp+1 w l lw 2 lc rgb "red" not,\
    '' u 1:ColPrec+1 axes x1y2 w l lw 2 lc rgb "blue" not
### end of code

which results in:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
  • perfect, thanks ... but still a long way to understand every line :) – Wolfgang Höfer Jan 27 '19 at 11:18
  • you're welcome. Let me know if some lines need more or special explanations... as I said there might be room for improvements or simplifications. – theozh Jan 27 '19 at 19:49
  • It's a great way to get content out of comments, but I have a problem with the part plot $DataIn u (Location = stringcolumn(1)[3:],$1) .... Ok - you create the variable "Location", and assing the characters 3 - * of the first stringcolumn to ist. The brackets, ok, because you do not directly address a column. But what about the $1 inside the bracket? – Wolfgang Höfer Feb 04 '19 at 08:51
  • it assigns the first line starting from the 3rd character to the variable `Location`. You actually don't need `$1`. The following also works: `plot $DataIn u (Location = stringcolumn(1)[3:]) every ::0::0 with table`. Added explanations to the code. – theozh Feb 04 '19 at 09:30
  • Thanks! But another question: You change the separator from whitespace to \n. For me that means, that every line in the file becomes a column. Then the content of the file is one long row with many columns. But in this case ::11::11 makes no sense -- but it works. That means, I am wrong -- where and why? – Wolfgang Höfer Feb 05 '19 at 08:32
  • good point :-) You're right. It's not consistent. I was just happy that it worked like this. I can only guess that for gnuplot a line is a line and stays a line. That's why you apparently get one column with n rows instead of one row with n columns. But for the detailed background you have to ask the gnuplot developers. – theozh Feb 05 '19 at 12:05
  • or maybe `every ...` is just looking for new lines `\n` independent of applying `set datafile separator "\n"`. So, gnuplot cannot (or does not) differentiate between end of line `\n` and separator `\n`. Hence, for gnuplot it will look like one column. Just speculating... – theozh Feb 05 '19 at 13:55
  • Ha, I really found something you don't know :) That really makes me feel less small :). Thanks nevertheless :) – Wolfgang Höfer Feb 05 '19 at 16:53
  • glad you found something ;-). Well, there are much more experienced gnuplot users than me out here. I hope you learned something, so you eventually might want to mark this answer as useful or even as a solution to your question ;-). – theozh Feb 05 '19 at 18:57