Rotate label by variable angle in polar plot - gnuplot

I plotted many points with labels in an polar coordinate system. Now I want to rotate the labels in the tangential direction. So I have to rotate
a point plotted at t=0, r=arbitrary by 0 degrees,
a point plotted at t=pi/2, r=arbitrary by 90 degrees,
a point plotted at t=pi, r=arbitrary by 180 degrees and so on.
I found the 'rotate by' function here: http://gnuplot.sourceforge.net/demo/textrotate.html
But only fixed values for the rotate angle are possible.
I thought about sth. like:
plot 'point-data.dat' using ($1/360*2*pi):($2):($1) with labels rotate by variable font "arial,8"
Is it possible?

I tried to do the same thing and found a "workaround" to do this. It is not perfect but it worked in my case:
You can do many plots in a loop and assign a different text angle for each plot:
data file data.txt:
#alpha r label rotation
0 1 text1 10
.2 1 text2 20
.4 1 text3 30
.6 1 text4 40
.8 1 text5 50
1 1 text6 60
Plotting commands:
isequal(x1,x2,x3)=(x1==x2?x3:1/0)
setonly(x1,x2,x3)=((x1==x2)?roto=x3:1/0)
set polar
plot for [k=0:7] "data.txt" u (isequal(($0),k,($1))):((setonly( ($0),k,($4) )*0.0)+($2)):3 w labels rotate by roto

Related

gnuplot: label overflowing outside canvas isn't drawn

I'm putting labels on top (offset by 10) of histogram bars like this:
plot "hist.txt" using 1:2 with boxes ls 1, "hist.txt" using 1:($2+10):(sprintf("%.1f%%",$2)) with labels
When the bar value is 87%, it draws the label.
When the bar value is 92%, the label isn't drawn, probably since there isn't enough space left. How do I tell it to draw the label anyway, and I don't mind if it would overflow the intended canvas size?
Assuming you limit the range of the y-axis to 0~100, the y-value of 92 + 10(= 102) will not be drawn because it exceeds the maximum value of yrange.
It works well if you use with labels offset 0, first 10 as the plot style specification instead of shifting the position of the label in using.
set key noautotitle
set tmargin screen 0.85
set xrange [0:11]
set yrange [0:100]
plot "hist.txt" using 1:2 with boxes ls 1, \
"hist.txt" using 1:2:(sprintf("%.1f%%",$2)) with labels offset 0,first 10
Sample "hist.txt" is,
1 30
2 60
3 87
4 92
5 50
6 20
7 10
8 30
9 50
10 40

Gnuplot stretched ellipse by angle option

