我觉得我的女神很漂亮,想把她的照片当成图表背景,该怎么做?

我觉得我的女神很漂亮,想把她的照片当成图表背景,该怎么做?

这个绘图方法来自于http://www.surveydesign.com.au/tipsgraphs.html, 该网站上给出了两种方法,经过好几天的尝试发现只有第一种比较好用,我们先来试试第一种。(这是是用纯Stata绘制, 实际上使用一些JavaScript图表库非常容易绘制)

水彩风格

工具

  • 图片转换网站:http://www.text-image.com/convert/ascii.html
  • 平台:Stata
  • 女神照片一张
  • 绘制图表的数据集一个:系统数据集auto
  • 思想:把图表上的所有像素点转换成数据值,然后绘制散点图

    步骤

  1. 把图片上传到这个网站,注意宽度设置成500。
  2. 把转换的结果保存为一个txt文件,这里我命名为xiaoxiao.txt
  3. 准备数据集,我们以系统数据集auto.dta为例:
  4. 运行下面的代码(注意代码里面的颜色是自己根据图片配出来的,可以使用RGB颜色编码):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
infix strL a1-a500 1-500 using "xiaoxiao.txt", clear
gen y = 90 + ((_N-_n+1)*1)
reshape long a, i(y) j(col)

replace a = "1" if a == "+"
replace a = "2" if a == "-"
replace a = "3" if a == "."
replace a = "4" if a == "/"
replace a = "5" if a == ":"
replace a = "6" if a == "-"
replace a = "7" if a == "M"
replace a = "8" if a == "N"
replace a = "9" if a == "`"
replace a = "10" if a == "d"
replace a = "11" if a == "h"
replace a = "12" if a == "m"
replace a = "13" if a == "o"
replace a = "14" if a == "s"
replace a = "15" if a == "y"

replace col = col/10

merge 1:1 _n using auto.dta
local size = "vtiny"

tw ///
(sc y col if a == "1" ,msymbol(circle) ///
mcolor("0 0 0*.2") msize(`size') ) ///
(sc y col if a == "2" ,msymbol(circle) ///
mcolor("255 255 255*.2") msize(`size')) ///
(sc y col if a == "3" ,msymbol(circle) ///
mcolor("255 0 0*.2") msize(`size') ) ///
(sc y col if a == "4" ,msymbol(circle) ///
mcolor("0 0 255*.2") msize(`size') ) ///
(sc y col if a == "5" ,msymbol(circle) ///
mcolor("0 0 255*.2") msize(`size') ) ///
(sc y col if a == "6" ,msymbol(circle) ///
mcolor("255 255 0*.2") msize(`size') ) ///
(sc y col if a == "7" ,msymbol(circle) ///
mcolor("0 255 255*.2") msize(`size') ) ///
(sc y col if a == "8" ,msymbol(circle) ///
mcolor("255 0 255*.2") msize(`size') ) ///
(sc y col if a == "9" ,msymbol(circle) ///
mcolor(white) msize(`size') ) ///
(sc y col if a == "10",msymbol(circle) ///
mcolor(orange*.2) msize(`size') ) ///
(sc y col if a == "11",msymbol(circle) ///
mcolor(brown*.2) msize(`size') ) ///
(sc y col if a == "12",msymbol(circle) ///
mcolor(sand*.2) msize(`size') ) ///
(sc y col if a == "13",msymbol(circle) ///
mcolor(black*.2) msize(`size') ) ///
(sc y col if a == "14",msymbol(circle) ///
mcolor(black*.2) msize(`size') ) ///
(sc y col if a == "15",msymbol(circle) ///
mcolor(black*.2) msize(`size') ) ///
(sc length mpg, msize(small)) ///
(lfit length mpg) ///
,yscale( range(100 250)) ///
ytitle(长度) xtitle(里程数) legend(off)
gr export 我的女神.png, replace
  1. 结果:

(配色是自己配的。。。好像还是配的挺乱的。。)

照片风格

这个方法是使用GIMP软件把照片转换成html格式,然后读入再经过处理绘制散点图,我运行了很多次都没产生结果,因为等待时间过长而不得不取消,最长的一次是运行了21个小时(我的电脑是内存:16 GB 1867 MHz LPDDR3+处理器:1.4 GHz Intel Core i7)。我把代码放在下面,大家可以自己试一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
sysuse auto, clear
save auto, replace
clear all
cap prog drop hex
prog hex
syntax varname, Newv(name)
gen `newv' = ((strpos("`=c(alpha)'", `varlist') - 1)/2)+10
replace `newv' = real(`varlist') if `newv' == 9.5
end

local divide = 8
insheet using "xiaoxiao.html", clear
gen row = .
replace row = 1 if index(v1, "TR")
replace row = sum(row)
keep if index(v1, "<TD BGCOLOR")
split v1, p(#)
split v12, p(>)
keep row v121

gen obs = _n
bysort row (obs): gen col = _n

rename v121 hex
gen hex1 = substr(hex, 1, 1)
gen hex1a = substr(hex, 2, 1)

gen hex2 = substr(hex, 3, 1)
gen hex2a = substr(hex, 4, 1)

gen hex3 = substr(hex, 5, 1)
gen hex3a = substr(hex, 6, 1)

hex hex1, newv(dec1)
hex hex1a, newv(dec1a)

hex hex2, newv(dec2)
hex hex2a, newv(dec2a)

hex hex3, newv(dec3)
hex hex3a, newv(dec3a)

gen dec1b = 16*dec1 + dec1a
gen dec2b = 16*dec2 + dec2a
gen dec3b = 16*dec3 + dec3a

keep dec1b dec2b dec3b row col

gen dec1c = floor(dec1b/`divide')*`divide'
gen dec2c = floor(dec2b/`divide')*`divide'
gen dec3c = floor(dec3b/`divide')*`divide'

gen str20 color = char(34)+string(dec1c, "%3.0f")+" "+string(dec2c, "%3.0f")+" "+string(dec3c, "%3.0f")+char(34)

egen group_col = group(color)

compress

save pict_graph, replace

use pict_graph, clear
qui sum col

local size = "*`=33/`r(max)''"

levelsof color, local(a)

merge 1:1 _n using auto
sum length
gen y = 90 + (((_N-_n+1)-90))/(_N/r(max))

replace col = col/9
local z = 1
foreach i of local a{
if `z' == 1{
local zz = ///
`"(sc y col if group_col == `z', ms(s) mcolor(`i') msize(`size'))"'
local z = 2
}
else{
local zz1 = ///
`"(sc y col if group_col == `z', ms(s) mcolor(`i') msize(`size'))"'
local zz "`zz' `zz1'"
local ++z
}
}
set rmsg on
set scheme s1mono
tw `zz' ///
(sc length mpg, mcolor(blue) msize(large)) ///
(lfit length mpg) ///
, ysc(range(100 350)) ///
yti("Length ins.") xti("mpg") ///
title("Mpg v Length") leg(off)
set rmsg off
gr export xiaoxiao.png, replace

# Stata

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×