How to use gnuplot to plot history graph with labels - gnuplot

I have two small datafiles
one
11 365.4
12 659.2
and one
11 432.1
12 882.4
I try to plot those as a histogram with labels
gnuplot <<EOF
set output 'house-energy.png'
set terminal png size 800,400 font "Arial,10"
set boxwidth 0.7 relative
set grid ytics linestyle 0
set style fill solid 0.20 border
set style data histogram
#set style histogram columnstacked
#set style histogram rowstacked
set title "Energy"
set xlabel "Month"
set ylabel "kWh"
set yrange [0:2000]
plot 'file1.dat' u 2: xtic(1) with histogram lc rgb "#0045FF" title "Energy house total", \
'' using 1:(\$2):(\$2) with labels notitle font ",10" , \
'file2.dat' u 2: xtic(1) with histogram lc rgb "#004500" title "Labb", \
'' using 1:(\$2):(\$2) with labels notitle font ",10"
EOF
But the labels are way off
Image showing plot
// GH

The histogram plotting style is implicitly using the pseudocolumn 0 (check help pseudocolumns) as x-coordinate. So, you have to place your label at column(0) or ($0) with some x-offset, e.g. ($0-0.15) in the one and other direction. And some offset in y-direction, e.g. via offset 0,0.7.
Script:
### histogram with labels
reset session
$Data1 <<EOD
11 365.4
12 659.2
EOD
$Data2 <<EOD
11 432.1
12 882.4
EOD
set boxwidth 0.7 relative
set grid ytics linestyle 0
set style fill solid 0.20 border
set style data histogram
set title "Energy"
set xlabel "Month"
set ylabel "kWh"
set yrange [0:2000]
set key noautotitle
plot $Data1 u 2:xtic(1) w histogram lc rgb "#0045FF" title "Energy house total", \
'' u ($0-0.15):2:2 w labels font ",10" offset 0,0.7, \
$Data2 u 2 w histogram lc rgb "#004500" title "Labb", \
'' u ($0+0.15):2:2 w labels font ",10" offset 0,0.7
### end of script
Result:

Related

How to plot several Y axis in the left side using gnuplot

I am plotting same plot as from here How to plot multiple y-axes?. But also i would like them to look like in the image below. With additional lines and tics, how can i do it?
As mentioned in the answer you linked, you can plot many plots on top of each other in the multiplot environment.
So, here you are plotting 6 plots on top of each other
plot 1, 2, 3 for the data/functions
plot 4, 5, 6 for the colored axes
and plot 6 as well for the key on top of everything
In order to get the same scale for the plot and the "external" axes I would recommend to store the ranges and step sizes in the variables y10,y11,y1d, ...., such that you have to change the values only at one location.
Script:
reset session
set key opaque box noautotitle
set multiplot
set margins screen 0.3, screen 0.95, screen 0.95, screen 0.1 # l,r,t,b
# first plot
set xrange[0:10]
set xtics 2
y10 = -1.2
y11 = 1.2
y1d = 0.4
set yrange [y10:y11]
set ytics y1d
set format y '' # no ytic labels
set grid x,y
plot sin(x) lc "red"
# second plot
set border 0 # no border
unset tics # no tics
unset grid # no grid
y20 = -3.0
y21 = 3.0
y2d = 1.0
set yrange [y20:y21]
plot 3*cos(x) lc "green"
# third plot
y30 = -1.5
y31 = 1.5
y3d = 0.5
set yrange [y30:y31]
plot 3*sin(x)*cos(x) lc "blue"
# plot for axis 1
set lmargin screen 0.27
set border 2 lc "red" lw 2
set ylabel "Voltage" tc "red" offset 1.5,0
set yrange [y10:y11]
set format y
set ytics y1d nomirror
plot NaN
# plot for axis 2
set lmargin screen 0.17
set border 2 lc "green" lw 2
set ylabel "Current" tc "green" offset 0.5,0
set yrange [y20:y21]
set ytics y2d
plot NaN
# plot for axis 3 and key
set lmargin screen 0.08
set border 2 lc "blue" lw 2
set ylabel "Power" tc "blue" offset 1,0
set yrange [y30:y31]
set ytics y3d
plot NaN w l lc "red" ti "sin(x)", \
NaN w l lc "green" ti "3*cos(x)", \
NaN w l lc "blue" ti "3*sin(x)*cos(x)"
unset multiplot
### end of script
Result:

