I have a dataframe with 217 rows:
df
A C
fm ae fm ae
0 0.491 0.368 0.789 0.789
1 0.369 0.333 0.433 0.412
2 0.372 0.276 0.772 0.759
3 0.346 0.300 0.474 0.391
4 0.213 0.161 0.323 0.312
.. ... ... ... ...
212 0.000 0.000 1.000 1.000
213 1.000 1.000 1.000 1.000
214 1.000 1.000 1.000 1.000
215 1.000 1.000 1.000 1.000
216 1.000 1.000 1.000 1.000
[217 rows x 4 columns]
I need to report the mean results, and I get the following results:
df.mean()
A fm 0.548
ae 0.508
C fm 0.671
ae 0.650
dtype: float64
But I would like the output to look like this:
fm ae
A 0.548 0.508
C 0.671 0.650
I tried with df.mean().groupby(level=0) but the output was none.
What is the cleanest way to achieve this organization of aggregate results?
One way is to unstack it:
out = df.mean().unstack()
or you could stack it, then use groupby + mean:
out = df.stack(level=0).groupby(level=1).mean()
Output:
ae fm
A 0.5438 0.5791
C 0.7663 0.7791
I am trying to plot a pm3d map using both the primary and secondary axes of gnuplot (version 5.2 patchlevel 8). However, the y2label is missing and the y2tics do not appear after three marks. It would be very helpful if anyone can kindly point out any mistake I have made. The script used is given below and the figure obtained is attached for your reference.Thank you in advance.
set term postscript
set output "map.ps"
set pm3d map corners2color c1
set palette maxcolors 8
set palette defined (0 "#FFFFFF", 1 "#FFFF00", 2 "#FFC020", 3 "#FF69B4", 4 "#4169E1", 5 "#008000", 6 "#20B2AA", 7 "#9ACD32")
set lmargin at screen 0.095
set rmargin at screen 0.795
unset key
# set x-axis
#*******************************************************
set xlabel "Time (ns)" font "Times-italic,18" offset 0, -0.5, 0
set xrange [0.000: 7500.000]
set xtics ("0" 0.000, "50" 1250.0, "100" 2500.0, "150" 3750.0, "200" 5000.0, "250" 6250.0, "300" 7500.0) font "Times-italic,16"
# set y-axis
#*******************************************************
set ylabel "Peptide Number" font "Times-italic,18" offset -1.2, 1, 0
set yrange [2.0: 36.0]
set ytics ("1" 5.0, "2" 12.0, "3" 19.0, "4" 26.0, "5" 33.0) font "Times-italic,16"
set ytics nomirror
set y2label "Residue Number" font "Times-italic,18" offset -2.5, 0, 0
set y2range [2.0: 36.0]
set y2tics nomirror ("1" 2.0, "7" 8.0, "1" 9.0 , "7" 15.0, "1" 16.0, "7" 22.0, "1" 23.0, "7" 29.0, "1" 30.0, "7" 36.0) font "Times-italic,14"
# set cb-axis
#*******************************************************
set colorbox vertical user origin 0.890, .147 size .04, .73
set cbrange [-0.500: 7.500]
set cbtics 0.000, 7.000, 1.0
set cbtics("None" 0.000,"Ext" 1.000,"Bridge" 2.000,"3-10" 3.000,"Alpha" 4.000,"Pi" 5.000,"Turn" 6.000,"Bend" 7.000)
set link y2
splot "-" with pm3d title "map.gnu"
1.000 1.000 2
1.000 2.000 0
1.000 3.000 0
1.000 4.000 0
1.000 5.000 0
1.000 6.000 3
1.000 7.000 0
1.000 8.000 0
1.000 9.000 0
1.000 10.000 0
1.000 11.000 0
1.000 12.000 0
1.000 13.000 0
1.000 14.000 1
1.000 15.000 0
1.000 16.000 0
1.000 17.000 6
1.000 18.000 0
1.000 19.000 7
1.000 20.000 0
1.000 21.000 0
1.000 22.000 4
1.000 23.000 0
1.000 24.000 0
1.000 25.000 6
1.000 26.000 6
1.000 27.000 5
1.000 28.000 0
1.000 29.000 0
1.000 30.000 4
1.000 31.000 0
1.000 32.000 0
1.000 33.000 7
1.000 34.000 0
1.000 35.000 0
1.000 36.000 0
2.000 1.000 0
2.000 2.000 0
2.000 3.000 0
2.000 4.000 2
2.000 5.000 4
2.000 6.000 5
2.000 7.000 0
2.000 8.000 0
2.000 9.000 6
2.000 10.000 0
2.000 11.000 0
2.000 12.000 0
2.000 13.000 7
2.000 14.000 0
2.000 15.000 0
2.000 16.000 0
2.000 17.000 0
2.000 18.000 0
2.000 19.000 8
2.000 20.000 0
2.000 21.000 0
2.000 22.000 0
2.000 23.000 0
2.000 24.000 9
2.000 25.000 6
2.000 26.000 6
2.000 27.000 6
2.000 28.000 0
2.000 29.000 5
2.000 30.000 0
2.000 31.000 0
2.000 32.000 0
2.000 33.000 7
2.000 34.000 0
2.000 35.000 0
2.000 36.000 0
I assume from your code that you have simple x,y,z data.
x with sample numbers from 0 to 7500 which correspond to 0 to 300 ns.
y in the range from 1 to 6
z in the range from 0 to 7
For the example below I create some random test data.
I can reproduce your issue with splot ... with pm3d, however, do not (yet) have a solution.
Therefore, my suggestion would be to use plot ... with boxxerror.
If you plot that many boxes, your PostScript file probably will get pretty large. So, I would recommend to use a pixel graphic with sufficient resolution, e.g. set term pngacairo size 1400,1000.
Code:
### "plot ... with boxxyerror" instead of "splot ... with pm3d"
reset session
set palette maxcolors 8
set palette defined (0 "#FFFFFF", 1 "#FFFF00", 2 "#FFC020", 3 "#FF69B4", 4 "#4169E1", 5 "#008000", 6 "#20B2AA", 7 "#9ACD32")
# create some random test data
set print $Data
do for [i=1:7500] for [j=1:5] {
print sprintf("%g %g %g", i, int(rand(0)*6)+(j-1)*7+2, int(rand(0)*8))
}
set print
set xrange [0:300]
XScale = 300/7500. # scaling between sample and x-axis units
set yrange [1: 37]
set for [i=1:5] ytics (sprintf("%g",i) i*7-2)
set ytics nomirror
set y2range [1: 37]
set for [i=1:5] y2tics ("1" i*7-5, "7" i*7+1)
set y2tics nomirror
set link y2
set cbrange [-0.500: 7.500]
set cbtics 0.000, 7.000, 1.0
myCbTics = "None Ext Bridge 3-10 Alpha Pi Turn Bend"
set for [i=0:words(myCbTics)] cbtics (word(myCbTics,i+1) i)
set key noautotitle
set tics out
set style fill solid 1.0
plot $Data u ($1*XScale):2:($1*XScale):(($1+1)*XScale):($2):($2+1):3 w boxxy palette z
### end of code
Result:
I do not know why the default link command set link y2 is failing, but it seems to work if you explicitly give the longer form set link y2 via y inv y.
Aside from that, note that set link y2 command will immediately replace any limits on y2range with limits matching the y1 range, so the set y2range command in your script as shown has no effect.
Hello guys I am new to gnuplot and im looking to plot a gif representing the evolution of some probabilities with cycles.
My data file is structured like this:
0 0 1 3.56133e-008 2 1.18619e-007 3 3.75373e-007 ...
0 0 1 3.56133e-008 2 2.26246e-008 3 1.44814e-007 ...
The first row represents cycle 0 while the first and second column represents position 0 and its probability. The number of positions is large so doing it manually will take too much time.
I got this for now but i dont really know how to do the for loop with this kind of data.
set terminal gif
set output 'Probability.gif'
stats 'Probability.txt' nooutput
set xlabel 'Position'
set ylabel 'Probability'
set yrange [0:1]
set style fill solid border -1
unset key
Thanks beforehand any help is apreciated.
What you can do is to extract all x and y pairs from each row and plot it into a table. Actually, gnuplot prefers to have data in columns. Then plot this table to the gif terminal with the option animated. Check help gif. If you have a file remove the datablock $Data <<EOD ... EOD and in the code replace (2 times) $Data by "YourFilename".
Code:
### animated rows x0 y0 x1 y1 x2 y2
reset session
$Data <<EOD
0 0.00 1 0.10 2 0.20 3 0.40 4 0.20 5 0.10 6 0.00
0 0.00 1 0.08 2 0.18 3 0.48 4 0.18 5 0.08 6 0.00
0 0.00 1 0.05 2 0.16 3 0.58 4 0.16 5 0.05 6 0.00
0 0.00 1 0.08 2 0.18 3 0.48 4 0.18 5 0.08 6 0.00
0 0.00 1 0.10 2 0.20 3 0.40 4 0.20 5 0.10 6 0.00
EOD
stats $Data nooutput
ColCount = STATS_columns
RowCount = STATS_records
set xrange[0:6]
set yrange[0:1]
set terminal gif size 400,400 animate delay 50 optimize
set output "myAnimation.gif"
do for [j=0:RowCount-1] {
set table $Extract
plot for [i=1:ColCount/2] $Data u (column(2*i-1)):(column(2*i)) every ::j::j w table
unset table
plot $Extract u 1:2 w lp pt 7 lw 2 title sprintf("Row %d",j)
}
set output
### end of code
Result:
I want to import the data for a three-dimensional parameter p(i,j,k) that is stored in in k excel sheets but GAMS does not let me use dollar control statements in loops. Is there any way to do that using loops or other flow control statements like 'for' or 'while'?
I need to do something like this but it is seemingly impossible:
loop(k,
$call gdxxrw Data.xlsx par=temp rng=k!A1:Z20 rdim=1 cdim=1
$gdxin Data.gdx
$load temp
$gdxin
p(i,j,k)=temp(i,j);
);
Suppose each sheet looks like:
(only difference is I use 2's in sheet2 and 3's in sheet3).
To read this do:
$set xls d:\tmp\test2.xlsx
$set gdx s.gdx
set
i /i1*i3/
j /j1*j5/
k 'sheet names' /Sheet1*Sheet3/
;
parameter
s(i,j) 'single sheet'
a(i,j,k) 'all data'
;
file f /task.txt/;
loop(k,
putclose f,'par=s rng=',k.tl:0,'!a1 rdim=1 cdim=1'/
execute 'gdxxrw i=%xls% o=%gdx% #task.txt trace=2';
execute_loaddc '%gdx%',s;
a(i,j,k) = s(i,j);
);
display a;
My results are:
---- 23 PARAMETER a all data
sheet1 sheet2 sheet3
i1.j1 1.000 2.000 3.000
i1.j2 1.000 2.000 3.000
i1.j3 1.000 2.000 3.000
i1.j4 1.000 2.000 3.000
i1.j5 1.000 2.000 3.000
i2.j1 1.000 2.000 3.000
i2.j2 1.000 2.000 3.000
i2.j3 1.000 2.000 3.000
i2.j4 1.000 2.000 3.000
i2.j5 1.000 2.000 3.000
i3.j1 1.000 2.000 3.000
i3.j2 1.000 2.000 3.000
i3.j3 1.000 2.000 3.000
i3.j4 1.000 2.000 3.000
i3.j5 1.000 2.000 3.000
So I am using a pm3d map to plot a data file with 3 columns x, y, z. The final plot shows some region in 2d and I have another data file x, y which are discrete coordinates of some of the points on the boundary of the region. I want to plot these points on top of a plot generated by pm3d map. If I simply try replot after plotting pm3d map, it doesn't show those points in the plot. Can anybody kindly tell me how can I achieve this?
Thanks in advance.
Edit: Here is the minimal example. The data file is something like this:
0.00 -0.50 4
0.00 -0.25 4
0.00 0.00 4
0.00 0.25 4
0.00 0.50 4
0.25 -0.50 1
0.25 -0.25 1
0.25 0.00 1
0.25 0.25 1
0.25 0.50 1
0.50 -0.50 0
0.50 -0.25 0
0.50 0.00 0
0.50 0.25 0
0.50 0.50 0
0.75 -0.50 0
0.75 -0.25 0
0.75 0.00 0
0.75 0.25 0
0.75 0.50 0
1.00 -0.50 3
1.00 -0.25 4
1.00 0.00 4
1.00 0.25 5
1.00 0.50 5
I am plotting this by following commands:
set pm3d map
set pm3d corners2color c1
spl 'file.dat'
I also have another file border.dat which contains discrete points like this:
0.00 -0.25
0.25 0.25
1.00 0.00
Now I want to plot the points (x and y coordinates) of the points given in this file on top of the plot that pm3d map (I am not using with pm3d; it's pm3d map!) generates for file.dat.
How can I achieve this?
Thank you