I want to draw an ellipse with gnuplot. It should be slightly tilted, hence I use the angle option.
But the angle messes with the size of the ellipse. Am I getting something wrong in the docu?
See the following example:
set output "test.pdf"
set obj ellipse center 5,0.5 size 4,0.2 angle 20
set xrange [0:10]
plot sin(x)
The added two png's show my results. I use version 5.2.8.
If you want the proportions of the ellipse to remain constant after rotation you must give the major and minor diameters in the same units, e.g. "units xx" or "units yy". Here I define the ellipse in terms of a 4:1 ratio of major:minor axis using x coordinates.
set obj 1 ellipse center 5,0.5 size 4,1 fs empty bo lc "blue" angle 0 units xx
set obj 2 ellipse center 5,0.5 size 4,1 fs empty bo lc "red" angle 20 units xx
set obj 3 ellipse center 5,0.5 size 4,1 fs empty bo lc "green" angle 40 units xx
set xrange [0:10]
plot sin(x)
Edit: Ethan's solution is certainly the way to go.
Just keep in mind, if you don't have set size ratio -1, a line, e.g. like plot x will not appear in a 45° degree angle to the x-axis.
Probably, the simplest solution would be to set size ratio -1 to have the same scale for x and y-axis. If you need different scale there should be a different solution.
Code:
### draw tilted ellipse with same proportions
reset session
set size ratio -1
set obj 1 ellipse center 5,0.5 size 4,0.2 angle 0 fc "red"
set obj 2 ellipse center 5,0.5 size 4,0.2 angle 20 fc "green"
set obj 3 ellipse center 5,0.5 size 4,0.2 angle 45 fc "blue"
set obj 5 ellipse center 5,0.5 size 4,0.2 angle 90 fc "magenta"
set xrange [0:10]
set yrange [-3:3]
plot sin(x)
### end of code
Result:
If the output to a dummy file is allowed, how about the following solution.
set terminal pdf size 10cm,10cm
set xrange [0:10]
# dummy plotting
set output "dummy.pdf"
plot sin(x)
factor = real(GPVAL_X_MAX - GPVAL_X_MIN)/real(GPVAL_Y_MAX-GPVAL_Y_MIN) \
* real(GPVAL_TERM_YMAX - GPVAL_TERM_YMIN)/real(GPVAL_TERM_XMAX - GPVAL_TERM_XMIN)
# real plotting
set output "test.pdf"
set obj ellipse center 5,0.5 size 4,factor*0.2 angle 20 unit xx
replot
In this script, the ratio of visible unit length between x and y axis is calculated using from variable GPVAL_* determined by the dummy plotting. Multipling it to vertical axis of the ellipse and using 'unit xx' as same as Ethan's answer, you may get the figure that you want.
If you are on UNIX(-like) system, replace 'set output "dummy.pdf"' as,
set output "/dev/null"

gnuplot not showing correct scale on y axis for a circle

I am modelling a graph problem using gnuplot
I am plotting a circle with gnuplot using the following command
set xtics 1
set ytics 1
plot 'circles.txt' with circle
my circles.txt contains the follwoing data
0 0 3
the upmost point on this circle(center at origin and radius of 3 ) should be (0,3) but it is shown as (0,2) in this graph
how can i rectify this error?
Plotting with circles is intended for plotting the points as circles so they would be round regardless of axes scaling. As it is pointed in gnuplot documentation,
The radius is always interpreted in the units of the plot's horizontal axis (x or x2). The scale on y and the aspect ratio of the plot are both ignored.
You can plot with ellipses instead; from documentation on plot with ellipses:
2 columns: x y
3 columns: x y major_diam
4 columns: x y major_diam minor_diam
5 columns: x y major_diam minor_diam angle
so you plot this as
plot 'circles.txt' using 1:2:($3*2):($3*2) with ellipses
(ellipses use diameter, so the size should be the third column twice)
Or set object ellipse:
set object ellipse at 0, 0 size 6, 6

Can You Calculate the Area of a Contour in Gnuplot?

