楼主: 匿名
20358 212

[Stata] [Stata] 如何添加直角坐标系及极坐标系及SDAS_绘图实例 [推广有奖]

31
niuniuyiwan 在职认证  发表于 2015-8-27 07:53:48
  1. //make up some data
  2. set seed 1
  3. clear
  4. set obs 50
  5. generate a=runiform()*1000
  6. generate b=runiform()*10
  7. generate s=runiform()<.5

  8. label define sexlab 1 `"{fontface "Female and male symbols":M }"' 0 ///
  9.       `"{fontface "Female and male symbols":F }"'
  10. label values s sexlab

  11. scatter a b , ms(i)  mlab(s) mlabpos(c) ///
  12. mlabsize(*2) title( "Example" `"{fontface "Female and male symbols":M F}"')
复制代码

Graph.png


以上为未加装字体时的图像 FEMALE AND MALE SYMBOLS FONT.zip (4.56 KB) 本附件包括:

  • FMSYMB.TTF
  • FMSYMB.WRI
  • LICENSE.TXT

解压后双击>-----安装-----即可

字体下载地址:http://www.fonts4free.net/female-and-male-symbols-font.html#

代码同上


Graph.png



32
niuniuyiwan 在职认证  发表于 2015-8-27 07:55:09
  1. // Fourier plot

  2. clear
  3. set obs 8
  4. gen x=(_n-1)*(2*_pi)/8
  5. gen y=sin(x)
  6. mata: h=st_data(.,"y")
  7. mata: fft(h)

  8. # delimit ;
  9. twoway scatter y x ||       
  10. function y=1/8*(4*sin(1*x)-4*sin( 7*x)), range(x) ||       
  11. function y=1/8*(4*sin(1*x)-4*sin(-1*x)), range(x) ||,
  12. legend(cols(1) label(1 "sampled values")       
  13.   label(2 "sum of high frequency components")       
  14.   label(3 "sum of low frequency components"))       
  15. title("Reconstructed signal for y=sin(x) sampled at 8 points");       
  16. # delimit cr       
  17.                        
复制代码

Graph.png



33
niuniuyiwan 在职认证  发表于 2015-8-27 07:58:00
  1. *修改后,添加#delimit执行无误
  2. // Circle using function
  3. #delimit ;
  4. twoway (function y = sqrt(1 -(x)^2),
  5. range(-1 1) lwidth(thick) lcolor(red))
  6. (function y = -sqrt(1 - (x)^2),
  7. range(-1 1) lwidth(thick) lcolor(red)), aspect(1);
复制代码

Graph.png


34
niuniuyiwan 在职认证  发表于 2015-8-27 08:39:36
  1. // normalden using function
  2. clear
  3. set obs 100

  4. generate x1 = rnormal(0,1)
  5. generate x2 = rnormal(0.5,1.3)

  6. summarize x1
  7. local M1 = r(mean)
  8. local SD1 = r(sd)

  9. summarize x2
  10. local M2 = r(mean)
  11. local SD2 = r(sd)

  12. twoway function x1 = normalden(x, `M1', `SD1') , range(-4 4) || ///
  13. function x2 = normalden(x, `M2', `SD2'), range(-4 5)
复制代码

Graph.png



35
niuniuyiwan 在职认证  发表于 2015-8-27 12:18:46
  1. //Mandelbrot Set
  2. *Example taken from Stata News
  3. *http://www.stata.com/stata-news/statanews.27.4.pdf
  4. *Please note that on my computer this took 3 hours to run.
  5. //Stata——14 MP,大约半小时
  6. clear all

  7. mata:
  8. real scalar escape(complex scalar Z, C, real scalar R, maxiter)
  9. {
  10. real scalar i
  11. for (i=0; i < maxiter; i++) {
  12.   if (norm(Z) <= R) {
  13.    Z = Z*Z+C
  14. }
  15.   else return(i)
  16.   }
  17.   return(i)
  18. }
  19. end

  20. mata:
  21. mandelbrot_set = J(201*201, 3, .)
  22. cnt = 1
  23. C = -0.8+0.156i
  24. for(i=0; i<=200; i++) {
  25. for(j=0; j<=200; j++) {
  26.   Z = C(-2+i*4/200, -2+j*4/200)
  27.   n = escape(Z, C, 100, 400)
  28.   mandelbrot_set[cnt,.]= (i, j, n)
  29.   cnt++
  30. }
  31. }
  32. end

  33. getmata (x y escape)= mandelbrot_set
  34. quiet summarize escape
  35. local min = r(min)-1
  36. local max = r(max)+1
  37. twoway contour escape y x, ccuts(`min'(1)`max')  ///
  38.   clegend(off) graphr(m(zero)) xscale(off)        ///
  39.   xlab(,nogrid)  yscale(off) ylab(,nogrid) scheme(s2color)
复制代码

Graph.png



36
niuniuyiwan 在职认证  发表于 2015-8-27 13:56:20
  1. //2nd Row of axis labels

  2. webuse turksales, clear
  3. line sales t,                              ///
  4.     xtick(119.5(4)159.5, tlength(*1.5))    ///
  5.     xmlabel(120/159,                       ///
  6.         noticks format(%tqq))              ///
  7.     xlabel(121.5(4)157.5,                  ///
  8.         noticks format(%tqCY) labgap(2.5)) ///
  9.     xtitle("")

  10. *(Maarten Buis - Stata list,  Thu, 7 Nov 2013)
复制代码


Graph.png



37
niuniuyiwan 在职认证  发表于 2015-8-27 14:14:28
  1. // Gap between bars in bar graph

  2. sysuse auto, clear

  3. graph bar (mean) price turn mpg weight, over(rep78)

  4. set obs 75
  5. replace rep78=2.5 in 75

  6. graph bar (mean) price turn mpg weight, over(rep78,  relabel( 3 " "  ))

  7. *(Nick Cox - Stata list,  Wed, 26 May 2010)
复制代码

Graph.png


38
niuniuyiwan 在职认证  发表于 2015-8-27 16:50:58
  1. // Candle Stick plot

  2. clear
  3. input ///
  4. str3 share         str20   time     price
  5.       abc          "1/5/2011:10:34" 10.34
  6.       abc          "1/5/2011:10:35" 10.25
  7.       abc          "1/5/2011:10:36" 10.15
  8.       abc          "1/5/2011:10:37" 10.73
  9.       abc          "1/5/2011:10:39" 10.95
  10.       abc          "1/5/2011:10:41" 10.53
  11.       abc          "1/5/2011:10:45" 10.99
  12.       abc          "1/5/2011:10:49" 11.05
  13.       abc          "2/5/2011:10:34" 9.34
  14.       abc          "2/5/2011:10:35" 9.25
  15.       abc          "2/5/2011:10:36" 9.15
  16.       abc          "2/5/2011:10:37" 9.73
  17.       abc          "2/5/2011:10:39" 9.95
  18.       abc          "2/5/2011:10:41" 9.53
  19.       abc          "2/5/2011:10:45" 9.99
  20.       abc          "2/5/2011:10:49" 9.05
  21.       abc          "3/5/2011:10:34" 8.34
  22.       abc          "3/5/2011:10:35" 8.25
  23.       abc          "3/5/2011:10:36" 8.15
  24.       abc          "3/5/2011:10:37" 8.73
  25.       abc          "3/5/2011:10:39" 8.95
  26.       abc          "3/5/2011:10:41" 8.53
  27.       abc          "3/5/2011:10:45" 8.99
  28.       abc          "3/5/2011:10:49" 12.05
  29.       abc          "4/5/2011:10:34" 12.34
  30.       abc          "4/5/2011:10:35" 12.25
  31.       abc          "4/5/2011:10:36" 12.15
  32.       abc          "4/5/2011:10:37" 12.73
  33.       abc          "4/5/2011:10:39" 12.95
  34.       abc          "4/5/2011:10:41" 12.53
  35.       abc          "4/5/2011:10:45" 12.99
  36.       abc          "4/5/2011:10:49" 17.05
  37. end
  38. l

  39. gen double date=dofc(clock(time,"DMY hm"))
  40. format date %td

  41. collapse  (p1) p1=price (p25) p25=price ///
  42. (p50) p50=price (p75) p75=price         ///
  43. (p99) p99=price, by(date)

  44. gen uav=p75- 1.5*(p25-p75)
  45. gen lav=p75+ 1.5*(p25-p75)
  46.                
  47. gen col=2               
  48. replace col=(p50-p50[_n-1])>0 if _n!=1       
  49.                
  50. twoway (rbar p75 p25 date if col==0, barwidth(.6) color(red))  ///
  51. (rbar p75 p25 date if col==1, barwidth(.6) color(green))       ///
  52. (rbar p75 p25 date if col==2, barwidth(.6) color(black))       ///
  53. (rspike uav lav date if col==2, lwidth(thick) color( black))   ///
  54. (rspike uav lav date  if col==1 , lwidth(thick) color( green)) ///
  55. (rspike uav lav date if col==0 , lwidth(thick) color(red)  ) , ///
  56. legend(off)
复制代码

Graph.png



39
niuniuyiwan 在职认证  发表于 2015-8-27 21:45:44
  1. // Adding average line to bar graph

  2. webuse grunfeld, clear   //getting data online
  3. keep if company == 1
  4. keep if year < 1945

  5. generate year2 = year + 0.3
  6. generate year3 = year - 0.3

  7. generate average = (invest + mvalue + kstock) / 3

  8. twoway bar invest year3, barw(0.3) || ///
  9. bar mvalue year, barw(0.3)         || ///
  10. bar kstock year2 , barw(0.3)       || ///
  11. line average year , name(a2, replace)

  12. *(Nick Cox - Stata list,  13/4/11)
复制代码

a2.png



40
niuniuyiwan 在职认证  发表于 2015-8-27 23:09:57
  1. // Graph Title - Includes years
  2. clear

  3. inp str10 date ricepr
  4. "01/01/2006"   700
  5. "01/02/2006"   700
  6. "01/03/2006"   900
  7. "01/04/2006"   900
  8. "01/05/2006"   900
  9. "01/06/2006"   900
  10. "01/07/2006"   900
  11. "01/08/2006"   933.33
  12. "01/09/2006"   1000
  13. "01/10/2006"   1000
  14. "01/11/2006"   1000
  15. "01/12/2006"   1000
  16. "01/01/2007"   1050
  17. "01/02/2007"   1087.5
  18. "01/03/2007"   1100
  19. "01/04/2007"   1100
  20. "01/05/2007"   1100
  21. "01/06/2007"   1100
  22. "01/07/2007"   1175
  23. "01/08/2007"   1200
  24. "01/09/2007"   1200
  25. "01/10/2007"   1300
  26. "01/11/2007"   1400
  27. "01/12/2007"   1400
  28. end

  29. gen date2=date(date,"DMY")
  30. gen monthlabel = substr("JFMAMJJASOND", month(date2), 1)

  31. replace monthlabel=monthlabel+ " " + string(year(date2),"%5.0f") ///
  32. if month(date2)==1

  33. labmask date2, values(monthlabel) //user written program, must be installed first
  34. levelsof date2, local(levels)
  35. line ricepr date2, xla(`levels', valuelabel angle(45))

  36. *(Nick Cox - Stata list,  Tue, 04 May 2010)
复制代码

Graph.png



您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 20:36