How to make xtics label color variable in box plot in GNUPlot?

This is my attempt at making the xtics label color match the line color in a box plot in gnuplot:
$DATA << EOD
1 1
2 2
3 3
EOD
set linetype 1 linecolor rgb "red"
set linetype 2 linecolor rgb "green"
set linetype 3 linecolor rgb "blue"
set key off
set boxwidth 0.5
set style fill solid 0.5
set xrange [0:4]
set yrange [0:4]
set xtics ("1" 1) textcolor rgb "red"
set xtics add ("2" 2) textcolor rgb "green"
set xtics add ("3" 3) textcolor rgb "blue"
plot $DATA using 1:2:1 with boxes linecolor variable
But it does not work:
Any idea? Thanks!
I'm not sure whether you can set xtics individually in different colors. So, the following solution sets the xtics as empty lines and you plot your xtics with labels with a certain offset. The disadvantage is that you have to set the y-position here: (0) with offset 0,-1. I hope there are better solutions.
Code:
### "xtic labels" in different colors
reset session
$Data << EOD
1 1
2 2
3 3
EOD
set linetype 1 lc "red"
set linetype 2 lc "green"
set linetype 3 lc "blue"
set key off
set boxwidth 0.5
set style fill solid 0.5
set xrange [0:4]
set yrange [0:4]
set format x "\n" # xtic label empty line
plot $Data u 1:2:1 w boxes lc var, \
'' u 1:(0):1:1 w labels tc var offset 0,-1
### end of code
Alternatively, you could use an offset relative to the graph:
plot $Data u 1:2:1 w boxes lc var, \
'' u 1:(0):1:1 w labels tc var offset 0, graph -0.05
Result:

How to manage tick lable and axis-lable in gnu multiplot

