Normalized histograms in gnuplot with added function plot - gnuplot

I have the following script to plot histograms:
set terminal postscript eps enhanced color
set title "Histogram\_CreatesFile"
colour1="#00A0ff"
colour2="navy"
colour3="#ffA000"
colour4="#800000"
set output 'Histogram_CreatesFile.eps'
set yrange [0:]
set style fill solid 0.8 border -1
bin_width = 0.2
set boxwidth bin_width
bin_number(x) = floor(x/bin_width)
rounded(x) = bin_width * ( bin_number(x) + 0.5 )
plot 'Histogram_CreatesFile.txt' using (rounded($1)):(1) smooth frequency with boxes lc rgb colour1 notitle
This is supposed to be empirical realisation of some distribution, so to make it more clear I would like to:
Normalize the bars appropriately so that they can be compared to a density function (I guess the sum of the areas of the bars should sum-up to unity? That would mean that the height of each bar should be divided by barWidth*numberOfElements)
On the same picture plot the theoretical distribution function, given by a closed form formula (e.g. Gaussian)
How can I achieve this?

I managed to solve this issue.
(1) The normalization goes into the column after the colon, so the plot command becomes:
plot 'ConfUoMBM1validation0_0.txt' using (rounded($1)):(1/(bin_width*STATS_records)) smooth frequency with boxes lc rgb colour1 notitle
(2) Plotting of functions can't be easier, just do it after a coma as you would normally do
So the final outcome is:
set terminal postscript eps enhanced color
set title "ConfUoMBM1validation0 0"
colour1="#00A0ff"
colour2="navy"
colour3="#ffA000"
colour4="#800000"
set output 'ConfUoMBM1validation0_0.eps'
set style fill solid 0.8 border -1
bin_width = 0.926911
set boxwidth bin_width
bin_number(x) = floor(x/bin_width)
rounded(x) = bin_width * ( bin_number(x) + 0.5 )
invsqrt2pi = 0.398942280401433
normal(x,mu,sigma)=sigma<=0?1/0:invsqrt2pi/sigma*exp(-0.5*((x-mu)/sigma)**2)
stats 'ConfUoMBM1validation0_0.txt' using (rounded($1)) nooutput
set xrange [STATS_min-bin_width/2.:STATS_max+bin_width/2.]
set yrange [0:]
plot 'ConfUoMBM1validation0_0.txt' using (rounded($1)):(1/(bin_width*STATS_records)) smooth frequency with boxes lc rgb colour1 notitle, normal(x,-0.14166974006432781,4.6345562297659741) with lines lc rgb colour2 lw 5 notitle

Related

Is there a mismatch between splot and arrow coordinates in gnuplot or am I missing something?

I'm trying to plot the following 3 intersecting planes:
x + 2y + 4z = 7
2x + 3y + 3z = 1
3x + 7y + 2z = -11
To add emphasis, I wanted to include some headless arrows along the intersection of each pair of planes and a small sphere to indicate the intersection point. But for some reason the planes themselves seem to be out of alignment with the axes. From the equations I can easily find the coordinate of the intersection point, find the coordinates of the edges of the lines that run along the intersection of each pair of planes and if needed find the parametric equation of the lines. But so far when I plot the lines as arrows from the edges of my plot or the intersection as a circle, the planes seem to be wrongly positioned within the coordinates. I can see that the circle or the lines are positioned correctly, but the planes themselves seem to have been shifted. What could be causing this? Am I entering the equations wrong? Maybe the shift of the xy plane moves things around? I think it must be something obvious I'm just not seeing. I find it quite puzzling.
reset
samps = 500
set samples samps,samps
set isosamples samps,samps
f(x,y) = 7/4 - x/4 - y/2
set table $Data01
splot f(x,y)
unset table
g(x,y) = 1/3 - 2/3*x - y
set table $Data02
splot g(x,y)
unset table
h(x,y) = -11/2 - 3/2*x - 7/2*y
set table $Data03
splot h(x,y)
unset table
Zmin = 1.0
Zmax = 3.5
set xrange [-1.2:0.5]
set yrange [-4:0]
set zrange [Zmin:Zmax]
set hidden3d
set xlabel 'x'
set ylabel 'y'
set zlabel 'z'
set xyplane at Zmin
unset xzeroaxis
unset yzeroaxis
unset zzeroaxis
set border 1023-128
set xtics out nomirror
set ytics out nomirror
set ztics out
#set xtics add ('' -4)
Frac(z) = (z - Zmin) / (Zmax - Zmin)
#MyPalette01
Red01(z) = 0
Green01(z) = 255*256
Blue01(z) = int(255*Frac(z))
MyPalette01(z) = Red01(z) + Green01(z) + Blue01(z)
#MyPalette02
Red02(z) = 255*256*256
Green02(z) = int(165*Frac(z))*256
Blue02(z) = 0
MyPalette02(z) = Red02(z) + Green02(z) + Blue02(z)
# MyPalette03
Red03(z) = int(-95*Frac(z)+255)*256*256
Green03(z) = int(32*Frac(z))*256
Blue03(z) = int(-15*Frac(z)+255)
MyPalette03(z) = Red03(z) + Green03(z) + Blue03(z)
#Red03(z) = int(255*Frac(z))*256*256
#Green03(z) = int(255*Frac(z))*256
#Blue03(z) = int(255*Frac(z))
set object circle at -1,-2,3 size 0.05 front
unset key
set pm3d
set pm3d lighting primary 0.5 specular 0.6
set pm3d ftriangles
set style fill transparent solid 0.75 noborder
set pm3d depthorder
unset colorbox
set view 68, 126
splot $Data01 u 1:2:3:(MyPalette01($3)) w l lc rgb var notitle, \
$Data02 u 1:2:3:(MyPalette02($3)) w l lc rgb var notitle, \
$Data03 u 1:2:3:(MyPalette03($3)) w l lc rgb var notitle
I found how to set multiple styles for each plane in here:
Gnuplot 5.2 splot: Multiple pm3d palette in one plot call
And this is how it looks:
Any Ideas?

