Plotting two data lines together in J - j

I have a sequence of data, and can easily use plot in J to show it:
data1 =: 0 0.7 1 0.7 0 _0.7 _1 _0.7 0 0.7
plot data1
However, if I have another set of data:
data2 =: 0.7 0 _0.7 _1 _0.7 0 0.7 1 0.7 0
How do I display both plots together, e.g. (these obviously don't work):
plot data1 data2
plot data1 ; data2
And this one doesn't work either, but instead treats the data as two dimensional:
plot (data1 ; data2)

Multiple data can be plot together by arranging them in a table. eg:
data1,:data2
0 0.7 1 0.7 0 _0.7 _1 _0.7 0 0.7
0.7 0 _0.7 _1 _0.7 0 0.7 1 0.7 0
plot data1,:data2
Take a look at the plot wiki
Another way is to use pd utility
pd'new'
pd data1
pd data2
pd 'show'

Related

How do you sum every nth column of data in gnuplot?

I would like to take an average over several columns of a data set in Gnuplot. The problem is that I want to average every other column (starting from the second column of my dataset). I was thinking of using every somehow but I still don't really understand when and where to use every. To help visualise my question: my data looks something like this:
x y1 z1 y2 z2
2 0.6 0 0.6 0
1 0.7 0 0.7 1
1 0.8 2 0.8 1
1 0.9 0 0.9 0
and I would like to average y1 and y2 and plot the result by doing something like:
stats filename nooutput
plot filename u 1:sum[col = every :2::2::STATS_columns] / ((STATS_columns-1)/2)
Not sure if this is anywhere close to doable though. Also, it would be nice to have a way of finding the number of columns used without any apriori knowledge of what the data looks like. In the example I have used my knowledge of the data to know that the average is over ((STATS_columns-1)/2) number of points.
Thank you for your response
From your code I assume you want to average y1 and y2 for each row and then plot it versus x (column 1). Since you have several identical x values, there would be another average, namely an average over the columns and over all identical x values.
I modified your data to better illustrate the difference.
I guess you were asking fot the red circles. The blue triangles are basically the average of the average, i.e. the average of the red points.
Check help summation and help smooth. sum has no step size with the index.
From gnuplot help:
sum [<var> = <start> : <end>] <expression>
Code:
### average over columns and smooth
reset session
$Data <<EOD
#x y1 z1 y2 z2
1 2.0 0 4.0 0
1 2.2 0 4.2 1
1 2.9 2 4.9 1
2 2.1 0 4.1 0
2 2.3 0 4.3 0
2 2.8 0 4.8 0
3 2.2 0 4.2 0
3 2.3 0 4.3 0
3 2.7 0 4.7 0
EOD
stats $Data nooutput
set offsets 0.5,0.5,0.5,0.5
Count = (STATS_columns-1)/2
plot $Data u 1:((sum[i=1:Count] column(i*2))/Count) w p pt 7 lc rgb "red" ti "average over y1,y2 columns for each row",\
$Data u 1:((sum[i=1:Count] column(i*2))/Count) smooth unique w p pt 9 lc rgb "blue" ti "average over y1,y2 for each x"
### end of code
Result:

How to use an integer list to find rows in pd.DataFrame with non-integer indices

How can I make this work?
import pandas as pd
L = [1,3,5]
df = pd.DataFrame([1,2,3,4,5,6,7], index=[0.1,0.2,0.3,0.4,0.5,0.6,0.7])
print(df[0])
print(df[0].loc(L))
I would like to have this output format:
0.2 2
0.4 4
0.6 6
I think that is .iloc
df.iloc[L]
Out[477]:
0
0.2 2
0.4 4
0.6 6

Split pandas columns into two with column MultiIndex

I need to split DataFrame columns into two and add an additional value to the new column. The twist is that I need to lift the original column names up one level and add two new column names.
Given a DataFrame h:
>>> import pandas as pd
>>> h = pd.DataFrame({'a': [0.6, 0.4, 0.1], 'b': [0.2, 0.4, 0.7]})
>>> h
a b
0 0.6 0.2
1 0.4 0.4
2 0.1 0.7
I need to lift the original column names up one level and add two new column names. The result should look like this:
>>> # some stuff...
a b
expected received expected received
0 0.6 1 0.2 1
1 0.4 1 0.4 1
2 0.1 1 0.7 1
I've tried this:
>>> h['a1'] = [1, 1, 1]
>>> h['b1'] = [1, 1, 1]
>>> t = [('f', 'expected'),('f', 'received'), ('g', 'expected'), ('g', 'received')]
>>> h.columns = pd.MultiIndex.from_tuples(t)
>>> h
f g
expected received expected received
0 0.6 0.2 1 1
1 0.4 0.4 1 1
2 0.1 0.7 1 1
This just renames the columns but does not align them properly. I think the issue is there's no link between a1 and b1 to the expected and received columns.
How do I lift the original column names up one level and add two new column names?
I am using concat with keys , then swaplevel
h1=h.copy()
h1[:]=1
pd.concat([h,h1],keys=['expected', 'received'],axis=1).\
swaplevel(0,1,axis=1).\
sort_index(level=0,axis=1)
Out[233]:
a b
expected received expected received
0 0.6 1.0 0.2 1.0
1 0.4 1.0 0.4 1.0
2 0.1 1.0 0.7 1.0

Drawing polygons with holes using Gnuplot

Is there a way using Gnuplot 5.0 to plot a polygon with a hole with filledcurves?
Here are my test data:
# Outer ring
0 -2
-2 0
0 2
2 0
0 -2
# Inner ring
-0.5 0.5
-0.5 -0.5
0.5 -0.5
0.5 0.5
-0.5 0.5
And here is the result:
I know I could re-order the vertices in order to hide the connecting line (in fact the polygon frontier) between the outer and inner ring. But I will deal with machine-generated data, and I would prefer minimize the amount of data preprocessing.
In some other drawing programs, we can draw holes inside polygons by changing the winding-rule to even-odd. But I didn't find such option in gnuplot.
Finally, I cannot just draw the "hole" in white, since in my application I have several shapes to draw, And I want to see other shapes behind the "hole".
Why not just plot the hole in white on top of the shape?
Separate your date into
# shape.txt
0 -2
-2 0
0 2
2 0
0 -2
and
# hole.txt
-0.5 0.5
-0.5 -0.5
0.5 -0.5
0.5 0.5
-0.5 0.5
and then use
plot "shape.txt" u 1:2 w filledcurves, 'hole.txt' u 1:2 w filledcurves lc 'white'
The following solution does what you already mentioned in your question: It reorders the points of the inner polygon in such a way, that the endpoint of the outer polygon and the starting point of the inner polygon have minimal distance (hence no line across the inner polygon). It does is automatically with gnuplot only, so this still might be an acceptable solution for you.
Assumptions:
the outer curve is closed (start point = end point)
the outer curve does not have duplicated points
Procedure:
go through the data and when the first x and y values are found again, the outer structure is finished and the inner structure starts (row index stored in idx0)
when the inner structure starts, it looks for the minimum distance to the start/end point of the outer structure (x0,y0) (row index stored in idx1)
the data is plotted into a datablock $Hollow: first the outer as is and then the inner starting from idx1 to end, and then the inner starting from (idx0+2) to idx1.
In the example below red lines are drawn first to illustrate the empty polygon in the center. Tested with gnuplot 5.0.0.
For multiple holes in a filled curve you might want to check gnuplot: How to draw a filled area with hole? with a rather general but pretty lengthy solution.
Code:
### draw hollow polygon
reset session
$Data <<EOD
# Outer ring
0 -2
-2 0
0 2
2 0
0 -2
# Inner ring
-0.5 0.5
-0.5 -0.5
0.5 -0.5
0.5 0.5
-0.5 0.5
EOD
Distance(x0,y0,x1,y1) = sqrt((x1-x0)**2 + (y1-y0)**2)
GetIdxs(colX,colY) = LastOuter==1 ? ( d1=Distance(x0,y0,column(colX),column(colY)), \
d0==d0 ? (d1<dmin ? (idx1=column(0), dmin=d1) : 0) : \
(idx1=column(0),dmin=d1), d0=d1, LastOuter) : \
column(0)==0 ? (d0=d1=dmin=NaN,x0=column(colX),y0=column(colY)) : \
column(colX)==x0 && column(colY)==y0 && LastOuter==0 ? \
(idx0=column(0),x0=column(colX),y0=column(colY),LastOuter=1 ) \
: LastOuter
set table $Dummy
plot LastOuter=0 $Data u 1:2:(GetIdxs(1,2)):(d1) w table
set table $Hollow
plot $Data u 1:2 every ::::idx0 w table
plot $Data u 1:2 every ::idx1 w table
plot $Data u 1:2 every ::idx0+2::idx1 w table
unset table
plot for [i=-5:5] -x+i/5.lw 2 lc "red" notitle, \
$Hollow u 1:2 w filledcurves lc 1
### end of code
Result:
Just for better understanding the content of $Dummy and $Hollow.
$Dummy
0 -2 -2 nan
-2 0 0 nan
0 2 0 nan
2 0 0 nan
0 -2 1 nan
-0.5 0.5 1 2.54951
-0.5 -0.5 1 1.58114
0.5 -0.5 1 1.58114
0.5 0.5 1 2.54951
-0.5 0.5 1 2.54951
$Hollow
0 -2
-2 0
0 2
2 0
0 -2
-0.5 -0.5
0.5 -0.5
0.5 0.5
-0.5 0.5
-0.5 -0.5
My other solution is based on reordering the inner curve in order to avoid the connection line from outer to inner curve crossing the open area. However, this was rather cumbersome with dummy and extra tables and with a lengthy, confusing expression.
Here is a another much simpler solution:
It is based on variable linecolors, i.e. invisible (fully-transparent) lines, when you switch from the outer curve to the inner curve. This should be fine unless you want to create a SVG or DXF and use the graph as pattern for a cutter.
Prerequisites:
outer and inner curves must be closed (first point = last point)
you have to make sure that outer and inner curves have opposite directions (CW=clockwise and CCW=counterclockwise), this was the case in your example. Otherwise the area will not be hollow but filled.
there should be no empty line between inner and outer curves, but two empty lines between the shapes.
You can easily tell gnuplot when the outer curve ends: when the first x,y-coordinates appears again. It doesn't matter where the inner curve starts as long as the direction is reversed to the outer one.
Just for illustration, I plotted sin(x) and made the 1st structure opaque and 2nd and 3rd structure in semitransparent color.
Script: (works for gnuplot>=5.0.3)
### plot areas with holes
reset session
$Data <<EOD
0 -2 0 # outer CW
-2 0 0
0 2 0
2 0 0
0 -2 0
-0.5 0.5 1 # inner CCW
-0.5 -0.5 1
0.5 -0.5 1
0.5 0.5 1
-0.5 0.5 1
-0.5 -1 0 # outer CCW
-0.1 -0.1 0
-1 0.5 0
-1.5 -1 0
-0.5 -1 0
-0.8 0.2 1 # inner CW
-0.2 -0.1 1
-0.6 -0.8 1
-1.2 -0.8 1
-0.8 0.2 1
0 -1 0 # outer CCW
1 -1 0
1.3 0 0
0.3 0 0
0 -1 0
1.1 -0.2 1 # inner CW
0.9 -0.8 1
0.2 -0.8 1
0.4 -0.2 1
1.1 -0.2 1
EOD
myColors = "0x00ff00 0x770000ff 0x77ff0000"
myColor(i) = i+1>words(myColors) ? 0x000000 : int(word(myColors,i+1))
myColorX(i) = (x0=x1, x1=$1, y0=y1, y1=$2, \
$0==0 ? (fx=x1,fy=y1) : x1==fx && y1==fy ? last=1 : 0, \
last ? 0xff123456 : myColor(i))
set key noautotitle
set multiplot layout 1,2
set title "fixed linecolor"
plot 2*sin(2*x) lw 2, \
for [i=0:2] $Data u 1:2:(myColor(i)) index i w filledcurves lc rgb var lw 2
set title "variable line color with fully transparent lines"
plot 2*sin(2*x) lw 2, \
for [i=0:2] x1=y1=(last=0,NaN) $Data u 1:2:(myColorX(i)) index i \
w filledcurves lc rgb var lw 2
unset multiplot
### end of script
Result:

Heatmap with Gnuplot on a non-uniform grid

I would like to create a heatmap with gnuplot based on a non-uniform grid, meaning that my x axis bins do not have all the same width, and I can't figure out how to do that because when I plot my data with for example "with image" I get uniformly sized boxes which do no correspond to my coordinates at all (because "image" treats the data just as matrix I guess). So I would like to find a method to get non-uniform boxes which are also positioned in the right place on the Cartesian plane.
My data look something like this:
1 1 0.2
1 2 0.8
1 3 0.1
1 4 0.2
2 1 0.7
2 2 0.2
2 3 0.3
2 4 0.1
5 1 0.2
5 2 0.4
5 3 0.1
5 4 0.9
7 1 0.3
7 2 0.2
7 3 0.9
7 4 0.6
If I run this command on Gnuplot
set xrange [1:10]
p 'mydata.dat' with image
I get an image with 16 boxes that have the same width and height (apparently I don't have enough "reputation" on Stackoverflow to post an image, otherwise I would), but ideally I would like the boxes to have different widths and be in the right place on the plane. For example the first box should range from 1 to 2, the second one from 2 to 5, the third one from 5 to 7, and the last one from 7 to 10 (which is why I wrote set xrange [1:10]).
Could anyone help me please? Thank you very much!
The easiest (maybe only viable) way is to add some dummy data points and use splot ... with pm3d. This plotting style handles heatmaps with general quadrangles.
The image plotting style plots one box (one big pixel) for each data point, while pm3d takes each data point as corner of one or more quadrangles. The color of each quadrangles is determined by the values of the corners and is adjustable with set pm3d corners2color.
So, in your case you need to expand the 4x4 matrix to a 5x5 matrix (expand to right and top), but select the lower left corner to determine the color set pm3d corners2color c1.
The changed data file is then:
1 1 0.2
1 2 0.8
1 3 0.1
1 4 0.2
1 5 0.5
2 1 0.7
2 2 0.2
2 3 0.3
2 4 0.1
2 5 0.5
5 1 0.2
5 2 0.4
5 3 0.1
5 4 0.9
5 5 0.5
7 1 0.3
7 2 0.2
7 3 0.9
7 4 0.6
7 5 0.5
10 1 0.5
10 2 0.5
10 3 0.5
10 4 0.5
10 5 0.5
To plot it use
set pm3d map corners2color c1
set autoscale fix
set ytics 1
splot 'mydata.dat' using 1:($2-0.5):3 notitle
The result with 4.6.3 is:
In general, the z-value of the dummy data points doesn't matter, but in the above script it should lay somewhere between minimum and maximum values to allow set autoscale fix to work properly on the color scale.
If you don't want to change the data file manually, you could do it with some script, but that's a different question.
Here is an alternative solution without splot ... pm3d, but with boxxyerror.
If you plot data it should go as automatic as possible and there should be no need to "invent" and manually add data.
The following solution (a little bit more complex) takes care about the widths (+/-dx) and heights (+/-dy) of the boxes according to the following principle:
if it is an "inner" box, take half the distance to the adjacent datapoint on that side
if it is an "outer" box, take half the distance to the adjacent "inner" datapoint
Here, x-distances are irregular and y-distances are regular, but y-distances could also be irregular.
Data: SO19294342.dat
1 1 0.2
1 2 0.8
1 3 0.1
1 4 0.2
2 1 0.7
2 2 0.2
2 3 0.3
2 4 0.1
5 1 0.2
5 2 0.4
5 3 0.1
5 4 0.9
7 1 0.3
7 2 0.2
7 3 0.9
7 4 0.6
Script: (works with gnuplot>=4.6.0, March 2012)
### heatmap with boxxyerror and variable box-sizes
reset
FILE = "SO/SO19294342.dat"
set style fill solid 1.0
set tics out
set size ratio -1
# extract x-positions
Xs = Ys = ''
Nx = Ny = 0
b = -1
stats FILE u (column(-1)!=b ? (Nx=Nx+1, Xs=Xs.sprintf(" %g",$1), b=column(-1)) : 0, \
column(-1)==0 ? (Ny=Ny+1, Ys=Ys.sprintf(" %g",$2)) : 0) nooutput
d(vs,n0,n1) = abs(real(word(vs,n0))-real(word(vs,n1)))/2
dn(vs,n) = (n==1 ? (n0=1,n1=2) : (n0=n,n1=n-1), -d(vs,n0,n1))
dp(vs,n) = (Ns=words(vs), n==Ns ? (n0=Ns-1,n1=Ns) : (n0=n,n1=n+1), d(vs,n0,n1))
plot FILE u 1:2:($1+dn(Xs,column(-1)+1)):($1+dp(Xs,column(-1)+1)):\
($2+dn(Ys,int(column(0))%Ny+1)):($2+dp(Ys,int(column(0))%Ny+1)):3 w boxxy palette notitle
### end of script
For gnuplot>=4.6.5 you could add :xtic(1):xtic(2) to the plot command to only show your x- and y-coordinates as x,y-ticlabels.
plot FILE u 1:2:($1+dn(Xs,column(-1)+1)):($1+dp(Xs,column(-1)+1)):\
($2+dn(Ys,int(column(0))%Ny+1)):($2+dp(Ys,int(column(0))%Ny+1)):3:\
xtic(1):ytic(2) w boxxy palette notitle
And for gnuplot>=5.0.0 you could add noextend to the ranges to avoid white areas on the sides:
set xrange[:] noextend
set yrange[:] noextend
Result: (created with gnuplot 4.6.0)

Resources