I have two files each is having five columns where 1st will be the x-axis in both the data.
I want to make a multiplot 2 2 in GNU with zero spacing.
I could manage many things but I am could not fix below problems:
1. How to place the figure at the center of the page i.e. equal left right margin? I set the left, right, top and bottom margin but it is not working.
2. How to number the figure. For example, Figure 1 1 should be numbered as (a), figure 1 2 should be numbered as (b) and so on.
3. How to put tick lable and tick mark on the plot 1 2 and 2 2 on right side of the plot?
4. how to create a common title at the bottom of the figure representing the X-axis.
I have tried to make gnu multiplot layout but still it is not giving desired results. The script I used is mentioned below:
My code is
[![set terminal postscript eps enhanced size 20cm,15cm color solid lw 3 "Times-Roman" 24
reset
set lmargin screen 0.10
set rmargin screen 0.95
set bmargin screen 0.15
set tmargin screen 0.9
set mxtics 2
set mytics 2
set tics font "Times-bold, 50"
set output "absorption.pdf"
set multiplot layout 2,2 margin 0.2, 0.9, 0.1, 0.9 spacing 0.00, 0.00
set tics scale 1.2
set tics font "Times-bold, 26"
set key spacing 1.2
unset key
set xrange \[0:8.5\]
set yrange \[0:1\]
set xlabel ' '
set format x ""
set ylabel 'A11' font 'Times-bold, 26' offset 1,1,3
unset label
plot "data1.dat" u 1:($2/10**4) w l lw 3 lt 2 lc rgb "red" title "x-D", 'data1.dat' u 1:($3/10**4) w l lw 3 lc rgb "blue" title "z-D"
unset label
unset format x
unset key
set key inside center top # to adjust the legends position
set xrange \[0:8.5\]
set yrange \[0:1\]
set title ' '
set xlabel ' '
set xlabel ' '
set format x ""
set ylabel ' '
set format y " "
set key spacing 1.2
set ylabel 'A12' font 'Times-bold, 26' offset 1,0,3
plot "data1.dat" u 1:($4/10**2) w l lw 3 lt 2 lc rgb "red" title "x-D", 'data1.dat' u 1:($5/10**2) w l lw 3 lt 2 lc rgb "blue" title "Z-D"
unset label
unset format y
unset format x
unset key
set xrange \[0:8.5\]
set yrange \[0:1.08\]
set xlabel ' '
unset label
set ylabel 'A21' font 'Times-bold, 28'
unset label
plot "data2.dat" u 1:($2/10) w l lw 3 lt 2 lc rgb "red" title "x-dir", 'data2.dat' u 1:($4/10) w l lw 3 lt 2 lc rgb "blue" title "z-dir" ,\
unset label
unset format x
unset key
set xrange \[0:8.5\]
set yrange \[0:1.08\]
set title ' '
set format y ""
set xlabel 'X-12-scale' font 'Times-bold, 28'
set ylabel 'A22' font 'Times-bold, 28'
plot "data2.dat" u 1:($3/10) w l lw 3 lt 2 lc rgb "red" title "x-dir", 'data2.dat' u 1:($5/10) w l lw 3 lt 2 lc rgb "blue" title "z-dir"
unset label
unset format y
unset key
unset multiplot
set output][1]][1]
My data should be like what I want according to attached figure queries and hand marks.
Try this
reset
set encoding utf8
set terminal pngcairo size 750,500 font ",10"
set output "Multiplot_2x2.png"
set multiplot \
layout 2,2 rowsfirst \
title "{/:Bold=11 Multiplot 2×2}" \
margins screen 0.10,0.92,0.12,0.90 \
spacing screen 0.00,0.00
set link y2
# Gaussian fuction
f(x,a,b,c) = a*exp(-((x-b)/c)**2)
# Parameters to first one
a1 = 0.95
b1 = 4.00
c1 = 1.00
# Parameters to second one
a2 = 0.95
b2 = 5.00
c2 = 1.00
# Line style
set style line 1 lc "#e41a1c" # red
set style line 2 lc "#377eb8" # blue
# -----------------------------------------------
set xrange [0:10]
set yrange [0:1.0]
set xtics format ""
set ytics
set ylabel "y-label"
set label 1 "{/:Bold (a)}" at graph 0.05, 0.9
plot f(x,a1,b1,c1) w l ls 1 notitle, f(x,a2,b2,c2) w l ls 2 notitle
# -----------------------------------------------
unset ylabel
set ytics format ""
set y2tics format ""
set y2label "y2-label"
set label 1 "{/:Bold (b)}"
plot f(x,a1,b1,c1) w l ls 1 title "Your title 1", f(x,a2,b2,c2) w l ls 2 title "Your title 2"
# -----------------------------------------------
unset y2tics
unset y2label
set xtics 0,2,9 format "%g"
set ytics 0,0.2,0.9 format "%g"
set ylabel "y-label"
set label 1 "{/:Bold (c)}"
plot f(x,a1,b1,c1) w l ls 1 notitle, f(x,a2,b2,c2) w l ls 2 notitle
# -----------------------------------------------
unset ylabel
set xtics 0,2,10
set xlabel "common x-label" offset screen -0.20,0.0
set ytics format ""
set y2tics
set y2label "y2-label"
set label 1 "{/:Bold (d)}"
plot f(x,a1,b1,c1) w l ls 1 notitle, f(x,a2,b2,c2) w l ls 2 notitle
# -----------------------------------------------
Result

Can't get gnuplot to fill below line

I'm trying to get gnuplot 5.0 under OS X 10.10.3 to fill blue below the lower series in this plot. I've tried lots of variations for the line dataFileForecast using 1:4 with filledcurves above y1=0 linestyle 3, but nothing seems to work.
Here's the source:
#!/opt/local/bin/gnuplot
reset
set terminal pngcairo enhanced background "#000000" font "Lato-Light,20" linewidth 1 rounded size 2048,670
set output "10DayForecast.png"
dataFileForecast = "10DayForecast.csv"
set datafile separator ','
set timefmt "%Y-%m-%d"
stats dataFileForecast using 2:4 nooutput
freezeWarning = 32.
Yhigh = STATS_max_x + 10.
Ylow = STATS_min_y - 10.
unset key
set border linetype rgb "#666666"
set boxwidth 0.25 relative
set style fill transparent solid 0.4
set style line 1 linetype rgb "#0066CC" # freeze line
set style line 2 linetype rgb "#FFFFFF" pointtype 7 pointsize .5 # points for both high and low temp
set style line 3 linetype rgb "#000077" # fill for low temp
set style line 4 linetype rgb "#FFFF00" # rain forecast
set style line 5 linetype rgb "#770000" # fill for high temp
set style line 6 linetype rgb "#FF7777" # line for high temp
set style line 7 linetype rgb "#3377FF" # line for low temp
set label "°" right
unset mxtics
set tics textcolor rgb "#666666"
# X Axis
set xdata time
set xtics format "%a" nomirror
set autoscale xfix
set offsets 12*60*60,12*60*60,0,0
# Y Axis
set yrange [Ylow:Yhigh]
unset ytics
if (32 > Ylow) set ytics 32,1,32 tc rgb "#FFFFFF" font ",18"
set ytics format "%2.0f°" nomirror
# Y2 Axis
set y2tics format "%2.0f%%" textcolor "#FFFF00" nomirror
set y2range [0:100]
plot dataFileForecast using 1:6 with impulses linestyle 4 axes x1y2,\
dataFileForecast using 1:2:4 with filledcurves above linestyle 5,\
dataFileForecast using 1:2 with lines linestyle 6,\
dataFileForecast using 1:2 with points linestyle 2,\
dataFileForecast using 1:2:3 with labels offset 1.75,0.5 textcolor "#FFFFFF" font ",18",\
dataFileForecast using 1:4 with filledcurves above y1=0 linestyle 3,\
dataFileForecast using 1:4 with lines linestyle 7,\
dataFileForecast using 1:4 with points linestyle 2,\
dataFileForecast using 1:4:5 with labels offset 1.75,0.5 textcolor "#FFFFFF" font ",18",\
freezeWarning with filledcurve above y1=0 linestyle 1
And here's a data file:
2015-06-29,94,94°,69,69°,20.0
2015-06-30,99,99°,72,72°,0.0
2015-07-01,102,102°,73,73°,0.0
2015-07-02,96,96°,69,69°,0.0
2015-07-03,94,94°,68,68°,10.0
2015-07-04,92,92°,67,67°,0.0
2015-07-05,91,91°,63,63°,0.0
2015-07-06,91,91°,62,62°,0.0
2015-07-07,91,91°,61,61°,0.0
2015-07-08,91,91°,63,63°,0.0
What am I missing?
I'm not sure, why this doesn't work. Could be a bug. As workaround you can give a third column in the using statement to indicate the lower boundary of the filled area:
plot ...,\
dataFileForecast using 1:4:(0) with filledcurves linestyle 3
This gives the output
The freezeWarning area is plotted correctly, if the yrange includes this region.

shorten the distance in between plots in multiplot

