how to make single legend for the overlapping graphs

User Forumhow to make single legend for the overlapping graphs
Seunghwa asked 3 years ago



I am trying to combine two graphs into one (transparently) because the x-axis of the two graphs do not exactly match. If I draw a graph using below command I can have the graph that I want but the legend boxes are separate. I was wondering if it is possible to make the two legend boxes into one (or at least if I can get rid of the box frame around the legend). Thank you so much!



ylabel("Empirical Rejection Rate");
_plctrl = { 4,4,0 };
_pltype = { 6, 3,  4 };
_pstype = { 10, 8,  0 };
_plwidth=  {11,11, 10};
_pcolor=  {0,0,0};

_plegctl= { 2, 5, 1.5, 4 };

fonts("simplex simgrma");
string_title=" Non-random Bernoulli, Bartlett, p="$+strl_1 $+ " \202r\201=" $+ str_r $+  " T=" $+ strl_2;
prt_title="-cf=" $+ "fixed_p_"$+strl_1 $+ "_r_" $+  str_r $+ "_T_" $+ strl_2 $+ ".eps" $+" -c=1 -q";


_pdate = ""; //not print date
_plctrl = { 1};
_pltype = { 6};
_pstype = { 5 };
_plwidth=  {11};
_pcolor=  {0};
_plegctl= { 2, 4, 1.7, 3};

3 Answers
aptech Staff answered 3 years ago

I think it will be simpler in many ways to draw just 1 graph. If the x's for the different y's have the same number of elements, you can just horizontally concatenate them and draw the overlapping graph, for example:

x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
y1 = cos(x);
y2 = sin(x);

//horizontally concatenate y's
y_final = y1~y2;

xy(x, y_final);

Since you cannot concatenate matrices or vectors with incompatible dimensions, if the y's have a different number of elements you will have to pad the shorter one with missing values. For example:

x1 = seqa(1,1,100);
x2 = seqa(1,1,10);

y1 = cos(x1);
y2 = sin(x2);

//create a 90x1 vector of missing values
pad = miss(zeros(90,1), 0);

//vertically concatenate the missing values on top of 'y2'
y2 = pad|y2;

//horizontally concatenate 'y1' and 'y2'
y_final = y1~y2;

xy(x1, y_final);
Seunghwa answered 3 years ago

Thank you so much for the quick answer. I followed the way you suggested. My x1 and x2 do not have much in common other than that all the numbers for both  x1 and x2 lie between 0 and 1. When I draw the graph by padding with the missing values, the xy() gave me discontinuous lines(curves). It seems that  the curve ends when a missing value is encountered and a new curve begins plotting at the next non-missing value. I was wondering if there is anyway to connect these discontinuous curves smoothly. Thank you.


aptech Staff answered 3 years ago

Yes, the line will be broken up by missing values. For a continuous curve, keep your data points contiguous. You can ensure this by vertically concatenating the vector of missing values that you are using for padding on top (or on bottom, either will work) of your data. Take a look at the second code snippet in the first response to see an example of this.