join points with arrow and label - plot

My data file is like this:
x1 y1 x2 y2 id1 id2 value
x1 y1 x2 y2 id1 id2 value
x1 y1 x2 y2 id1 id2 value
.
.
.
I would like to joint the two points (x1,y1) and (x2,y2) with an arrow goes from 1 to 2.
Also i would like to:
add near the point 1 "id1" and near the point 2 "id2"
add a label with value in the middle of the arrow.

Use the vectors plotting style for the arrows and label to add all the labels:
plot 'data.txt' using 1:2:($3-$1):($4-$2) with vectors t '', \
'' using 1:2:5 with labels offset char 0,1 t '',\
'' using 3:4:6 with labels offset char 0,1 t '',\
'' using (0.5*($1+$3)):(0.5*($2+$4)):7 with labels offset char 0,1 t ''
Plotting with vectors requires the starting point an the arrow length, $1 accesses the values of the first column for calculations, ($3-$1) is the x-length of the arrow.
offset char 0,1 puts the label at a vertical offset of one character height above the given point. For more help see help vectors and help labels.

Related

How to plot a graph of points generated by two vectors in Julia?

I have two vectors where each corresponding position of each vector represents a point, for example position 1 of one vector has a value of x1 and position 1 of the other vector has a value of y1, and these values (x1 , y1) represent a point. And so on with the points (x2, y2), (x3, y3)... I need to plot a graph with this set of points.
How can I do this in Julia?
You just do scatter(x,y) to draw the points
x = [1,2,3,4];
y = [4,7,5,9];
using Plots
scatter(x,y; legend=:topleft)

Lines Between Datapoints of different columns

I have these example-data saved in test.txt
1 2
2 3
3 5
4 5
5 6
I can easily plot them by
plot "test.txt" u 0:1,"" u 0:2
the result is
just the data plotted
but what i want is some lines between the datapoints to illustrate the difference and which points belong to the same x-value. (photoshopped scribble below)
same image with lines between datapoints
Is there any way i could achieve this with gnuplot?
Use the vectors style
plot "test.txt" u 0:1,"" u 0:2, "" u 0:1:(0):($2-$1) with vectors nohead
The vectors style expects 4 values which are the x and y coordinates, the x change (0 in this case), and the y change (the difference between your 2 columns). The nohead option removes the arrow head that is normally added.
You can style the vector using most line style specifications, and if necessary, can adjust coordinates to add some spacing. For example, setting the lines black and padding the lines by 0.1 in both directions by
plot "test.txt" u 0:1, "" u 0:2, "" u 0:($1+0.1):(0):($2-$1-0.2) with vectors nohead lc "black"
gives
I manually set ranges with set xrange and set yrange to match your images, as mine was using slightly different defaults.
See help vectors for more information on the vectors style.

Marking coordinates of the points plotted using gnuplot

I want to plot my graph that connects a few point using 'linespoints'
I also want a label against each of the plotted point marking the coordinate of the plotted point. If possible draw a line along x and y axis marking the coordinates of the plotted points.
Any help?
You can use the with vectors option to draw arrows from column 1 & 2 with the length given in column 3 & 4. The nohead removes the arrow tips. And with labels you can place a string given as third column. The left causes left aligned text (i.e. right of the coordinates), and the offset moves the text one character width to the right.
plot "data.csv" u 1:2 with linespoints, \
'' u 1:2:(0):(-$2) with vectors, \
'' u 1:2:(-$1):(0) with vectors nohead, \
'' u 1:2:(sprintf("x=%.1f; y=%.1f", $1, $2)) with labels left offset 1, 0

gnuplot pm3d plot triangle data