I have to plot a multiplot comprising two columns and five rows. I have plotted that but I find the distance in between the plots are big and I want to reduce them. I used the last sample coding (template) as in the website ( http://www.gnuplot.info/demo_canvas/layout.html ). I tried to adjust the top and bottom margin parameters. If I use smaller values the label for x-axis disappears and the distance between plots shortened. So I have to use big values. I just wonder is there any other way to bring the plot nearer to each other? I have given the coding for plot below for your view.
I would be glad to get some insights on this issue.
Thanks in advance
#!/usr/bin/gnuplot
# SET TERMINAL
set terminal postscript color enhanced "Arial" 10 #dashed lw 1 "Helvetica" 14
set output "plot-distribution-isoMalto-thermo.ps"
# SET MACRO
set macro
labelFONT="font 'Arial,20'"
scaleFONT="font 'Arial,14'"
graph="using 1:2"
axislabelFONT="font 'Arial,18' "
#main_titleFONT="font 'times,14'"
graphLabel="at 120,9000 font 'Arial,20' "
position_orienation="at -50,18000 rotate right"
color="linecolor rgb 'black' "
layer1=" w l lt 1 lw 3 lc rgb 'black'"
layer3=" w l lt 3 lw 3 lc rgb 'red' "
layer2=" w l lt 2 lw 3 lc rgb 'green'"
layer4=" w l lt 4 lw 3 lc rgb 'blue'"
# SET MARGINS
set tmargin 0.5
set bmargin 4.0
set lmargin 15
set rmargin 3
# SET RANGE
set xrange [0:180]
set yrange [0:12000]
set xtics nomirror 0, 60, 180 #scaleFONT
set ytics 0, 3000, 12000 #scaleFONT
set format x ""
# MULTIPLOT START
set multiplot layout 5, 2 #title "Multiplot layout 5, 2"
set nokey
# PLOTTING STARTS
#plot1
#set title "Plot 1"
set xtics nomirror
set label 1 "(a)" #graphLabel
plot "angle_output-thermo-malto-L1.dat" #layer1,\
"angle_output-thermo-malto-L3.dat" #layer3
#plot2
#set title "Plot 2"
set label 1 "(b)" #graphLabel
plot "angle_output-thermo-malto-L2.dat" #layer2 ,\
"angle_output-thermo-malto-L4.dat" #layer4
#plot3
#set title "Plot 3"
set label 1 "(c)" #graphLabel
plot "angle_output-thermo-bcmChain1-L1.dat" #layer1 ,\
"angle_output-thermo-bcmChain1-L3.dat" #layer3
#plot4
#set title "Plot 4"
set label 1 "(d)" #graphLabel
plot "angle_output-thermo-bcmChain1-L2.dat" #layer2 ,\
"angle_output-thermo-bcmChain1-L4.dat" #layer4
#plot5
#set title "Plot 5"
set label 1 "(e)" #graphLabel
set label 2 "Distribution / N" #position_orienation #labelFONT
plot "angle_output-thermo-bcmChain2-L1.dat" #layer1 ,\
"angle_output-thermo-bcmChain2-L3.dat" #layer3
#plot6
#set title "Plot 6"
set nolabel
set label 1 "(f)" #graphLabel
plot "angle_output-thermo-bcmChain2-L2.dat" #layer2 ,\
"angle_output-thermo-bcmChain2-L4.dat" #layer4
#plot7
#set title "Plot 7"
set label 1 "(g)" #graphLabel
plot "angle_output-thermo-cello-L1.dat" #layer1 ,\
"angle_output-thermo-cello-L3.dat" #layer3
#plot8
#set title "Plot 8"
set label 1 "(h)" #graphLabel
plot "angle_output-thermo-cello-L2.dat" #layer2 ,\
"angle_output-thermo-cello-L4.dat" #layer4
# for plot 9 and 10
unset yrange
set yrange [0:16000]
set ytics 0, 4000, 16000
set key
#plot9
#set title "Plot 9"
set label 1 "(i)" #graphLabel
set format x
plot "angle_output-thermo-isomalto-L1.dat" #layer1 title "layer1",\
"angle_output-thermo-isomalto-L3.dat" #layer3 title "layer3"
#plot10
#set title "Plot 10"
set label 1 "(j)" #graphLabel
set label "Angle in {/Symbol q} / deg" at -90,-8500 #labelFONT
set format x
plot "angle_output-thermo-isomalto-L2.dat" #layer2 title "layer2",\
"angle_output-thermo-isomalto-L4.dat" #layer4 title "layer4"
# END MULTIPLOT
unset multiplot
#reset
#pause -1
I know this is an old question, but it might be possible to do something with set size to increase the size of each plot (optionally, a set origin for each plot might be necessary). Otherwise, your best bet is to use set lmargin,set rmargin, set bmargin and set tmargin.
You can use set ?margin at ... to set the margin relative to screen coordinates (as opposed to character coordinates in the form you have put it. This will allow you to place the plots exactly where you want to. see http://gnuplot.sourceforge.net/demo/margins.html

Resources