gnuplot -- plot range different from axes range - plot

I have this very simple, working gnuplot code:
splot x * x
This plots a surface from -10 < x < 10 and -10 < y < 10.
I want to plot the same surface from -10 < x < 10 and -10 < y < -9 HOWEVER I still want the graph's axes to use -10 < x < 10 and -10 < y < 10. The axes bounds should not change, but the range of values plotted should be smaller.
In other words, the surface should look like a thin band in one small section of the plot. The rest should be empty space. Below is a very poor effort at creating what I want using the graph I have.
I have tried manipulating yrange, but this changes BOTH the surface bounds and the axes bounds. I have also tried disabling autoscale but I'm unsure what to do next.

In current gnuplot (version 5.2.4) you can separate the sampling range from the axis range whether or not you enable parametric mode. See example in the on-line demo set:
sampling.html (see plot #9)

One way to do this is through parametric mode, which gives you a separate set of coordinates (u and v) to set ranges for
f(x,y) = x*x
set xrange [-10:10]
set yrange [-10:10]
set urange [-10:10]
set vrange [-10:-9]
set parametric
splot u, v, f(u,v)
Alternatively you can achieve the same effect by using the ++ special filename instead of parametric mode:
f(x,y) = x*x
set xrange [-10:10]
set yrange [-10:10]
set urange [-10:10]
set vrange [-10:-9]
splot "++" u 1:2:(f($1,$2)) w l

Using a programming language with gnuplot might give better control over plots.
Here is an example using c and a script.
plot.c
#include <stdio.h>
#include <math.h>
int main()
{
int x, y;
double z;
for (y=-10; y <= 10; y++)
{
for (x=-10; x <= 10; x++)
{
z = x * 0.025;
printf("%f %f %f\n", (double)x, (double)y, 100 - cos(z * M_PI*2) * 100);
}
printf("\n");
}
fflush(stdout);
return 0;
}
plot.bat
gcc plot.c -o prog.exe -lm
prog > data.txt
echo splot "data.txt" using 1:2:3 with lines | gnuplot -p
plot.sh
gcc plot.c -o prog -lm
./prog > data.txt
echo 'splot "data.txt" using 1:2:3 with lines' | gnuplot -p
Here is an example using python and a script.
It will output:
plot.py
import math
print("\"Surface One\"")
for y in range(-10, 10+1):
for x in range(-10, 10+1):
z = x * 0.025
print("%f %f %f 1" % (float(x), float(y), 100 - math.cos(z * math.pi*2) * 100))
print("")
print("")
print("\"Surface Two\"")
for y in range(-10, -8+1):
for x in range(-10, 10+1):
z = x * 0.025
print("%f %f %f 2" % (float(x), float(y), 100 - math.cos(z * math.pi*2) * 100))
print("")
print("")
print("\"Surface Three\"")
for y in range(-10, 10+1):
for x in range(-10, 10+1):
z = x * 0.1
print("%f %f %f 7" % (float(x)/2, float(y)/2, math.sin(z * math.pi*2) * 10))
print("")
print("")
plot.bat
cd %~dp0
python plot.py > data.txt
echo ^
set terminal wxt size 570,420; ^
stats "data.txt" u 1:2 nooutput; ^
set border 0x7F linecolor rgb "#555555"; ^
set grid xtics linecolor rgb "#888888" linewidth 0.2 linetype 9; ^
set grid ytics linecolor rgb "#888888" linewidth 0.2 linetype 9; ^
set grid ztics linecolor rgb "#888888" linewidth 0.2 linetype 9; ^
set xrange [-10.0:10.0]; ^
set yrange [-10.0:10.0]; ^
set zrange [-10.0:100.0]; ^
splot for [IDX=0:STATS_blocks-1] "data.txt" i IDX using 1:2:3:4 with lines ^
lc variable title columnheader(1) | gnuplot -p
plot.sh
#!/bin/bash
python plot.py > data.txt
echo \
'set terminal wxt size 570,420; \
stats "data.txt" u 1:2 nooutput; \
set border 0x7F linecolor rgb "#555555"; \
set grid xtics linecolor rgb "#888888" linewidth 0.2 linetype 9; \
set grid ytics linecolor rgb "#888888" linewidth 0.2 linetype 9; \
set grid ztics linecolor rgb "#888888" linewidth 0.2 linetype 9; \
set xrange [-10.0:10.0]; \
set yrange [-10.0:10.0]; \
set zrange [-10.0:100.0]; \
splot for [IDX=0:STATS_blocks-1] "data.txt" i IDX using 1:2:3:4 with lines \
lc variable title columnheader(1)' | gnuplot -p

Example without explicit parametric
This was mentioned at: https://stackoverflow.com/a/51546029/895245 with a link to the docs, here is a minimal example:
set terminal png
set output 'splot-domain.png'
# Number of x and y samples.
set isosamples 10, 5
# Plotted domain.
set urange [-5.0 : 0.0]
set vrange [-5.0 : 5.0]
# Visible domain.
set xrange [-5.0 : 5.0]
set yrange [-5.0 : 5.0]
# Just to make plot look nicer.
set hidden3d
set xyplane at 0
set xlabel 'x'
set ylabel 'y'
splot '++' using 1:2:($2**2) with lines
GitHub upstream.
Output:
Tested on gnuplot 5.2 patchlevel 8.

Related

gnuplot - an epslatex terminal does not plot the x-label

I have this small MWE
set title font "Monospaced,13"
set grid
f(x) = 5 * log(x) / log(2)
g(x) = 5 + sqrt(5 + x)
F = '$5 \log_2(x)$'
G = '$5 + \sqrt{5 + x}$'
# set yrange [0:300]
set xrange [0:4000]
set xlabel 'My Label'
set style line 1 linecolor rgb '#a82828' linetype 1 linewidth 5 pointtype 5 pointsize 1.5
set style line 2 linecolor rgb '#516db0' linetype 1 linewidth 5 pointtype 9 pointsize 1.5
plot f(x) title F with lines ls 1, g(x) title G with lines ls 2
set terminal epslatex background rgb "grey" standalone size 10.0cm,10.0cm color colortext
set output 'RunTimeComparisonTreeIndexed.tex'
set style rect fc rgb "white" fs
set object 1 rectangle from screen 0,0 to screen 1,1 behind
set key left top box lt -1 lw 2 opaque
replot
exit
The above script relies on this TeX-post.
It gives me:
(Note that the x-label "My Label" is missing.)
What am I missing here?
Edit
I use the following LaTeX snippet for including the plot PDF file (see the linked TeX.StackExchange post):
\begin{figure}[!h]
\centering
%\setlength{\voffset}{0cm}
%\setlength{\hoffset}{0cm}
\includepdf[pages={1},pagecommand={},width=0.5\textwidth]{RunTimeComparisonTreeIndexed.pdf}
%\setlength{\voffset}{- 10cm}
%\setlength{\hoffset}{-10cm}
\label{fig:rtcti}
\caption{Hello}
\end{figure}
I'm not "fluent" in LaTeX, so this was also a good exercise for me and will be a memory aid for myself.
Use the terminal cairolatex and directly create a PDF with TeX text formatting without detour via .eps. I used MiKTeX and pdflatex under Win10. gnuplot will create two files (SO74149797.tex and SO74149797.pdf).
Script: (requires gnuplot>=5.4.2 for fillcolor for keybox)
### create pdf with TeX text formatting via cairolatex
reset session
f(x) = 5 * log(x) / log(2)
g(x) = 5 + sqrt(5 + x)
F = '$5 \log_2(x)$'
G = '$5 + \sqrt{5 + x}$'
set title font "Monospaced,13"
set xrange [0:4000]
set xlabel 'My Label'
set grid
set key left top box lt -1 lw 2 opaque fc "grey" # fillcolor option fc for gnuplot>=5.4.2
set key width 1 spacing 1.5
set style line 1 linecolor rgb '#a82828' linetype 1 linewidth 5 pointtype 5 pointsize 1.5
set style line 2 linecolor rgb '#516db0' linetype 1 linewidth 5 pointtype 9 pointsize 1.5
set terminal cairolatex pdf input size 10.0cm,10.0cm color colortext
set output 'SO74149797.tex'
plot f(x) title F with lines ls 1, \
g(x) title G with lines ls 2
set output
### end of script
LaTeX: (important to add these packages)
\documentclass[a4paper,10pt]{article}
\usepackage{graphicx}
\usepackage[cp1252]{inputenc}
\usepackage{color}
\begin{document}
\input{SO74149797}
\end{document}
Result: (screenshot from final PDF document)

Computing numerical derivative with gnuplot

I've been trying to compute numerically the derivative using gnuplot, using the scripts in this other discussion, even with the same data file. However I keep getting this error:
gnuplot> d(y) = ($0 == 0) ? (y1 = y, 1/0) : (y2 = y1, y1 = y, y1-y2)
^
"prova.g", line 7: ')' expected
I don't know what to do here. Any help?
Here is an example for numerical derivatives from my collection. Requires gnuplot >=5.0 and with the use of files instead of datablocks (and probably with some tweaking with gnuplot>=4.4.0).
Script: (works with gnuplot>=5.0.0, Jan. 2015)
### numerical derivatives
reset session
# create some data
MyFunction = "sin(x)/x"
set table $Data
set samples 150
plot [-10:10] '+' u 1:(#MyFunction) w table
unset table
DerivX(colX) = (x0=x1,x1=column(colX),(x0+x1)/2.)
DerivY(colY) = (y0=y1,y1=column(colY),(y1-y0)/(x1-x0))
set table $Deriv1
plot x1=y1=NaN $Data u (DerivX(1)):(DerivY(2)) w table
unset table
set table $Deriv2
plot x1=y1=NaN $Deriv1 u (DerivX(1)):(DerivY(2)) w table
unset table
set table $Deriv3
plot x1=y1=NaN $Deriv2 u (DerivX(1)):(DerivY(2)) w table
unset table
plot $Data u 1:2 w l lc rgb "red" ti MyFunction, \
$Deriv1 u 1:2 w l lc rgb "web-green" ti "1st Derivative", \
$Deriv2 u 1:2 w l lc rgb "blue" ti "2nd Derivative", \
$Deriv3 u 1:2 w l lc rgb "magenta" ti "3rd Derivative"
### end of script
Result:
Addition: (version for gnuplot 4.2.6, Sept. 2009)
gnuplot 4.2.6 doesn't have datablocks and serial evaluation, but here is a cumbersome workaround without these features.
for illustration, the script creates a data file SO68198576.dat (you already have your input file)
plot the data file into another file TEMP1 skipping the first data line
merge the files line by line into another file TEMP2 using the system command paste (either on Linux already on the system or on Windows you have to install, e.g. CoreUtils from GnuWin).
now you can calculate dx and dy between two successive datapoints from column 1 and 4 and column 2 and 5, respectively.
since the files have different length, the last line(s) should be skipped. This can be done by the system command head -n -2.
That's how TEMP2 looks like:
#Curve 0 of 1, 150 points #Curve 0 of 1, 150 points
#x y type #x y type
-10 -0.0544021 i -9.86577 -0.0432646 i
-9.86577 -0.0432646 i -9.73154 -0.0310307 i
-9.73154 -0.0310307 i -9.59732 -0.0178886 i
...
Script: (works with gnuplot 4.2.6, requires system commands paste and head)
### numerical derivative for gnuplot 4.2.6
reset
FILE = "SO68198576.dat"
set table FILE
set samples 150
plot sin(x)/x
unset table
TEMP1 = FILE.'1'
TEMP2 = FILE.'2'
set table TEMP1
plot FILE u 1:2 every ::1
unset table
system(sprintf('paste %s %s > %s', FILE, TEMP1, TEMP2))
system(sprintf('head -n -2 %s > %s',TEMP2, TEMP1))
x0(col) = (column(col)+column(col+3))/2.
dx(col) = (column(col+3)-column(col))/2.
dy(col) = (column(col+3)-column(col))/2.
plot FILE u 1:2 w lp pt 7 title "Data", \
TEMP1 u (x0(1)):(dy(2)/dx(1)) w lp pt 7 title "1st Derivative"
### end of script
Result: (screenshot gnuplot 4.2.6)

How can I draw data 2D at differents points as fenceplot at Gnuplot, with a background transparent?

I have three files where each file is 2D with an x-axis vs y-axis. I plot these files as fenceplot at Gnuplot.
With this code
reset
set term postscript eps color enhanced font "Times,15"
set view 54,111
unset xrange
set xtics +.2
set ytics +.5
set yrange [0:5]
set ticslevel 0
set xlabel "Lc/Lx"
set xlabel font "" textcolor lt -.85 rotate parallel
set ylabel "R"
set ylabel font "" textcolor lt -.85 rotate parallel
set zlabel "{/Symbol D} P / P_{NS}"
set xlabel font "" textcolor lt -.85 rotate parallel
set style fill transparent solid 0.25
set pm3d depthorder
y(x) = sin(x)
set ytics ("0.40" 0, "0.45" 1, "0.50" 2)
splot "PressureDesv.dat0" u 1:(0):2 t "R=0.40", "PressureDesv.dat 1" u 1:(1):2 t "R=0.45", "PressureDesv.dat2" u 1:(2):2 t "r=0.50"
set output "maxwell_speed_distribution.eps"
replot
set output
set term x11
I obtained this plot, but I cannot fill this curves with a background tranparent.
How do I fill these curves with a background tranparency?
Assuming I understood your question correctly, check help zerrorfill and help colorspec and the following example:
Code:
### transparent zerrorfill
reset session
### create some test data
set table $Data
set xrange[0:1]
f(x,a) = a*sin(pi*x)
plot '+' u 1:(f(x,2.0)):(f(x,1.7)):(f(x,1.5)) w table
unset table
set view 55,134
set yrange[0.36:0.54]
set ytics 0.05 offset -1,0
set ztics 0.5
set xyplane relative 0
splot $Data u 1:(0.40):2:(0):2 w zerror fc rgb 0xccff0000 lc "red" ti "R=0.40", \
'' u 1:(0.45):3:(0):3 w zerror fc rgb 0xcc00ff00 lc "green" ti "R=0.45", \
'' u 1:(0.50):4:(0):4 w zerror fc rgb 0xcc0000ff lc "blue" ti "R=0.50"
### end of code
Result:
set style data zerrorfill
set style fill transparent solid 0.5 border
set xyplane at 0
set xrange [0.3 : 0.6]
set xtics (0.40, 0.45, 0.50)
splot FILE1 using (0.40):1:2:(0):2 title "File 1",\
FILE2 using (0.45):1:2:(0):2 title "File 2",\
FILE3 using (0.50):1:2:(0):2 title "File 3"
The column assignments for zerrorfill are x:y:z:zlow:zhigh

Title for each plot in multiplot

I am doing this plot in gnuplot
enter image description here \
and I don't know why there is no legend with the colors of the curves.
I am using the following these lines in the terminal,
<<<
mySplitX = 0.33
unset key
set samples 200
set multiplot
set border 7
set origin 0,0
set size mySplitX,1
set margins -1, 0, -1, -1
set xlabel " "
set format x "%g"
set mxtics 10
set logscale x
set xrange [1:30]
set ylabel "C_l^{TT}[{/Symbol=10 m}K^2]"
set ytics nomirror
set mytics 5
set yrange [0:15000]
plot "<(sed -n '1,30p' CMB-TT_data_Planck_cleaned.dat)" u 2:3:($3-$4):($3+$5) lc 7 pt 7 ps .5 w errorbars notitle, "explanatory00_cl.dat" u 1:2 notitle w l lc 6, "CDM00_cl.dat" u 1:2 w l lc 4
set arrow 1 from screen mySplitX
set border 13
set origin mySplitX,0
set size 1-mySplitX,1
set margins 0, -1, -1, -1
set xlabel " l"
set xtics add (30)
set mxtics 5
unset logscale x
set xrange [30:2500]
unset ylabel
unset ytics
set y2tics
set my2tics 5
set format y2 ""
plot "<(sed -n '1,30p' CMB-TT_data_Planck_cleaned.dat)" u 2:3:($3-$4):($3+$5) lc 7 pt 7 ps .5 w errorbars notitle, "explanatory00_cl.dat" u 1:2 w l lc 6 t "LCDM", "CDM00_cl.dat" u 1:2 w l lc 4 t "No CDM"
as you can see I am using: title "name" in the same of the comand "plot" but I don't know why this does not work. Could anybody help me, pls?
I am looking for some like this:
enter image description here

How to plot into a histogram specific values from a file using Gnuplot

I'd like to plot two bars for a histogram, one depending on the first line of a file, while the other one depending on the second line. E.g., assuming to have a CSV file like this:
1 0.5
2 0.7
I want to plot a first bar blue of value 0.5 and a second bar red of value 0.7.
I've written the following script so far (I have a multiplot because I need to plot 4 figures of equal properties):
#!/usr/bin/env gnuplot
set term pngcairo enhanced size 1500,700
set output 'accuracy_plot.png'
set multiplot layout 1,4
set xlabel 'rounds' font ',16'
set ylabel 'mean' font ',16'
set xrange[1:2]
set yrange[0:1]
set style fill solid
set grid xtics lt 0 lw 1 lc rgb "#333333"
set grid ytics lt 0 lw 1 lc rgb "#333333"
set xtics font ',14'
set xtics autofreq 1
set ytics font ',14'
set key font ',12'
set title font ',20'
###
set title 'Q1'
plot "q1.csv" using 1:2 title '' with boxes linecolor rgb 'blue', \
"truth1.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
set title 'Q2'
plot "q2.csv" using 1:2 title '' with boxes linecolor rgb 'blue', \
"truth2.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
set title 'Q3'
plot "q3.csv" using 1:2 title '' with boxes linecolor rgb 'blue', \
"truth3.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
set title 'Q4'
plot "q4.csv" using 1:2 title '' with boxes linecolor rgb 'blue', \
"truth4.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
unset multiplot
I've tried solutions like "<(sed -n '1,1p' q1.csv)" but it didn't work.
Any idea?
You don't need to post all that settings for the tics, the labels and the multiplotting to show the problem.
You can use e.g. linecolor variable to select different line types depending on the value in the first column:
set terminal png enhanced
set output 'foobar.png'
set yrange [0:1]
set style fill solid
set linetype 1 lc rgb 'blue'
set linetype 2 lc rgb 'red'
plot 'q1.csv' using 1:2:1 with boxes lc variable notitle
Note, that the changes with set linetype aren't restored with reset, but you must restart gnuplot (version 5.0 will have a reset session option for this).
I changed every CSV in this form:
R1 R2
0.5 0.7
and my script:
#!/usr/bin/env gnuplot
set term pngcairo enhanced size 1500,700
set output 'accuracy_plot.png'
set multiplot layout 1,4
set xlabel 'rounds' font ',16'
set ylabel 'mean' font ',16'
set xrange[1:2]
set yrange[0:1.5]
set style fill solid
set grid xtics lt 0 lw 1 lc rgb "#333333"
set grid ytics lt 0 lw 1 lc rgb "#333333"
set xtics font ',14'
set xtics autofreq 1
set ytics font ',14'
set key font ',12'
set title font ',20'
set style data histograms
set style histogram columnstacked
###
set title 'Q1'
plot "q1.csv" using 1 lt 1 title columnhead, \
newhistogram lt 1 at 1, '' u 1 ti col, \
newhistogram lt 2 at 2, '' u 2 ti col, \
"truth1.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
set title 'Q2'
plot "q2.csv" using 1 lt 1 title columnhead, \
newhistogram lt 1 at 1, '' u 1 ti col, \
newhistogram lt 2 at 2, '' u 2 ti col, \
"truth1.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
set title 'Q3'
plot "q3.csv" using 1 lt 1 title columnhead, \
newhistogram lt 1 at 1, '' u 1 ti col, \
newhistogram lt 2 at 2, '' u 2 ti col, \
"truth1.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
set title 'Q4'
plot "q4.csv" using 1 lt 1 title columnhead, \
newhistogram lt 1 at 1, '' u 1 ti col, \
newhistogram lt 2 at 2, '' u 2 ti col, \
"truth1.csv" using 1:2 title 'truth' with lines linewidth 3 linecolor rgb 'black'
###
unset multiplot
This is the result (with real values):

Resources