Plotting data included in script fails in loop - loops

I would like to plot data points included inside a script file.
This should be done multiple times (plotting to different files).
Therefore, I am using a do-for-loop.
This loop let's Gnuplot freeze on excution.
Could you please hint me to the cause?
This is my MWE:
reset
set autoscale
do for [index=1:1] {
plot "-" with lines ls 2 notitle
0.500 5
1.000 6
1.500 7
e
}

Yes, seems like the combination of do for with inline data isn't supported. It also wouldn't be very convenient, since this would require a separate data block for every iteration like in
set style data linespoints
plot '-' using 1:2, '-' using 1:3
1 2 3
4 5 6
e
1 2 3
4 5 6
e
With version 5.0 inline data blocks were introduced which allow reusing inline data:
$data <<EOD
1 2 3
4 5 6
EOD
do for [i=2:3] {
plot $data using 1:i w l
pause -1
}

Related

Pandas How to Align Two Columns in a DataFrame and NaN empty cells

I'm using Python 3.8.8
I have a DataFrame structured like this:
A
B
0
1
1
2
2
1
3
7
4
7
5
8
and an array:
C = [3, 4, 7]
I would like to add an array "C" as a new column to the DataFrame. The problem is this array has a different length of index than the df. I would like to make up for the difference in length in C by filling the empty cells with NaNs. My desired result would look something like:
A
B
C
0
1
NaN
1
2
NaN
2
1
3
3
7
4
4
7
7
5
8
NaN
What I am looking for specifically is a way to add C starting at a specific index of the df, but I don't know how to work around the discrepancy between the length of the df and array.
Thank you for your time
To get around the problem of 'different length' when putting your list into the dataframe, you can convert it to a pandas series. Once you do that, you can easily add it to your dataframe with the rest of the values being filled with np.nan.
In your case, you can specifically also set the index when you convert your C list to a series, which you can then assign to your dataframe. Pandas nature to align data on indices will place the series on the right index
Consider using the code below:
c = pd.Series([3, 4, 7],index=[2,3,4])
df['C'] = c
prints:
A B 0
0 0 1 NaN
1 1 2 NaN
2 2 1 3.0
3 3 7 4.0
4 4 7 7.0
5 5 8 NaN
Renaming 0 should be trivial.

gnuplot from file with multiple metrics

I have a C program that writes three files with the first column being the X values (clock cycles). The other columns are a set of metrics like % of memory usage, memory "holes", etc. Like I said before, there are three files like this (one per algorithm: first fit, best fit and worst fit).
Example - Headers: Clock Cycle, % Memory Usage and Number of "holes":
File 1 (First Fit):
1 20% 5
2 30% 9
3 70% 12
4 90% 3
File 2 (Best Fit):
1 15% 3
2 20% 5
3 80% 7
4 40% 3
5 60% 9
File 3 (Worst Fit):
1 15% 3
2 20% 5
3 80% 7
I would like to know if there is a way with gnuplot to generate one graph per metric comparing the three algorithms in those metrics.
By the way, sorry about my english, hope you guys understand.
set term pngcairo size 600,400
set output 'memory.png'
plot 'file1' using 1:2 w lp, \
'file2' using 1:2 w lp, \
'file3' using 1:2 w lp
set output 'holes.png'
plot 'file1' using 1:3 w lp, \
'file2' using 1:3 w lp, \
'file3' using 1:3 w lp
.......
(it would be better if you get rid of '%' in second column)

Divide a data set into bins of size n matlab

