Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Mata & Graphs


From   "Ben Jann" <ben.jann@soz.gess.ethz.ch>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: Mata & Graphs
Date   Tue, 25 Jul 2006 23:44:04 +0200

Raphael wrote:
> I have the following matrix X, in Mata and would like to 
> draw a twoway line graph using each column as a variable. 
> How is this done?
>
>              1             2
>   +-----------------------------+
> 1 |  .8913041862             1  |
> 2 |  .8708690808             2  |
> 3 |  .8413558121             3  |
>   +-----------------------------+

Here is my solution:

mata:
void plot(
 real colvector y,
 real colvector x,
 | string scalar opts)
{
	real scalar n, N, Y, X

	n = rows(y)
	if (rows(x)!=n) _error(3200)
	N = st_nobs()
	if (N<n) st_addobs(n-N)
	st_store((1,n), Y=st_addvar("double", st_tempname()), y)
	st_store((1,n), X=st_addvar("double", st_tempname()), x)
	stata("twoway scatter " + st_varname(Y) + " " +
	 st_varname(X) + ", " + opts)
	if (N<n) st_dropobsin((N+1,n))
	st_dropvar((Y,X))
}
end

mata: y = .8913041862 \ .8708690808 \ .8413558121
mata: x = 1 \ 2 \ 3
mata: plot(y, x)
mata: plot(y, x, "c(l)")

ben


*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index