I have a data file "data.txt" which contains the coordinates of the borders of several boxes in three dimensions. Each line represents a single box. The file contains over 100 boxes.
x_Min x_Max y_Min y_Max z_Min z_Max
-0.2 0.2 -0.2 0.2 -0.2 0.2
0.2 0.4 -0.2 0.2 -0.2 0.2
....
...
..
Now I want to plot that. In two dimensions it is very easy by using
plot "boxes.txt" u 1:2:3:4 w boxxyerrorbars
With (x-Value):(y-Value):(Half Width):(Half Height).
Than I get this:
But how can I achieve this in three dimensions? I didn't find any solution for this problem.
In case you are still interested in a gnuplot solution...
If it is sufficient to just draw the edges of the boxes you can use the plotting style with vectors. You simply need to select the necessary columns and plot all edges in 3 loops. Here gnuplot's integer division (e.g. 1/2=0) is helpful.
However, if you want to plot surfaces and hide surfaces if they are covered by another box you'd better use with pm3d (check help pm3d). Then, however, you have to re-shape your input data.
Script:
### plot edges of boxes in 3D
reset session
$Data <<EOD
x_Min x_Max y_Min y_Max z_Min z_Max
-0.2 0.2 -0.2 0.2 -0.2 0.2
0.3 0.4 -0.1 0.2 -0.1 0.2
-1.5 -0.5 -1.2 -0.4 -0.9 0.0
0.5 1.0 -1.0 -0.5 -0.5 -0.1
0.0 0.3 -1.4 -1.1 -1.0 -0.7
EOD
set xyplane relative 0
set view equal xyz
set view 60,30,1.7
set xtics 0.5
set ytics 0.5
set ztics 0.5
set key noautotitle
splot for [i=0:3] $Data u 1:i/2+3:i%2+5:($2-$1):(0):(0):0 w vec lc var nohead, \
for [i=0:3] '' u i/2+1:3:i%2+5:(0):($4-$3):(0):0 w vec lc var nohead, \
for [i=0:3] '' u i/2+1:i%2+3:5:(0):(0):($6-$5):0 w vec lc var nohead
### end of script
Result:
I actually found a solution using Python and Matplotlib.
import numpy as np
import matplotlib.pyplot as plt
import random
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.gca(projection='3d')
DIM = 3;
# Unit cube
cube = [[[0.0,1.0],[0.0,0.0],[0.0,0.0]],\
[[0.0,0.0],[0.0,1.0],[0.0,0.0]],\
[[0.0,0.0],[0.0,0.0],[0.0,1.0]],\
[[1.0,1.0],[0.0,1.0],[0.0,0.0]],\
[[1.0,0.0],[1.0,1.0],[0.0,0.0]],\
[[1.0,1.0],[0.0,0.0],[0.0,1.0]],\
[[1.0,1.0],[1.0,1.0],[0.0,1.0]],\
[[0.0,0.0],[1.0,1.0],[0.0,1.0]],\
[[0.0,0.0],[0.0,1.0],[1.0,1.0]],\
[[0.0,1.0],[0.0,0.0],[1.0,1.0]],\
[[1.0,1.0],[0.0,1.0],[1.0,1.0]],\
[[0.0,1.0],[1.0,1.0],[1.0,1.0]]]
# Number of Cubes
numb_Cubes = 5
# Array with positions [x, y, z]
pos = [[0 for x in range(DIM)] for y in range(numb_Cubes)]
for k in range(numb_Cubes):
for d in range(DIM):
pos[k][d] = random.uniform(-1,1)
# Size of cubes
size_of_cubes = [0 for y in range(numb_Cubes)]
for k in range(numb_Cubes):
size_of_cubes[k] = random.random()
# Limits
xmin, xmax = -1, 1
ymin, ymax = -1, 1
zmin, zmax = -1, 1
for n in range(numb_Cubes):
for k in range(len(cube)):
x = np.linspace(cube[k][0][0]*size_of_cubes[n]+pos[n][0], cube[k][0][1]*size_of_cubes[n]+pos[n][0], 2)
y = np.linspace(cube[k][1][0]*size_of_cubes[n]+pos[n][1], cube[k][1][1]*size_of_cubes[n]+pos[n][1], 2)
z = np.linspace(cube[k][2][0]*size_of_cubes[n]+pos[n][2], cube[k][2][1]*size_of_cubes[n]+pos[n][2], 2)
ax.plot(x, y, z, 'black', lw=1)
ax.set_xlim([xmin,xmax])
ax.set_ylim([ymin,ymax])
ax.set_zlim([zmin,ymax])
The result I get:
I am still interested in a solution for gnuplot or a faster solution for Python.
Related
I have a list of more than 100 points. I'd like to plot a figure like this picture. The lines connect any two points whose distance is less than 3.
1.53 2.40
5.39 3.02
4.35 1.29
9.58 8.34
6.59 1.45
3.44 3.45
7.22 0.43
0.23 8.09
4.38 3.49
https://www.codeproject.com/Articles/1237026/Simple-MLP-Backpropagation-Artificial-Neural-Netwo
You probably have to check every point against every other point whether the distance is less than your threshold. So, create a table with all these points, the vector between them and plot them with vectors. The following example creates some random points with random sizes and random colors.
Code:
### Plot connections between points which are closer than a threshold
reset session
set size square
# create some random test data
set print $Data
myColors = "0xe71840 0x4d76c3 0xf04092 0x47c0ad 0xf58b1e 0xe6eb18 0x59268e 0x36b64c"
myColor(n) = int(word(myColors,n))
do for [i=1:100] {
print sprintf("%g %g %g %d", rand(0), rand(0), rand(0)*2+1, myColor(int(rand(0)*8)+1))
}
set print
d(x1,y1,x2,y2) = sqrt((x2-x1)**2 + (y2-y1)**2)
myDist = 0.2
set print $Connect
do for [i=1:|$Data|-1] {
x1=real(word($Data[i],1))
y1=real(word($Data[i],2))
do for [j=i+1:|$Data|] {
x2=real(word($Data[j],1))
y2=real(word($Data[j],2))
if (d(x1,y1,x2,y2)<myDist) { print sprintf("%g %g %g %g", x1, y1, x2-x1, y2-y1) }
}
}
set print
set key noautotitle
plot $Connect u 1:2:3:4 w vec lc "grey" nohead, \
$Data u 1:2:3:4 w p pt 7 ps var lc rgb var
### end of code
Result:
You do not specify how to choose the node size or color. I show an example using a constant pointsize and taking the color from sequential linetypes
[![enter image description here][1]][1]$DATA << EOD
1.53 2.40
5.39 3.02
4.35 1.29
9.58 8.34
6.59 1.45
3.44 3.45
7.22 0.43
0.23 8.09
4.38 3.49
EOD
N = |$DATA|
do for [i=1:N] {
do for [j=i+1:N] {
x0 = real(word($DATA[i],1))
y0 = real(word($DATA[i],2))
x1 = real(word($DATA[j],1))
y1 = real(word($DATA[j],2))
if ((x1-x0)**2 + (y1-y0)**2 <= 9) {
set arrow from x0,y0 to x1,y1 nohead
}
}
}
unset border
unset tics
unset key
set pointsize 3
plot $DATA using 1:2:0 with points pt 7 lc variable
I try to draw some vector fields in a circular region. Consider the following MWE
unset grid
unset tics
unset colorbox
unset border
set size square
besselj(n, x) = n > 1 ? 2*(n-1)/x*besselj(n-1,x) - besselj(n-2,x) : (n == 1 ? besj1(x) : besj0(x))
dbesselj(n, x) = n/x*besselj(n,x) - besselj(n+1,x)
rho(x,y) = sqrt(x**2+y**2)
phi(x,y) = atan2(y,x)
d = 1.0
l = 1.0
z = l/2
q = 1
set xrange [-d/2*1.1:d/2*1.1]
set yrange [-d/2*1.1:d/2*1.1]
Erho(x,y,n,ynp) = (-1/rho(x,y)) * besselj(n, (ynp*2/d)*rho(x,y)) * (-n*sin(n*phi(x,y))) * sin(q*pi*z/l)
Ephi(x,y,n,ynp) = (ynp*2/d) * dbesselj(n, (ynp*2/d)*rho(x,y)) * (cos(n*phi(x,y))) * sin(q*pi*z/l)
Ex(x,y,n,ynp) = rho(x,y) > d/2 ? NaN : cos(phi(x,y))*Erho(x,y,n,ynp) - sin(phi(x,y))*Ephi(x,y,n,ynp)
Ey(x,y,n,ynp) = rho(x,y) > d/2 ? NaN : sin(phi(x,y))*Erho(x,y,n,ynp) + cos(phi(x,y))*Ephi(x,y,n,ynp)
mag(x,y,n,ynp) = sqrt(Ex(x,y,n,ynp)**2 + Ey(x,y,n,ynp)**2)
set object circle at 0,0 size 0.5 fc black lw 3 front
set multiplot layout 1,2
set title 'TE_{01}'
set table 'tmp.dat'
set samples 16
set isosamples 16
plot '++' u 1:2:(Ex($1,$2,0,3.832)/50):(Ey($1,$2,0,3.832)/50) w vectors
unset table
set samples 250
set isosamples 250
plot '++' u 1:2:(mag($1,$2,0,3.832)) w image notitle, \
'tmp.dat' u 1:2:3:4 w vectors head filled lc black lw 1 notitle
set title 'TE_{11}'
set table 'tmp.dat'
set samples 16
set isosamples 16
plot '++' u 1:2:(Ex($1,$2,1,1.841)/20):(Ey($1,$2,1,1.841)/20) w vectors
unset table
set samples 250
set isosamples 250
plot '++' u 1:2:(mag($1,$2,1,1.841)) w image notitle, \
'tmp.dat' u 1:2:3:4 w vectors head filled lc black lw 1 notitle
unset multiplot
which plots the vector field as well as its magnitude inside the circle with diameter d. The result from this is
which is totally okay for the left image (TE01), but the right image (TE11) looks ugly because there are some vectors which are drawn outside the circle. My actually desired result is this
where I have no vectors outside of the black circle. How can I achieve that?
I know there is the clip function in gnuplot, but this does not allow to specify the shape to be used for clipping.
Here is what you can try. Define your own clip function, e.g. a circle.
First you need to check whether a data point is outside of your circle or not.
Clip(x,y) returns NaN if it is outside and 0 if it is inside.
Now, when you plot simply add the value of the clip function to your value. Your data will be clipped within a circle because something +0 remains unchanged and something +NaN will be NaN and will not be plotted. It is sufficient if you do this just for x (vector start) and x + delta x (vector end).
Code:
### clip function in circle form
reset session
set size square
# create some test data
set samples 25
Scaling = 0.5
set table $Data
plot [-5:5] '++' u 1:2:(Scaling*$1/sqrt($1**2+$2**2)): \
(Scaling*$2/sqrt($1**2+$2**2)) : (sqrt($1**2+$2**2)) with table
unset table
set palette rgb 33,13,10
CenterX = 0
CenterY = 0
Radius = 3.5
Clip(x,y) = sqrt((x-CenterX)**2 + (y-CenterY)**2) > Radius ? NaN : 0
set xrange[-6:6]
set yrange[-6:6]
set multiplot layout 1,3
plot $Data u 1:2:3:4:5 w vec lc pal not
plot $Data u ($1+Clip($1,$2)):2:($3+Clip($1+$3,$2+$4)):4:5 w vec lc pal not
CenterX = 1
CenterY = 1
plot $Data u ($1+Clip($1,$2)):2:($3+Clip($1+$3,$2+$4)):4:5 w vec lc pal not
unset multiplot
### end of code
Result:
I am having trouble doing something that seems to me straightforward.
My data is:
ROE_SP500_Q2_2018_quantile.to_json()
'{"index":{"0":0.0,"1":0.05,"2":0.1,"3":0.15,"4":0.2,"5":0.25,"6":0.3,"7":0.35,"8":0.4,"9":0.45,"10":0.5,"11":0.55,"12":0.6,"13":0.65,"14":0.7,"15":0.75,"16":0.8,"17":0.85,"18":0.9,"19":0.95},"ROE_Quantiles":{"0":-0.8931,"1":-0.0393,"2":0.00569,"3":0.03956,"4":0.05826,"5":0.075825,"6":0.09077,"7":0.10551,"8":0.12044,"9":0.14033,"10":0.15355,"11":0.17335,"12":0.1878,"13":0.209175,"14":0.2357,"15":0.27005,"16":0.3045,"17":0.3745,"18":0.46776,"19":0.73119}}'
My code for the plot is:
plt.close()
plt.figure(figsize=(14,8))
sns.barplot(x = 'Quantile', y = 'ROE', data = ROE_SP500_Q2_2018_quantile)
plt.vlines(x = 0.73, ymin = 0, ymax = 0.6, color = 'blue', size = 2)
plt.show()
which returns the following image:
I would like to correct the following problems:
a) The ticklabels which are overly crowded in a strange way I do not understand
b) The vline which appears in the wrong place. I am using the wrong argument to set the thickness of the line and I get an error.
Pass to parameter data DataFrame, check seaborn.barplot:
data : DataFrame, array, or list of arrays, optional
Dataset for plotting. If x and y are absent, this is interpreted as wide-form. Otherwise it is expected to be long-form.
sns.barplot(x = 'index', y = 'ROE_Quantiles', data = ROE_SP500_Q2_2018_quantile)
#TypeError: vlines() missing 2 required positional arguments: 'ymin' and 'ymax'
plt.vlines(x = 0.73, ymin = 0, ymax = 0.6, color = 'blue', linewidth=5)
j = '{"index":{"0":0.0,"1":0.05,"2":0.1,"3":0.15,"4":0.2,"5":0.25,"6":0.3,"7":0.35,"8":0.4,"9":0.45,"10":0.5,"11":0.55,"12":0.6,"13":0.65,"14":0.7,"15":0.75,"16":0.8,"17":0.85,"18":0.9,"19":0.95},"ROE_Quantiles":{"0":-0.8931,"1":-0.0393,"2":0.00569,"3":0.03956,"4":0.05826,"5":0.075825,"6":0.09077,"7":0.10551,"8":0.12044,"9":0.14033,"10":0.15355,"11":0.17335,"12":0.1878,"13":0.209175,"14":0.2357,"15":0.27005,"16":0.3045,"17":0.3745,"18":0.46776,"19":0.73119}}'
import ast
df = pd.DataFrame(ast.literal_eval(j))
print (df)
index ROE_Quantiles
0 0.00 -0.893100
1 0.05 -0.039300
10 0.50 0.153550
11 0.55 0.173350
12 0.60 0.187800
13 0.65 0.209175
14 0.70 0.235700
15 0.75 0.270050
16 0.80 0.304500
17 0.85 0.374500
18 0.90 0.467760
19 0.95 0.731190
2 0.10 0.005690
3 0.15 0.039560
4 0.20 0.058260
5 0.25 0.075825
6 0.30 0.090770
7 0.35 0.105510
8 0.40 0.120440
9 0.45 0.140330
plt.close()
plt.figure(figsize=(14,8))
sns.barplot(x = 'index', y = 'ROE_Quantiles', data = df)
plt.vlines(x = 0.73, ymin = 0, ymax = 0.6, color = 'blue', linewidth=5)
plt.show()
I want to reproduce this effect in gnuplot:
How can I achive it? If it can't be done, what software can I use to reproduce it?
Using a 2d kernel for every pixel can be done inside gnuplot. That way, more dense accumulations get brighter than single pixels. Check show palette rgbformulae and the respective chapter in the help to change the colours.
set term wxt size 300,300 background rgb 0
set view map
set samp 140
set dgrid3d 180,180, gauss kdensity2d 0.2,0.2
set palette rgbform 4,4,3
splot "+" us 1:(sin($1/3)**2*20):(1) with pm3d notitle
Disclaimer: It can be done with gnuplot as instructed in this answer but you should probably consider a different tool to draw this particular type of plot.
There is at least one way to do it, with preprocessing of the data. The idea is to mimic the glow effect by using a Gaussian kernel to smear the data points. Consider the following data, contained in a file called data:
1 2
1 2.1
1.1 2.2
2 3
3 4
I have purposely placed the first 3 points close to each other to be able to observe the intensified glow of neighboring points. These data look like this:
Now we smear the data points using a 2D Gaussian kernel. I have written the following python code to help with this. The code has a cutoff of 4 standard deviations (sx and sy) around each point. If you want the glow to be a circle, you should choose the standard deviations so that the sx / sy ratio is the same as the ratio of the x/y axes lengths in gnuplot. Otherwise the points will look like ellipses. This is the code:
import numpy as np
import sys
filename = str(sys.argv[1])
sx = float(sys.argv[2])
sy = float(sys.argv[3])
def f(x,y,x0,y0,sx,sy):
return np.exp(-(x-x0)**2/2./sx**2 -(y-y0)**2/2./sy**2)
datafile = open(filename, 'r')
data = []
for datapoint in datafile:
a, b = datapoint.split()
data.append([float(a),float(b)])
xmin = data[0][0]
xmax = data[0][0]
ymin = data[0][1]
ymax = data[0][1]
for i in range(1, len(data)):
if(data[i][0] < xmin):
xmin = data[i][0]
if(data[i][0] > xmax):
xmax = data[i][0]
if(data[i][1] < ymin):
ymin = data[i][1]
if(data[i][1] > ymax):
ymax = data[i][1]
xmin -= 4.*sx
xmax += 4.*sx
ymin -= 4.*sy
ymax += 4.*sy
dx = (xmax - xmin) / 250.
dy = (ymax - ymin) / 250.
for i in np.arange(xmin,xmax+dx, dx):
for j in np.arange(ymin,ymax+dy, dy):
s = 0.
for k in range(0, len(data)):
d2 = (i - data[k][0])**2 + (j - data[k][1])**2
if( d2 < (4.*sx)**2 + (4.*sy)**2):
s += f(i,j,data[k][0],data[k][1],sx,sy)
print i, j, s
It is used as follows:
python script.py data sx sy
where script.py is the name of the file where the code is located, data is the name of the data file, and sx and sy are the standard deviations.
Now, back to gnuplot, we define a palette that mimics a glowing pattern. For isolated points, the summed Gaussians yield 1 at the position of the point; for overlapping points it yields values higher than 1. You must consider that when defining the palette. The following is just an example:
set cbrange [0:3]
unset colorbox
set palette defined (0 "black", 0.5 "blue", 0.75 "cyan", 1 "white", 3 "white")
plot "< python script.py data 0.05 0.05" w image
You can see that the points are actually ellipses, because the ratio of the axes lengths is not the same as that of the standard deviations along the different directions. This can be easily fixed:
plot "< python script.py data 0.05 0.06" w image
Set a black background, and then plot your dataset several time in different colours with decreasing pointsize.
set term wxt backgr rgb "black"
plot sin(x) w p pt 7 ps 2 lc rgb 0x00003f not, \
sin(x) w p pt 7 ps 1.5 lc rgb 0x00007f not, \
sin(x) w p pt 7 ps 1 lc rgb 0x0000af not, \
sin(x) w p pt 7 ps .5 lc rgb 0x0000ff
Alternatively, some combination of splot with pm3d,set dgrid3d gauss kdensity2d, and set view map, combined with a suitable palette, can be used, see my other answer.
I'm trying to reproduce a figure I've found on a linear algebra book using gnuplot. This is the original image
You can see an intersection between two planes described by the two equations:
2u + v + w = 5
4u - 6v = -2.
I suppose that in order to plot the first equation using gnuplot I have to transform it in the form:
splot 5 - 2*x - y
where u -> x; v -> y and w -> z which is the free variable. But the result is very different from what expected. Any clue?
The approach you outline makes sense, however, the results may be far from what you expect.
I propose you draw single lines, using the arrow function in gnuplot.
This example will generate a plot very similar to the one you showed (only one plane, though):
set term gif
set output "demo_plane.gif"
# define your axis limits:
xmax = 6.5
xmin = -1.5
ymax = 8.5
ymin = -1.5
zmax = 5.5
zmin = -0.5
set xrange [xmin:xmax]
set yrange [ymin:ymax]
set zrange [zmin:zmax]
# remove the original axis
unset border
unset xtics
unset ytics
unset ztics
# define you data points:
x1 = 3.0
y1 = -1.0
z1 = 0.0
x2 = -1.0
y2 = 7.0
z2 = 0.0
x3 = -3.0
y3 = 7.0
z3 = 4.0
x4 = 1.0
y4 = -1.0
z4 = 4.0
# define 'arrow' without head:
set arrow 1 from x1,y1,z1 \
to x2,y2,z2 nohead
set arrow 2 from x2,y2,z2 \
to x3,y3,z3 nohead
set arrow 3 from x3,y3,z3 \
to x4,y4,z4 nohead
set arrow 4 from x4,y4,z4 \
to x1,y1,z1 nohead
# draw new axis manually (again, using arrow):
set arrow 5 from 0,0,0 \
to 6,0,0
set arrow 6 from 0,0,0 \
to 0,6,0
set arrow 7 from 0,0,0 \
to 0,0,5
# annotate axis labels:
set label "u" at 6.25,0,0
set label "v" at 0,6.25,0
set label "w" at 0,0,5.25
# plot will not show when empty, include dummy plot command:
set parametric
splot x1, y1, z1 not
With a little rotation you will get a figure like this: