I'm pretty new to gnuplot, so I'm thankful for every advice.
Right now, I am trying to plot some data using the logscale command. But I don't know why all the xtics disappear when I use the logscale. This is the script I use:
#creates a plot of all the four different loops with a logscale. Fits the functions as well and saves the fitting data
#in a file named fitting.dat
set size 1,1
# set logscale
set logscale y 10
set logscale x 10
#set xlabel and y label
set xlabel "Dimension of Matrix"
set ylabel "time [s]"
#scale plot
set xrange [450:850]
set yrange[0.01:5]
#nothing displayed from fitting
set fit quiet
#position of legend
set key top right
set key horizontal
# guessing the parameters, the fit will be better and we know that the exponent should be \approx 3
b=3
d=3
f=3
h=3
#Define all th four different data fitting functions, asuming f(x) ~ a*x^b
f(x)= a*x**b
g(x)=c*x**d
h(x)=e*x**f
j(x)=g*x**h
#fit the different functions
fit f(x) 'matmul.txt' using 1:2 via a,b
fit g(x) 'matmul.txt' using 1:3 via c,d
fit h(x) 'matmul.txt' using 1:4 via e,f
fit j(x) 'matmul.txt' using 1:5 via g,h
# save the fitting parameters in an extra file
set print 'fitting.dat'
print 'function'
print a,'*x', '**', b , ' rows'
print c,'*x', '**', d , ' cols'
print e,'*x', '**', f , ' intrinsic function'
print g,'*x', '**', h , ' lapack routine'
# plot everything
plot "matmul.txt" u 1:2 t "rows" ,\
"matmul.txt" u 1:3 t "cols" ,\
"matmul.txt" u 1:4 t "intrinsic" ,\
"matmul.txt" u 1:5 t "lapack" ,\
f(x) t sprintf("row:%.2e*x^(%.2f)", a,b),\
g(x) t sprintf("col:%.2e*x^(%.2f)",c,d),\
h(x) t sprintf("int:%.2e*x^(%.2f)",e,f),\
j(x) t sprintf("lap:%.2e*x^(%.2f)",g,h)
#choose output format
set terminal png
set output "time.png"
replot
#now, non-logarithmic plot
#unset logscale
set yrange[0.01:1]
unset logscale
#plot again
plot "matmul.txt" u 1:2 t "rows" ,\
"matmul.txt" u 1:3 t "cols" ,\
"matmul.txt" u 1:4 t "intrinsic" ,\
"matmul.txt" u 1:5 t "lapack" ,\
f(x) t sprintf("col:%.2e*x^(%.2f)", a,b),\
g(x) t sprintf("row:%.2e*x^(%.2f)",c,d),\
h(x) t sprintf("int:%.2e*x^(%.2f)",e,f),\
j(x) t sprintf("lap%.2e*x^(%.2f)",g,h)
My Input file 'matmul.txt' looks like this:
#Dim rows cols intrinsic lapack
500 0.1320E+00 0.1040E+00 0.6800E-01 0.2000E-01
520 0.1400E+00 0.1320E+00 0.5600E-01 0.2000E-01
540 0.1480E+00 0.1400E+00 0.6000E-01 0.3200E-01
560 0.1680E+00 0.1480E+00 0.7200E-01 0.2400E-01
580 0.1800E+00 0.1680E+00 0.6800E-01 0.3200E-01
600 0.1920E+00 0.1960E+00 0.7200E-01 0.3600E-01
620 0.2080E+00 0.2040E+00 0.9600E-01 0.2000E-01
640 0.4000E+00 0.3520E+00 0.8400E-01 0.3200E-01
...
Now, If I run this file, I obtain the following output plot
I don't know why, but the range of the yscale is not correct and the xtics are not displayed. If I plot it without 'logscale', the plot is exactly what I want. Why doesn't this work?
Tics in logarithmic plots are not separated by a constant summand as in 1, 2, 3, ..., they are separated by a constant factor as in 1, 10, 100, ...
This means in your case for the y-axis: You have given the range [0.01:5], leading to tics at 0.01, 0.1, 1 as it is seen in the picture. Above 1, you have minor tics at 2, 3, 4, and 5. 5 is the upper boundary of the graph as specified in the range. To also have a label at this tic, just add it with:
set ytics add (5)
or change the yrange to one of
set yrange [0.01:1]
set yrange [0.01:10]
For your xtics: Labels would be at 1, 10, 100, 1000, ... But your range is from 450 to 850: no labeled xtic inside.
Again, you can set them manually:
set xtics (450, 550, 650, 750, 850)
Your x-axis spans less than a decade and the default major tic frequency is a decade. If you want labeled tics within this range use set xtics (400,500,600,700,800) or whatever you want.
This is all in the documentation, just search for "logscale"
Related
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:
I have a file with four columns. I have plotted 4D-plot using gnuplot tool as follows.
splot 'test.dat' u 1:2:3:($4<200.0?$4/4.184:1/0) w pm3d
Now I want to see a piece of the plot whose X-axis is some constant value. Let's say when the first column is 0.3, I want to see a 3D plot constructed out of 2,3,4 columns.
You don't show your data, so I assumed something.
Similar as you determine your color with the ternary operator you can "filter" a slice with constant x+dx.
Code:
### slice from 4D data
reset session
# create some test data
f(x,y) = x**2 + y**2
c(x,y) = x + y
set print $Data
do for [i=-10:10] {
do for [j=-10:10] {
print sprintf("%.3f %.3f %.3f %.3f", i, j, f(i,j), c(i,j))
}
print ""
}
set print
set xrange [-10:10]
set yrange [-10:10]
set zrange [0:200]
set cbrange [-20:20]
SliceX = 5
dx = 1
set multiplot layout 1,2
splot $Data u 1:2:3:4 w pm3d notitle
splot $Data u ($1>=SliceX && $1<=SliceX+dx?$1:NaN):2:3:4 w pm3d notitle
unset multiplot
### end of code
Result:
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:
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:
I have some data in a file, with x in [2,4].
I put some of them, those for x in [2.5, 3.5], in a new file, and then I fit just the second file.
Then, I plot the first file, with all the data, and replot the fit function.
In this way, the fit function is plotted for x in [2,4] but is horrible because it does not fit in [2, 2.5] and [3, 3.5].
So I'd like to have the plot of this fit function only in the right range, but gnuplot doesn't allow me to set a particular x range when using replot.
So, how can I have all the data but the fit function only in the right region in an unique plot?
Put the fit function in a datafile, then plot this datafile together with your original data.
# This is the complete xrange
set xrange [-2:2]
# This creates some test data to play with
set table "data.txt"
plot sin(x)
unset table
# Fit some of the created data points
f(x) = a*x + b
fit [-0.5:0.5] f(x) "data.txt" via a, b
# Plot the fit function to a temporary file
# Note, only the xrange of the fit is used
set table "fit.dat"
plot [-0.5:0.5] f(x)
unset table
# Plot both datafiles in one diagram
plot "data.txt" w l, "fit.dat" w l lw 4