i'm trying to plot an heatmap on a triangular surface, the coordinates and "heat values" are obtained with the methog shown on page staff.aist.go.jp/a.noda/programs/ternary/ternary-en.html.
so, i process the data and obtain a data file in the form:
x y val
where x and y are values between 0 and 1, and val is an integer representing the frequency i need to show.
the data file is this: http://tinyurl.com/lqsqtvv
and the plot script is this:
#!/usr/bin/gnuplot
reset
set terminal pngcairo size 640,480
set output 'heat_map_triangle.png'
set border linewidth 0
unset tics
set bmargin 3
set lmargin 3
set rmargin 3
set tmargin 3
set dgrid3d
set pm3d map
#set pm3d ftriangles
set pm3d interpolate 0,0
set pm3d at bs
set label 'Y' at 0, -0.03 center
set label 'Z' at 1, -0.03 center
set label 'X' at 0.5, 0.886 center
set style line 1 lt 1 lw 3 pt -1 ps 1
# x
set arrow 1 from 0,0 to 1, 0.0 nohead linestyle 1
# z
set arrow 11 from 1, 0 to 0.50, 0.866 nohead linestyle 1
# y
set arrow 21 from 0.50, 0.866 to 0,0 nohead linestyle 1
splot "./triangle.out" using 1:2:3
so, i'm getting out this plot
that is not exactly what i wanted...
i can't understand how to tell pm3d not to fill zones that are not in the data file (e.g. outside the triangle) and why the triangle top edge is taller than the heatmap.
it there a way to plot the data the way i want it?
in pm3d documentation it says that it can leave empty spaces, but how?
thanks
Ultimately, for pm3d to work, gnuplot requires that the data be on some sort of grid "mesh". The mesh needs to composed of quadrillaterals, but that is the only stipulation. e.g., your gridpoints could be arranged like this:
1
2
3
4 5 6 10
9
8
7
In this case, gnuplot will create a quadrilateral from points 1-2-4-5 and points 2-3-5-6, etc. etc. Gnuplot will color the quadrilateral depending on the corners2color option of pm3d. By default, it uses the average of the 4 values on the corners of the cells.
To put this in a datafile, you'd want to the datafile coordinates like this:
x1 y1 z1
x2 y2 z2
x3 y3 z3
x10 y10 z10
x4 y4 z4
x5 y5 z5
x6 y6 z6
x10 y10 z10
x7 y7 z7
x8 y8 z8
x9 y9 z8
x10 y10 z10
Notice how I left a blank line between horizontal "scans" across the data. (Of course, we could have structured the datafile to take vertical "scans" across the data as well). I also repeated a point at the right vertex of my triangle to give it a sharp point. This isn't strictly necessary, but I wanted to demonstrate it was possible.
Your data isn't in that form and normally, gnuplot would give you an error complaining that your data wasn't gridded. however, you've added the line set dgrid3d which tells gnuplot that your data isn't on a grid and that gnuplot should use an inverse distance weighting function to interpolate your data onto a grid. Unfortunately, gnuplot creates a regular (rectangular) grid and there is no way to tell it to create some other kind of grid. Ultimately, you need to figure out how to beat your data into this form.
If you were prepared to use R, and the ggtern library, the following could be achieved:
Which was done with the following code:
#Load library
library(ggtern)
#Load the data
df <- read.table("./data.txt")
colnames(df) = c("x","y","Value")
#Put in ternary coordinates
df.new <- data.frame(transform_cart_to_tern(data=df),Value=df$Value)
df.new <- df.new[order(df.new$Value),]
df.new <- df.new[which(df.new$Value > 0),]
#Plot the diagram
ggtern(data=df.new,aes(y=T,x=L,z=R)) +
geom_point(aes(color=Value,alpha=Value)) +
scale_color_gradient(low="transparent",high="red") +
guides(alpha="none") +
theme_rgbw() +
theme(legend.position=c(0,1),legend.justification=c(0,1)) +
labs(title="Example Density Plot",color="Frequency")

What is the equation stands for in geometry(intuitively)?

I am writing a bilinear interpolation method.
This method can be abstract by solve the equation A*x = b, A is a 4x4 matrix below:
1 x1 y1 x1*y1
1 x2 y2 x2*y2
1 x3 y3 x3*y3
1 x4 y4 x4*y4
Here, (x1, y1), (x2, y2), (x3, y3) and (x4, y4) is four points containing the dst interpolation point.
My problem is when det(A) = 0(then x! = A-1*b), what is the quadrangle looks like?
The determinant becomes 0 when one of the rows or columns can be expressed as a linear combination of the others. Using columns, this equation must hold for some constants a, b, c for each of the four points:
a*1 + b*x + c*y + xy = 0
This is the equation a hyperbola with asymptotes parallel to the axis, so the determinant is zero if and only if the four points fall on the same hyperbola.
For example, if you pick the rectangle (-2, -1), (-1, -2), (1, 2), (2, 1) the determinant will be zero since the points fall on the hyperbola defined by t → (t, 2/t).
Another way to look at it: you are free to pick any 3 points. The three points define a unique hyperbola. The determinant is 0 if and only if you pick the fourth point from that hyperbola.
Joni's answer above is entirely correct, but here's a physical interpretation that you might like:
Picture a square as a well-behaved quadrilateral defined by the following four points: 1 = (0,0), 2 = (1,0), 3 = (1, 1), and 4 = (0, 1).
If you start skewing it by anchoring points 1 and 2 but tugging point 3 to the right in such a way that the sides remain the same length, but the angle between the x-axis and the line segment between points 2 and 3 changes from 90 degrees to 180 and the angle between the x-axis and the line segment between points 1 and 4 changes from 90 degrees to 0, the determinant will approach zero as the angle increases. When you have points 1 = (0,0), 2 = (1, 0), 3 = (2,0), and 4 = (1,0) the quad will be collapsed to a line segment and the determinant will be zero.
You can run this experiment with your matrix and see if I'm correct.

Resources