Plotting multiple pm3d surfaces each having its own palettes in gnuplot

What I basically want to do is basically the same as the solution to this question
Gnuplot 5.2 splot: Multiple pm3d palette in one plot call
, but which works with pm3d. If you read the comment to that answer, the answerer said that the solution does not work if he used pm3d. Also, would it be possible to define the palette in a simpler manner, such as set palette defined ()?
The development branch of gnuplot supports multiple named palettes. The method shown here, however, works on earlier versions of gnuplot also. It uses the fill style to provide a color (rather than the pm3d palette), and shows how to define the fill colors so that they mimic set palette defined(). This demo constructs only one mapping, but you could define several mappings each with its own array of colors and mapping function to use them.
This demo is extracted from the full demo for named palettes in the development branch. If you are interested you can find the full demo here:
Version 5.5 named palette demo
#
# Demonstrate construction and use of a separate palette
# Ethan A Merritt - May 2020
#
# Method 1:
# The first method works also in 5.2 but requires "lc rgb variable"
# rather than the more natural "fillcolor rgb variable".
# "set pm3d interpolate" breaks the color mapping of this method
#
# This creates a palette equivalent to
# set palette defined (0 "dark-blue", 1 "white")
#
array blues[256]
do for [i=1:256] {
blues[i] = int( (0x7f + (i-1)/(255.) * 0xffff80) );
}
#
# This is the equivalent of
# set cbrange [-1:1]
blues_min = -1
blues_max = 1
#
# This function maps z onto a palette color
#
blues(z) = (z <= blues_min) ? blues[1] \
: (z >= blues_max) ? blues[256] \
: blues[ floor(255. * (z-blues_min)/(blues_max-blues_min)) + 1]
foo(x,y) = sin(x*y)
set samples 41
set isosamples 41
unset colorbox
set cbrange [-1:1]
set xrange [0:5]; set urange [0:5]
set yrange [0:5]; set vrange [0:5]
set title "Use hand-constructed 'blues' palette via rgb variable"
splot '++' using 1:2:(foo($1,$2)):(blues(foo($1,$2))) with pm3d fillcolor rgb variable \
title "pm3d using 1:2:3:4 with pm3d fillcolor rgb variable"
Maybe you could define the palette differently with set palette defined but then you probably would have to combine your 3 palettes into 1 palette and you would lose "color resolution", as far as I know a palette has 256 color steps. To be honest, I haven't thought about this in detail.
I checked again the code you referenced... apparently an additional line will do the "trick". Then you can plot with pm3d.
set pm3d depthorder
Code: (slightly modified code from here: https://stackoverflow.com/a/57501649/7295599)
### multiple "palettes" within one splot command
reset session
set samples 101,101
set isosamples 101,101
f(x,y) = sin(1.3*x)*cos(0.9*y)+cos(.8*x)*sin(1.9*y)+cos(y*.2*x)
set table $Data01
splot f(x,y)
unset table
g(x,y) = y
set table $Data02
splot g(x,y)
unset table
h(x,y) = 0.5*x
set table $Data03
splot h(x,y)
unset table
Zmin = -3
Zmax= 3
set xrange[-5:5]
set yrange[-5:5]
set zrange[Zmin:Zmax]
set hidden3d
set angle degree
Frac(z) = (z-Zmin)/(Zmax-Zmin)
# MyPalette01
Red01(z) = 65536 * ( Frac(z) > 0.75 ? 255 : int(255*abs(2*Frac(z)-0.5)))
Green01(z) = int(255*sin(180*Frac(z)))*256
Blue01(z) = int(255*cos(90*Frac(z)))
MyPalette01(z) = Red01(z) + Green01(z) + Blue01(z)
# MyPalette02
Red02(z) = 65536 * int(255*Frac(z))
Green02(z) = 256 * (Frac(z) > 0.333 ? 255 : int(255*Frac(z)*3))
Blue02(z) = (Frac(z) > 0.5 ? 255 : int(255*Frac(z)*2))
MyPalette02(z) = Red02(z) + Green02(z) + Blue02(z)
# MyPalette03
Red03(z) = 65536 * (Frac(z) > 0.5 ? 255 : int(255*Frac(z)*2))
Green03(z) = 256 * (Frac(z) > 0.333 ? 255 : int(255*Frac(z)*3))
Blue03(z) = int(255*Frac(z))
MyPalette03(z) = Red03(z) + Green03(z) + Blue03(z)
set pm3d depthorder
unset colorbox
set view 44,316
splot $Data01 u 1:2:3:(MyPalette01($3)) w pm3d lc rgb var notitle, \
$Data02 u 1:2:3:(MyPalette02($3)) w pm3d lc rgb var notitle, \
$Data03 u 1:2:3:(MyPalette03($3)) w pm3d lc rgb var notitle
### end of code
Result:

Gnuplot 5: color gradient shading between curves

This was created with Matplotlib. Is it possible to make the same type of shading in Gnuplot 5?
I'm not aware that gnuplot has a gradient fill option, but I could be wrong.
The following is a bit of an "ugly" workaround. You basically create 3 plots on top of each other. You might want to adjust the palette to get the desired colors and a smooth transition.
a dummy plot to get the palette as background (i.e. the colorbox as large as the graph)
cover the part above y>f(x) and y>0 to x2-axis as well as below y<f(x) and y<0 to x1-axis.
plot again f(x) to see f(x) and the axes tics again
Edit:
The earlier version of the code used multiplot. It's not necessary, just use set colorbox back. But then set xzeroaxis ls -1 is not visible anymore, add plot 0 w l ls -1 instead.
Code:
### filled curve with gradient
reset session
f(x) = sin(x)/(1+x)
fabove(x) = f(x)<0 ? 0 : f(x)
fbelow(x) = f(x)>0 ? 0 : f(x)
set samples 200
set palette defined (0 "white", 1 "red", 2 "black")
set colorbox back user origin graph 0, graph 0 size graph 1, graph 1
unset cbtics
set xrange[0:15]
set xzeroaxis ls -1
set yrange[-0.2:0.5]
plot fabove(x) w filledcurves x2 fc rgb "white" not, \
fbelow(x) w filledcurves x1 fc rgb "white" not, \
f(x) w l lw 2 lc rgb "black", \
NaN palette, \
0 w l ls -1
### end of code
Result:

Plotting two parallel charged plate configuration in Gnuplot

I want to plot a parallel plate capacitor setup with plates at x = -1 and x = +1 lying in the yz plane. I have to then show the potential varying in between them and the vector plot of electric field.
How can I generate the solid plates in 3D?
I am not sure if Gnuplot is the best tool for this, nevertheless an approximation could be perhaps achieved with parametric plotting, where the x-coordinate is fixed and y/z are directly mapped to the u/v parameters:
set terminal pngcairo rounded font ",16"
set xr [-4:4]
set yr [-4:4]
set zr [-4:4]
set palette defined ( 0 "black", 1 "#666666" )
set pm3d at s
unset surface
unset colorbox
set isosamples 100
unset key
set parametric
set ur [-2:2]
set vr [-2:2]
splot \
-1,u,v w l lc rgb '#333333', \
+1,u,v w l lc rgb '#333333'
#or set larger ur/vr and use, e.g.,
# -1,(u>-2&&u<2?u:1/0),(v>-2&&v<2?v:1/0) w l lc rgb '#333333', \
# +1,(u>-2&&u<2?u:1/0),(v>-2&&v<2?v:1/0) w l lc rgb '#333333'
This would give you:

Gnuplot: 3D Surface Outline

I am plotting a simple 3D surface in GNUPlot for the following function:
f(x,y)=x**2-y**2
This works fine. However I would like to only display the outline of the surface. Without the colors or grid lines along it. Is there a way to achieve this?
Here is an example of what I am looking to create:
Outline of the surface
Thank you for your help.
In this specific case you could also adjust the number of isolines drawn by gnuplot:
f(x,y) = x**2 - y**2
set xr [-10:10]
set yr [-10:10]
unset key
set isosamples 2,3
splot f(x,y)
I'm not aware of a general solution. In your special case I would consider just drawing each single line using parametric mode like in this script:
f(x,y) = x**2 - y**2
set parametric
set urange [-10:10]
set vrange [-10:10]
set nokey
#set border 0 # uncomment to remove the axes
#unset xtics
#unset ytics
#unset ztics
set arrow 1 from 0,0,0 to 0,0,100
set arrow 1 head lw 2
splot u,-10,f( u,-10) lc 0, \
u, 10,f( u, 10) lc 0, \
-10, v,f(-10, v) lc 0, \
10, v,f( 10, v) lc 0, \
u, 0,f( u, 0) lc 0
This is the result:

Resources