I have a data set of size 11490x1. the data is recorded every 0.25 second(i.e. 4hz). So, 1 second accounts for 4 data points. The goal here is to further create sub sets every 3 seconds, meaning that I want to look at data every 3 seconds and analyze it. for example: if I had data such as [1 2 3 4 5 6 8 2 4 2 4 3 2 4 2 5 2 5 24 2 5 1 5 1], I want to have a sub set [1 2 3 4 5 6 8 2 4 2 4 3 ] and so on...
Any help would be appreciate.
It really depends on how you plan to "analyse" your data. The simplest way is to use a loop:
n = 4*3;
breaks = 0:n:numel(data)
for i = 1:numel(breaks)-1
sub = data(breaks(i)+1:breaks(i+1));
%// do analysis
%// OR sub{i} = data(breaks(i)+1:breaks(i+1));
end
A vectorized approach might use reshape(data,[],12) after padding data so that mod(numel(data),12)==0
A third way might be to break your matrix up into a cell array using mat2cell or in a for loop like above but instead of sub=... rather use sub{i}=...

Calculating mean over an array of lists in R

I have an array built to accept the outputs of a modelling package:
M <- array(list(NULL), c(trials,3))
Where trials is a number that will generate circa 50 sets of data.
From a sampling loop, I am inserting a specific aspect of the outputs. The output from the modelling package looks a little like this:
Mt$effects
c_name effect Other
1 DPC_I 0.0818277549 0
2 DPR_I 0.0150814475 0
3 DPA_I 0.0405341027 0
4 DR_I 0.1255416311 0
5 (etc.)
And I am inserting it into my array via a loop
For(x in 1:trials) {
Mt<-run_model(params)
M[[x,3]] <- Mt$effects
}
The object now looks as follows
M[,3]
[[1]]
c_name effect Other
1 DPC_I 0.0818277549 0
2 DPR_I 0.0150814475 0
3 DPA_I 0.0405341027 0
4 DR_I 0.1255416311 0
5 (etc.)
[[2]]
c_name effect Other
1 DPC_I 0.0717384637 0
2 DPR_I 0.0190812375 0
3 DPA_I 0.0856456427 0
4 DR_I 0.2330002551 0
5 (etc.)
[[3]]
And so on (up to 50 elements).
What I want to do is calculate an average (and sd) of effect, grouped by each c_name, across each of these 50 trial runs, but I’m unable to extract the data in to a single dataframe (for example) so that I can run a ddply summarise across them.
I have tried various combinations of rbind, cbind, unlist, but I just can’t understand how to correctly lift this data out of the sequential elements. I note also that any reference to .names results in NULL.
Any solution would be most appreciated!

What format does matlab need for n-dimensional data input?

I have a 4-dimensional dictionary I made with a Python script for a data mining project I'm working on, and I want to read the data into Matlab to do some statistical tests on the data.
To read a 2-dimensional matrix is trivial. I figured that since my first dimension is only 4-deep, I could just write each slice of it out to a separate file (4 files total) with each file having many 2-dimensional slices, looking something like this:
2 3 6
4 5 8
6 7 3
1 4 3
6 6 7
8 9 0
This however does not work, and matlab reads it as a single continuous 6 x 3 matrix. I even took a look a dlmread but could not figure out how to get it do what I wanted. How do I format this so I can put 3 (or preferably more) dimensions in a single file?
A simple solution is to create a file with two lines only: the first line contains the target array size, the second line contains all your data. Then, all you need to do is reshape the data.
Say your file is
3 2 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
You do the following to read the array into the variable data
fid = fopen('myFile'); %# open the file (don't forget the extension)
arraySize = str2num(fgetl(fid)); %# read the first line, convert to numbers
data = str2num(fgetl(fid)); %# read the second line
data = reshape(data,arraySize); %# reshape the data
fclose(fid); %# close the file
Have a look at data to see how Matlab orders elements in multidimensional arrays.
Matlab stores data column wise. So from your example (assuming its a 3x2x3 matrix), matlab will store it as first, second and third column from the first "slice", followed by the first, second third columns from the second slice and so on like this
2
4
3
5
6
8
6
1
7
4
3
3
6
8
6
9
7
0
So you can write the data out like this from python (I don't know how) and then read it into matlab. Then you can reshape it back into a 3x2x3 matrix and you'll retain your correct ordering.

Resources