I have 31 possible scenarios for the value of the average and I would like to generate for each scenario samples but I do not get as much as I would like.
This is the scenarios
> scenari<-s*shift;scenari
Var1 Var2 Var3 Var4 Var5
2 1.5 0.0 0.0 0.0 0.0
3 0.0 1.5 0.0 0.0 0.0
4 1.5 1.5 0.0 0.0 0.0
5 0.0 0.0 1.5 0.0 0.0
6 1.5 0.0 1.5 0.0 0.0
7 0.0 1.5 1.5 0.0 0.0
8 1.5 1.5 1.5 0.0 0.0
9 0.0 0.0 0.0 1.5 0.0
10 1.5 0.0 0.0 1.5 0.0
11 0.0 1.5 0.0 1.5 0.0
12 1.5 1.5 0.0 1.5 0.0
13 0.0 0.0 1.5 1.5 0.0
14 1.5 0.0 1.5 1.5 0.0
15 0.0 1.5 1.5 1.5 0.0
16 1.5 1.5 1.5 1.5 0.0
17 0.0 0.0 0.0 0.0 1.5
18 1.5 0.0 0.0 0.0 1.5
19 0.0 1.5 0.0 0.0 1.5
20 1.5 1.5 0.0 0.0 1.5
21 0.0 0.0 1.5 0.0 1.5
22 1.5 0.0 1.5 0.0 1.5
23 0.0 1.5 1.5 0.0 1.5
24 1.5 1.5 1.5 0.0 1.5
25 0.0 0.0 0.0 1.5 1.5
26 1.5 0.0 0.0 1.5 1.5
27 0.0 1.5 0.0 1.5 1.5
28 1.5 1.5 0.0 1.5 1.5
29 0.0 0.0 1.5 1.5 1.5
30 1.5 0.0 1.5 1.5 1.5
31 0.0 1.5 1.5 1.5 1.5
32 1.5 1.5 1.5 1.5 1.5
and this is the function
genereting_fuction<-function(n){
for (i in 1:length(scenari)){
X1=rnorm(n)+scenari[i,1]
X4=rnorm(n)+scenari[i,4]
X2=X1*p12+std_e2*rnorm(n)+scenari[i,2]
X3=X1*p13+X4*p43+std_e3*rnorm(n)+scenari[i,3]
X5=X2*p25+X3*p35+std_e5*rnorm(n)+scenari[i,5]
sample=cbind(X1,X2,X3,X4,X5)
return(sample)
}
}
genereting_fuction(10)
I should get 31 samples of size 10X5 but I get only one sample
You are applying the for loop over return as well and eventually returning the sample corresponding to the last scenario only.
Try this :
genereting_fuction<-function(n){
sample <- list()
for (i in 1:nrow(scenari)){
X1=rnorm(n)+scenari[i,1]
X4=rnorm(n)+scenari[i,4]
X2=X1*p12+std_e2*rnorm(n)+scenari[i,2]
X3=X1*p13+X4*p43+std_e3*rnorm(n)+scenari[i,3]
X5=X2*p25+X3*p35+std_e5*rnorm(n)+scenari[i,5]
sample[[i]]=cbind(X1,X2,X3,X4,X5)
}
sample
}
The output will be a list and its ith element will be a sample corresponding to the ith scenario.
Related
I have a big dataframe list[378x87](S3:dataframe), and I want to simplify it by subsetting the rows in a way that has experimental significance.
I can do that because the 387 rows are in fact subgroups of the same geographical region, (with around 14 groups recognizable by the initial letters), which are named as follows:
DAW2-11 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 0.0 0.0 0.1 0.0 0.1 0 0 0.0 0.0 0.0 0.0 10.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 5.0
DAW2-12 0.1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 0.0 0.1 0 0 0.0 0.0 0.0 0.0 5.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 3.0
DAW2-13 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 3.0 0.1 0.0 0.0 0.0 0.1 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 0.0 0.1 0.0 0.0 0.5 0 0 0.0 0.0 0.0 0.0 15.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.1 0.0 10.0
DAW2-21 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 0.0 0.0 0.0 0.0 0.0 0 0 0.0 0.0 0.0 0.5 7.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.1 0.0 1.0
DAW2-22 0.0 0.1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 0.0 0.1 0.5 0.0 0.5 0 0 0.0 0.0 0.0 0.0 3.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 10.0
DAW2-23 0.0 0.0 0.0 5.0 0.5 0.0 0.0 0.0 0.0 0.0 0.1 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 0.0 0 0 0.0 0.0 0.0 0.0 10.0 0.0 0.0 0.1 0.0 0.0 0.0 4.0 0.0 0.0 0.0 0.5 0.0 2.0
.
.
.
The question is ¿How can I create a new dataframe subsetting by the first letters, DAW2 in this case, and for the rest of the 14 groups?
Replicating data from the OP to create two split groups, a solution in Base R is as follows:
textFile <- "DAW2-11 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 0.0 0.0 0.1 0.0 0.1 0 0 0.0 0.0 0.0 0.0 10.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 5.0
DAW2-12 0.1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.5 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.0 0.0 0.1 0 0 0.0 0.0 0.0 0.0 5.0 0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 0.0 4.0 0.0 0.0 0.0 0.5 0.0 2.0
"
data <- read.table(text = textFile,header = FALSE,stringsAsFactors = FALSE)
data$splitVar <- as.factor(substr(data$V1,1,4))
splitData <- split(data,data$splitVar)
splitData$DAW2[1:5]
At this point the object splitData contains two data frames, one for the rows from the original data frame where splitVar == 'DAW2' and another where it is equal to DAW3.
The split() function uses the value of the split variable to name each data frame in the resulting list, so subsequent R code can use the $ form of the extract operator to access a data frame by its region code.
We'll print the first 5 columns of the first data frame in the list to illustrate that the first data frame only contains data for DAW2.
> splitData$DAW2[1:5]
V1 V2 V3 V4 V5
1 DAW2-11 0.0 0.0 0 0
2 DAW2-12 0.1 0.0 0 0
3 DAW2-13 0.0 0.0 0 0
4 DAW2-21 0.0 0.0 0 0
5 DAW2-22 0.0 0.1 0 0
6 DAW2-23 0.0 0.0 0 5
7 DAW2-11 0.0 0.0 0 0
>
Note: Given the sample data, it appears that the region data is represented by the first three characters of the first column in the data frame. If the region information has a variable number of characters ending before the -, we can create the splitVar as follows.
data$splitVar <- as.factor(sapply(strsplit(data$V1,"-"),function(x) x[1]))
Now that we have a version of the code that produces correct output, we can simplify the solution as noted in Daniel's comment, which uses a regular expression with sub() to delete all characters in V1 starting with -.
splitData <- split(data,sub("-.*","",data$V1))
...and the output from the DAW3 data frame:
> splitData <- split(data,sub("-.*","",data$V1))
> splitData$DAW3[1:5]
V1 V2 V3 V4 V5
8 DAW3-12 0.1 0.0 0 0
9 DAW3-13 0.0 0.0 0 0
10 DAW3-21 0.0 0.0 0 0
11 DAW3-22 0.0 0.1 0 0
12 DAW3-23 0.0 0.0 0 5
>
I have a problem with solving equation of retention with method Runge-Kutta (2nd order) in Scilab.
I must to solve equation:
dh/dt=(InF(t)-OutF(t))/F(h), or this equation in picture (link)
where:
h - height [m];
t - time [sec];
InF- inflow to reservoir [m^3/sec];
OutF - outflow from reservoir [m^3/sec];
F - area [m^2];
but i don't have OutF(t), I have OutF(h) and i need to get chart transformation wave inflow-outflow.
I don't know how can I put to scilab code without errors.
Example code (met. Runge-Kutta 2nd order) for function (x,y):
function [y,x]=ExplicitRungeKutta(y0,x0,f,dt,N)
y=zeros(N,1)*%nan;
x=zeros(N,1)*%nan;
y(1)=y0;
x(1)=x0;
for i=2:1:N do
k1=f(x(i-1),y(i-1));
k2=f(x(i-1)+3*dt./4,y(i-1)+3*k1*dt./4);
y(i)=y(i-1)+(k1./3+2*k2./3)*dt;
x(i)=x(i-1)+dt;
end
endfunction
function dy=F(x,y)
dy=-x./(6*y-2*(x.^2)./y);
endfunction
y0=1;
x0=1;
xN=1.34;
N=25;
dt=(xN-x0)/N;
[y,x]=ExplicitRungeKutta(y0,x0,F,dt,N);
plot2d(x,y)
and this is good program, but my case is harder to implicate to scilab.
This is code to errors program:
clear
clc
WYS=[0 0.25 0.5 0.75 1 1.25 1.5 1.75 2 2.25 2.5 2.75 3 3.25 3.5 3.75 4 4.25 4.5 4.75 5 5.25 5.5 5.75 6];
FZ=[7 12.02 22.00 56.03 170.07 346.36 492.35 655.67 820.95 969.25 1164.35 1461.50 1885.19 2429.21 3040.32 3617.32 4155.82 4605.32 5018.49 5448.25 5801.89 6140.23 6414.84 6710.24 7013.57];
//wys - height in metres, Fz - area in m^2
TIME=[0 900 1800 2700 3600 4500 5400 6300 7200 8100 9000 9900 10800 11700 12600 13500 14400 15300 16200 17100 18000 18900 19800 20700 21600 22500 23400 24300 25200 26100 27000 27900 28800 29700 30600 31500 32400 33300 34200 35100 36000 36900 37800 38700 39600 40500 41400 42300 43200 44100 45000 45900 46800 47700 48600 49500 50400 51300 52200 53100 54000 54900 55800 56700 57600 58500 59400 60300 61200 62100 63000 63900 64800 65700 66600 67500 68400 69300 70200 71100 72000 72900 73800 74700 75600 76500 77400 78300 79200 80100 81000 81900 82800 83700 84600 85500 86400 87300 88200 89100 90000 90900 91800 92700 93600 94500 95400 96300 97200 98100 99000 99900 100800 101700 102600 103500 104400 105300 106200 107100 108000 108900 109800 110700 111600 112500 113400 114300 115200 116100 117000 117900 118800 119700 120600 121500 122400 123300 124200 125100 126000 126900 127800 128700 129600 130500 131400 132300 133200 134100 135000 135900 136800 137700 138600 139500 140400 141300 142200 143100 144000 144900 145800 146700 147600 148500 149400 150300 151200 152100 153000 153900 154800 155700 156600 157500 158400 159300 160200 161100 162000 162900 163800 164700 165600 166500 167400 168300 169200 170100 171000 171900 172800]
R10=[0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.2 0.3 0.3 0.5 0.8 1.3 1.9 2.6 3.3 4.1 4.9 5.7 6.4 7.1 7.7 8.4 9.0 9.5 10.1 10.6 11.0 11.5 11.9 11.7 11.1 10.2 9.0 7.8 6.7 5.8 5.2 4.7 4.4 4.2 4.0 3.9 3.9 3.8 3.8 3.8 3.8 3.8 3.8 3.8 3.8 3.8 3.8 3.8 3.8 3.8 3.9 3.9 3.9 3.9 3.9 3.9 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.1 4.1 4.1 4.1 4.1 4.1 4.1 4.2 3.9 3.6 3.1 2.6 2.0 1.5 1.1 0.8 0.6 0.4 0.3 0.2 0.1 0.1 0.1 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0]
// n=193 //intlow to reservoir, time in seconds, R10- rain 10years, chart inflow to reservoir
WYS1=[0 0.35 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3 3.1 3.2 3.3 3.4 3.5 4.3 4.5 4.6 4.7 4.8 4.9 5 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6]
oF1=[0 0 0.05 0.15 0.4 0.66 1.1 1.5 2 2.2 2.42 3 3.4 3.8 4.2 4.52 4.84 5.12 5.42 5.65 5.9 6.14 6.37 6.62 6.84 7.04 7.24 7.45 7.65 7.84 8 9.4 9.75 11.11 13.452 16.435 19.93 23.875 28.2 32.915 37.96 41.955 45.95 49.945 53.94 57.935 61.93 65.925]
//outlow from reservoir, of1 - outflow in m3/sec
//y=h,x=t, inf(t=x)=u outf(h=y)=w, Fz(h=y)=z
function [fz,w,u,h,t]=ExplicitRungeKutta(fz0,w0,u0,h0,t0,f,dt,N)
h=zeros(N,1)*%nan;
t=zeros(N,1)*%nan;
h(1)=h0;
t(1)=t0;
u(1)=u0;
w(1)=w0;
fz(1)=fz0;
for i=2:1:N do
k1=f(t(i-1),h(i-1));
k2=f(t(i-1)+3*dt./4,h(i-1)+3*k1*dt./4);
h(i)=h(i-1)+(k1./3+2*k2./3)*dt;
t(i)=t(i-1)+dt;
u(i)=interp1(TIME,R10,t(i),'linear');
w(i)=interp1(WYS,OF1,h(i),'linear');
fz(i)=interp1(WYS,FZ,h(i),'linear');
end
endfunction
function dh=F(u,w,fz)
dh=(u-w)./fz;
endfunction
h0=0;
t0=0;
u0=0;
w0=0;
fz0=7;
tN=172800;
N=17280;
dt=(tN-t0)/N;
[u,w,fz,h,t]=ExplicitRungeKutta(u0,w0,fz0,h0,t0,F,dt,N);
Maybe anybody have any idea how to try solve it?
I am trying to get all points on a 2d plane in the range (0..10,0..10) with a step of 0.5. I would like two store these values in a dataframe like this:
x y
1 1 1.5
2 0 0.5
3 4 2.0
I am considering using a loop to start from 0.0 for the x column and fill the y column such that I get something like this:
x y
1 0 0
2 0 0.5
3 0 1
and so on upto 10. And increment it by 0.5 and do for 1 and so on. I would like to know a more efficient way of doing this in R?.
Is this what you want?
expand.grid(x=seq(0,10,by=0.5),y=seq(0,10,by=0.5))
x y
1 0.0 0.0
2 0.5 0.0
3 1.0 0.0
4 1.5 0.0
5 2.0 0.0
6 2.5 0.0
7 3.0 0.0
8 3.5 0.0
9 4.0 0.0
10 4.5 0.0
11 5.0 0.0
12 5.5 0.0
13 6.0 0.0
14 6.5 0.0
15 7.0 0.0
16 7.5 0.0
17 8.0 0.0
18 8.5 0.0
19 9.0 0.0
20 9.5 0.0
21 10.0 0.0
22 0.0 0.5
23 0.5 0.5
24 1.0 0.5
25 1.5 0.5
26 2.0 0.5
27 2.5 0.5
28 3.0 0.5
29 3.5 0.5
30 4.0 0.5
...
I want to plot a scheduling diagram, in other words, Gantt Chart, in R.
What I have is a two dimensional array. A sample array is attache below.
: 1 2 3 4 5 6 7 8 9 10 11 12 :=
1 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2 0.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3 0.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
4 0.0 1.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
5 1.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
6 1.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
7 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
8 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
9 0.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
10 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
11 1.0 1.0 0.0 0.0 0.0 0.0 0.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0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 1.0
33 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0
34 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0
35 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0
36 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0
Each row denotes a time period, and each column denotes a project. If the number in the cell is 1, it means the current project is scheduled to run during this time period. There are 12 projects and 36 months. Is there any way to draw the gantt chart efficiently?
Well as a starter, here's a quick & dirty approach using ggplot:
gantt_ <- as.data.frame(t(gantt))
df <- do.call(rbind.data.frame, lapply(seq_along(gantt_), function(x) {
r <- rle(gantt_[[x]])
start <- cumsum(r$lengths) - r$lengths + 1
end <- start + r$lengths - 1
cbind(project = x, xmin = start[r$values == 1], xmax = end[r$values == 1])
}))
library(ggplot2)
ggplot(transform(df,
project = factor(project, levels = 36:1),
xmin = xmin - .1,
xmax = xmax + .1),
aes(x = xmin, xend = xmax, y = project, yend = project)) +
geom_segment(size = 4) +
scale_x_continuous(breaks = 1:12) + labs(x = "time")
Data used:
gantt <- read.table(header=T, text="
1 2 3 4 5 6 7 8 9 10 11 12
1 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
2 0.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
3 0.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
4 0.0 1.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
5 1.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
6 1.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0
7 1.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.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0.0 0.0 0.0 1.0 1.0 0.0 1.0
29 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 1.0
30 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0
31 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0
32 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 1.0
33 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0
34 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0
35 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0
36 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 1.0")
For more options, have a look at
library(sos)
findFn("gantt")
You could take a look at the following post. This uses ggplot as well.
https://dwh-businessintelligence.blogspot.nl/2016/05/what-if-for-project-management.html
What it basically does is draw the rectangles:
ggplot(dsg, aes(xmin = MinWeek, xmax = MaxWeek, ymin = ymin, ymax = ymin + 80 * (Hours/28), fill = factor(Person))) +
geom_rect() +
facet_grid(Grouping~., scales = "free_y") +
xlab("Week") +
theme(plot.title = element_text(lineheight=.8)) +
geom_text(aes(label = paste(Project, " (",Person, ", hrs:",Hours, ") WK:", MaxWeek , sep=""), y = ymin + 24, x = MaxWeek - 2), size = 5) +
geom_rect(data=dsg2, alpha=0.2) +
kobe_theme() +
xlim(0,max(dsg[["MaxWeek"]]) + 2) +
geom_text(aes(label = Grouping, y = 80, x = mw, size=20));
All the code is on github: https://github.com/stevedep/WhatIf
I am trying to draw all contours lines in a same color following the example from here: http://gnuplot.sourceforge.net/demo/contours.25.gnu
However, the example works, but my own code does not work:
set xyplane 0;
set pm3d
set contour
set cntrparam levels 6
unset surface;
unset key;
set pm3d map
set title "t";
splot for [i=1:1] "-" using 1:2:3 notitle with lines lc rgb "dark-blue";
....data....
Can you help me find the problem?
Here to download the code file:
https://dl.dropboxusercontent.com/u/45318932/contourpm3d.plt
I am using gnuplot4.6.5
The relevant line is
unset clabel
I know, that is very unintuitive. Don't know the reason behind it.
Here is the complete script with the respective changes, for reference:
set xyplane 0;
set pm3d
set contour
unset clabel
set cntrparam levels 6
unset surface;
unset key;
set pm3d map
splot for [i=1:1] "-" using 1:2:3 notitle with lines lw 2 lc rgb "dark-blue";
#a1 a2 t
0.0 0.0 25.0
0.0 0.1 28.0
0.0 0.2 37.0
0.0 0.3 23.0
0.0 0.4 23.0
0.0 0.5 15.0
0.0 0.6 16.0
0.0 0.7 33.0
0.0 0.8 16.0
0.0 0.9 20.0
0.0 1.0 14.0
0.1 0.0 25.0
0.1 0.1 47.0
0.1 0.2 26.0
0.1 0.3 14.0
0.1 0.4 16.0
0.1 0.5 15.0
0.1 0.6 27.0
0.1 0.7 13.0
0.1 0.8 14.0
0.1 0.9 20.0
0.1 1.0 0.0
0.2 0.0 25.0
0.2 0.1 28.0
0.2 0.2 26.0
0.2 0.3 14.0
0.2 0.4 16.0
0.2 0.5 16.0
0.2 0.6 32.0
0.2 0.7 14.0
0.2 0.8 19.0
0.2 0.9 0.0
0.2 1.0 0.0
0.3 0.0 57.0
0.3 0.1 36.0
0.3 0.2 26.0
0.3 0.3 14.0
0.3 0.4 15.0
0.3 0.5 16.0
0.3 0.6 31.0
0.3 0.7 18.0
0.3 0.8 0.0
0.3 0.9 0.0
0.3 1.0 0.0
0.4 0.0 42.0
0.4 0.1 23.0
0.4 0.2 26.0
0.4 0.3 19.0
0.4 0.4 15.0
0.4 0.5 16.0
0.4 0.6 34.0
0.4 0.7 0.0
0.4 0.8 0.0
0.4 0.9 0.0
0.4 1.0 0.0
0.5 0.0 54.0
0.5 0.1 23.0
0.5 0.2 26.0
0.5 0.3 17.0
0.5 0.4 15.0
0.5 0.5 16.0
0.5 0.6 0.0
0.5 0.7 0.0
0.5 0.8 0.0
0.5 0.9 0.0
0.5 1.0 0.0
0.6 0.0 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