I've been using gnuplot for a couple of weeks now. I have large data files with 23 variables, but I select specifically x-y co-ordinate data and fluorescence intensity data for my analysis.
On of the things I would like to do is a contour plot of my fluorescing particles. I should add that this contour plot is over time so there will be several spots nearly overlapping, but this is in fact the same particle. I would like to draw contours around these spots, colour code according to intensity and have the area of the contour displayed on the graph.
I have achieved all but one of these goals for my contour plot. I cannot devise a way for gnuplot to calculate and display the area within the contour. If I could then I would have an estimate of the area of my particle. I recognise my goal may be beyond the capabilities of gnuplot, but if there were a solution then it would be very neat.
Here is my script for the contour plot which as I said gives everything I need bar the area within contours.
The co-ordinates are in nanometres and each point on the dataset is the centre of a molecule. I have taken a small range of co-ordinates because there is so much data, it would not be possible to distinguish otherwise (there are over 80 000 data points). I have also set a threshold of intensity as I only want relatively bright fluorescent particles (done with set cntrparam levels incremental 8000,5000,100000). $23 and $24 are the x and y co-ordinates respectively. $12 is the intensity.
#Contour plot of Fluorescent Particle Location with Intensity
#Gnuplot script file for plotting data in file "1002 all.txt"
reset
set dgrid3d 100,1000,1
set pm3d
set isosample 30
set xlabel 'x (nm)'
set ylabel 'y (nm)'
set contour base
set cntrparam levels incremental 8000,5000,100000
unset key
unset surface
set view map
set xrange[20000:22000]
set yrange[7000:10000]
splot "1002 all.txt" using ($23<22000 && $23>20000 ?$23 : 1/0):$24<10000 && $24>7000 ?$24 : 1/0):12 with lines
set terminal push
set terminal png
set output "1002_all_fluorophores_section_contour.png" # set the output filename
set terminal png size 1280,760
replot
set output
As #Christoph says, gnuplot might not be a numerical tool, however, the calculation of a polygon area is not too complicated and can easily be done with gnuplot only. Assumption is that you have closed polygons, i.e. last point == first point, and the data of the individual polygons is separated by two empty lines.
edit: script changed to work with gnuplot 4.6.0 as well.
Data: SO28173844.dat
1 1
2 1
2 2
1 2
1 1
3 1
5 4
9 0
8 4
7 4
9 8
6 8
4 9
0 6
3 1
4 0
5 3
7 1
4 0
Script: (works for gnuplot>=4.6.0, March 2012)
### calculate areas of closed polygons
reset
FILE = "SO28173844.dat"
set size ratio -1
set style fill solid 0.3
set grid x,y front
set key noautotitle
stats FILE u 0 nooutput # get number of blocks, i.e. polygons
N = STATS_blocks
getArea(colX,colY) = ($0==0?(Area=0, x1=column(colX), y1=column(colY)) : 0, \
x0=x1, y0=y1, x1=column(colX), y1=column(colY), Area=Area+0.5*(y1+y0)*(x1-x0))
getMinMax(colX,colY) = (x2=column(colX), y2=column(colY), $0==0? (xMin=xMax=x2, yMin=yMax=y2) : \
(x2<xMin?xMin=x2:0, x2>xMax?xMax=x2:0, y2<yMin?yMin=y2:0, y2>yMax?yMax=y2:0))
Areas = Centers = ''
do for [i=1:N] {
stats FILE u (getArea(1,2),getMinMax(1,2)) index i-1 nooutput
Areas = Areas.sprintf(" %g",abs(Area))
Centers = Centers.sprintf(' %g %g',0.5*(xMin+xMax),0.5*(yMin+yMax))
}
CenterX(n) = real(word(Centers,int(column(n))*2+1))
CenterY(n) = real(word(Centers,int(column(n))*2+2))
Area(n) = real(word(Areas,int(column(n)+1)))
myColors = "0xff0000 0x00ff00 0x0000ff"
myColor(i) = sprintf("#%06x",int(word(myColors,(i-1)%words(myColors)+1)))
plot for [i=1:N] FILE u 1:2 index i-1 w filledcurves lc rgb myColor(i), \
'+' u (CenterX(0)):(CenterY(0)):(sprintf("A=%g",Area(0))) every ::0::N-1 w labels center
### end of script
Result:

gnuplot: smooth line in polar coordinates

I tried to plot some data in polar coordinates with gnuplot and then draw a smooth line.
data.dat:0 10
20 15
40 40
60 80
80 140
100 140
120 80
140 40
160 15
180 10
gnuplot:
set polar
set angle degrees
set grid
set grid polar
plot "data.dat" smooth csplines
I expected gnuplot to draw ovoid curve between points, but it drew strange line from left to right ignoring polar coordinates. Do you think there is any solution?
csplines are cubic so that's the best you can get with them. Try
plot 'data.dat' smooth bezier
but even then, you can't achieve much with such a small data set.
There is another trick you can try but it improves things only a little: you first output a table of the data generated by smooth bezier normal plot and then plot them polar:
# save smooth bezier data
set table
set output 'b_data.dat'
plot 'data.dat' smooth bezier
# plot
unset table
set term x11
set polar
set angle degrees
set grid
set grid polar
plot "b_data.dat" w lines

Resources