I am trying to add a legend to my R plot that has five lines which I built with the following code:
predictors=c(1,3,5,7,10,13)
meanvec25=c(23.642465, 14.590552, 11.480690, 10.919103, 9.365745, 10.265116)
meanvec100=c(22.668256, 12.220893, 10.598733, 9.768388, 9.710492, 10.139874)
meanvec500=c(21.864226, 12.290780, 9.879924, 9.598233, 9.856007, 9.629011)
meanvec2000=c(22.072814, 11.658274, 10.208657, 9.789607, 9.720389, 9.781965)
meanvec5000=c(22.006622, 11.535754, 10.172841, 9.664394, 9.722084, 9.814400)
plot(predictors,meanvec25,col="red",type="l")
lines(predictors,meanvec100,col="blue",type="l")
lines(predictors,meanvec500,col="green",type="l")
lines(predictors,meanvec2000,col="purple",type="l")
lines(predictors,meanvec5000,col="orange",type="l")
Then I typed this line to add a legend:
legend(1,95,legend=c("25 Trees","100 Trees","500 Trees","2000 Trees","5000 Trees"),col=c("red", "blue","green","purple","orange"), lty=c(1,1,1,1,1), cex=0.8)
Nothing is added to my graph, but no error messages appear either. I tried fiddling with the location numbers at the beginning of the function, but it doesn't seem to help. Why won't the legend appear?
Thanks!
The scaling for axes X,Y and Z that I specify in the procedure scale3 is overridden. I don't understand why? Could someone point out what is wrong with my code?
The width of a box is n1/2-1(~128 points) and it is more than the height(98 points).
n1=257
n2=257
n3=98
file='test.ps'
set_plot,'Z'
erase,255
scale3,XRange=[0,n1-1],YRange=[0,n2-1],ZRange=[0,n3-1],az=-30,ax=30
PLOTS,[0,n1-1],[0,0],[0,0],/T3D,color=0,thick=2
PLOTS,[0,0],[0,n2-1],[0,0],/T3D,color=0,thick=2
PLOTS,[0,n1-1],[n2-1,n2-1],[0,0],/T3D,color=0,thick=2
PLOTS,[n1-1,n1-1],[0,n2-1],[0,0],/T3D,color=0,thick=2
PLOTS,[0,n1-1],[n2-1,n2-1],[n3-1,n3-1],/T3D,color=0,thick=2
PLOTS,[n1-1,n1-1],[0,n2-1],[n3-1,n3-1],/T3D,color=0,thick=2
PLOTS,[0,n1-1],[0,0],[n3-1,n3-1],/T3D,color=0,thick=2
PLOTS,[0,0],[0,n2-1],[n3-1,n3-1],/T3D,color=0,thick=2
PLOTS,[0,0],[0,0],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[n1/2-1,n1/2-1],[0,0],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[n1-1,n1-1],[0,0],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[n1-1,n1-1],[n2-1,n2-1],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[0,0],[n2-1,n2-1],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[n1/2-1,n1/2-1],[0,n2-1],[n3-1,n3-1],/T3D,color=0,thick=2
PLOTS,[0,n1-1],[n2/2-1,n2/2-1],[n3-1,n3-1],/T3D,color=0,thick=2
PLOTS,[n1-1,n1-1],[n2/2-1,n2/2-1],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[n1/2-1,n1/2-1],[0,n2-1],[0,0],/T3D,color=0,thick=2
PLOTS,[0,0],[n2/2-1,n2/2-1],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[n1/2-1,n1/2-1],[n2-1,n2-1],[0,n3-1],/T3D,color=0,thick=2
PLOTS,[0,n1-1],[n2/2-1,n2/2-1],[0,0],/T3D,color=0,thick=2
g=TVRD()
set_plot,'PS'
device,filename=file,xsize=8,ysize=6,/encapsulated
tv,g
device,/close
end
Do you mean mean the display is anisotropic? Just add:
device, set_resolution=[500, 500]
after you change to the Z-buffer device. This is what I get:
I found this answer
[Octave : How to change line color and width in drawRect
how to change the color in drawRect.
When I try the same for drawArrow, I get:
h=drawArrow(0,0,3,1,1,1,1,1);
h =
scalar structure containing the fields:
body = -12.469
head = -11.925
set(h,'color','r');
error: octave_base_value::array_value():
wrong type argument 'scalar struct'
error: set: H must be a graphics handle
What I am doing wrong here? For me it looks the same.
Thanks
Karl
You really should mention that you are using the octave-forge geometry package. drawArrow returns a struct with handles for the arrow body and head (as shown by your code) so you can and have to set them separately:
pkg load geometry
h = drawArrow (0,0,3,1,1,1,1,1);
set (h.body, "color", "r")
set (h.head, "facecolor", get(h.body, "color"))
You can also set a border around the head with "edgecolor"
I would like to add a legend to a surface plot.
I have tried, doing like this :
X=[0:0.3:2*%pi]; //example data
[x,y]=ndgrid(X,X);
z1=sin(X')*cos(X);
z2=z1/2;
z3=z1/3;
figure=scf();
surf(x,y,z1);
surf(x,y,z2);
surf(x,y,z3);
axes=figure.children(1);
axes.children(1).foreground=color(0,0,0);
axes.children(2).foreground=color(0,0,0);
axes.children(3).foreground=color(255,0,0);
axes.children(1).color_flag=0;
axes.children(2).color_flag=0;
axes.children(3).color_flag=0;
axes.children(1).color_mode=color(0,255,0);
axes.children(2).color_mode=color(0,0,255);
axes.children(3).color_mode=0;
legend(['z1','z1 divided by 2','z1 divided by 3'],opt=2,font_size=2);
I get the following error message:
!--error 10000
legend : Neither handle of type 'Polyline' can be found.
If it's not possible to do this with the basic version of Scilab, could you please advise to me some libraries Scilab permitting to do this.
Thanks for your help.
Legend not possible for surface plots
Legends is only for plot2dx graphs as stated in the documentation of legend properties:
This entity defines the parameters for legends drawn below plot2dx
graphs or created by the captions function.
Alternatives
You could simply add a title using xtitle
xtitle('z1,z1 div by2, z2 div by 3');
You could draw a box using uicontrol and style it using uicontrol_properties:
fig = gcf();
uicontrol(fig, "style", "text", ...
"string", "<html>z1<br>z1/2<br>z1/3</html>", ...
"position",[100 100 100 100], ...
"fontsize",15);