1. 程式人生 > 其它 >4大分析工具的程式碼表白朮,520花式秀恩愛!

4大分析工具的程式碼表白朮,520花式秀恩愛!

儘管笛卡爾和瑞典公主的故事已被證實只是杜撰,但因這個故事出名的心形函式被廣為流傳。今天又是一個虐單身狗的日分析師子,面對各種毫無新意的表白方式,讓我們來看看理工科式的表白~

一切都是從一個故事開始的:

1650年,斯德哥爾摩街頭,一個寧靜的午後,笛卡爾邂逅了18歲的瑞典公主克里斯汀。

機遇巧合,一段純粹、美好的愛情悄然萌發。

然而,沒過多久,他們的戀情傳到了國王的耳朵裡。國王大怒,將笛卡爾放逐,而公主被軟禁在宮中。 身體孱弱的笛卡爾不久便染上重病,在生命進入倒計時的那段日子,他日夜思念的還是街頭偶遇的那張溫暖的笑臉。他每天堅持給她寫信,盼望著她的迴音。然而,這些信都被國王攔截下來。在笛卡爾給克里斯汀寄出第十三封信後,他永遠地離開了這個世界。此時,被軟禁在宮中的小公主依然徘徊在皇宮的走廊裡,思念著遠方的情人。

這最後一封信上沒有寫一句話,只有一個方程:r=a(1-sinθ) ——傳說中著名的心形函式。

不過,事實告訴我們,除非你的目標妹子也是一隻Geeker(至少會用Mathematica或者MATLAB等軟體),否則像笛卡爾這樣單給一個函式的結局大概就是你推公式別人推妹子了……表白什麼的還是選擇更淺顯易懂的方法吧。

接下來看看用各種資料分析軟體做出的心形模型,保證亮瞎你的表白物件!

Stata

在這個特殊的日子裡,特意蒐集了利用Stata繪製心形線的程式,程式碼如下:

clear
range t 0 2*_pi 1000
gen x=16*sin(t)^3
gen y=13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t)
egen x_min=min(x)
egen x_max=max(x)
egen y_min=min(y)
egen y_max=max(y)
gen a=(x-x_min)/(x_max-x_min)
gen b=(y-y_min)/(y_max-y_min)
line b a
gr_edit yaxis1.draw_view.setstyle, style(no)
gr_edit xaxis1.draw_view.setstyle, style(no)
gr_edit plotregion1.AddTextBox added_text editor .7055394244311991 .2810707216715078
gr_edit plotregion1.added_text_new = 1
gr_edit plotregion1.added_text_rec = 1
gr_edit plotregion1.added_text[1].style.editstyle  angle(default) size(medsmall) color(red) horizontal(left) vertical(middle) margin(zero) linegap(zero) drawbox(no) boxmargin(zero) fillcolor(bluishgray) linestyle( width(thin) color(red) pattern(solid)) box_alignment(east) editcopy
gr_edit plotregion1.added_text[1].style.editstyle size(large) editcopy
gr_edit plotregion1.added_text[1].text = {}
gr_edit plotregion1.added_text[1].text.Arrpush  "           I LOVE YOU"
graph export "C:Desktop520.png", as(png) wid(800)hei(600) replace

以下為輸出結果:

以上程式基於stata12執行,如果有stata14的話,還可以輸入中文!祝大家好運!

R

接下來是R繪製心形線,程式碼如下:

1)加載入程式包

library(grid)

2)繪製心形函式

heart <- function(lcolor){
t=seq(0, 2*pi, by=0.1)

x=16*sin(t)^3

y=13*cos(t)-5*cos(2*t)-2*cos(3*t)-cos(4*t)

a=(x-min(x))/(max(x)-min(x))

b=(y-min(y))/(max(y)-min(y))

grid.lines(a,b,gp=gpar(col=lcolor,lty = "solid",lwd = 6))

}

heart("hotpink")

grid.newpage()

3)繪製玫瑰函式

rose=function(){
grid.circle(x=0.5, y=0.5, r=0.5,gp=gpar(fill="red",lwd = 3))

vp <- viewport(.5, .5, w=.9, h=.9)

pushViewport(vp)

grid.polygon(x=c(0.08, .5, 0.94),y=c(.22, 1.03, .22),gp=gpar(lwd =

vp2 <- viewport(.5, .5, w=.4, h=.4)

pushViewport(vp2)

grid.circle(x=0.5, y=0.5, r=0.5,gp=gpar(fill="red",lwd = 3))

vp3 <- viewport(.5, .5, w=.9, h=.9,angle=180)

pushViewport(vp3)

grid.polygon(x=c(0.08, .5, 0.94),y=c(.22, 1.03, .22),gp=gpar(lwd = 3))}

rose()

4)調整圖形

grid.newpage()
pushViewport(viewport(x=0.1, y=0.1,w=0.2, h=0.2))

grid.newpage()

for (j in 1:30) {

vp <- viewport(.5, .5, w=.9, h=.9)

pushViewport(vp)

heart("hotpink")

}

5)調整圖形及新增文字

grid.newpage()
vp1 <- viewport(.4, .5, w=.5, h=.5,angle=15)

pushViewport(vp1)

heart("red")

vp2 <- viewport(0.9, .27, w=.7, h=.7,angle=-30)

pushViewport(vp2)

heart("hotpink")

grid.text("執手偕老",

x=0.2,y =1.2, just = c("center", "bottom"),

gp = gpar(fontsize=30), vp = vp)

vp3 <- viewport(-0.65, 1.2, w=.3, h=.3,angle=-30)

pushViewport(vp3)

rose()

結果如下:

注:操作平臺為Rstudio,由於手頭電腦沒有裝R,所以上述程式沒有驗證,就交給大家自己實踐了!祝好運!

以上程式來自:

http://mp.weixin.qq.com/s?__biz=MzA4NTAyMjQ2Mg==&mid=206788501&idx=1&sn=53e07723fdb395a787e57ea53bffd332&scene=21#wechat_redirect

Matlab

利用matlab抱得美人歸:

f=@(x,y,z) x.^2.*z.^3+9*y.^2.*z.^3/80-(x.^2+9*y.^2/4+z.^2-1).^3;%心形曲面函式

[x,y,z]=meshgrid(-1.5:0.1:1.5);%畫圖範圍

v=f(x,y,z);

%畫圖

h=patch(isosurface(x,y,z,v,0));

isonormals(x,y,z,v,h)

set(h,'FaceColor','r','EdgeColor','none');

title('Programmed By Dylan Wang')

alpha(0.6)

grid off;

axis([-1.5 1.5 -1.5 1.5 -1.5 1.5])

lighting Gouraud

h = camlight('left');

for i = 1:180;%水平旋轉照相機

camorbit(1,0)

camlight(h,'left');

drawnow;

end

效果如斯:

SAS

1)準備心形圖的圖形資料

data heart;

length side $ 8;

pi=constant('PI');

do t=0 to 60 by 100/200;

x = -.01*(-t**2+40*t+1200)*sin(pi*t/180);

y = .01*(-t**2+40*t+1200)*cos(pi*t/180);

side='left';

output;

side='right';

x=-1*x;

output;

end;

run;

2)準備圖形上的文字

這個最重要,字一定要大,而且千萬記得署上自己的名字。過年的時候收到幾條沒有署名的祝福簡訊,鬱悶的我也不知道該怎麼問人家名字。

data anno;
input textsize y1 function :$8. label $40.;
retain drawspace "GRAPHPERCENT"

function "text"

width 80

widthunit 'percent'

textcolor "black"

textweight "bold";

cards;

20 50 text 5.20  我愛你!

15 45 text 經管之家

;

run;

3)畫圖

ods listing;

ods graphics / width=600px height=600px noborder;

proc sgplot data=heart sganno=anno noautolegend;

title "Heart";

series x=x y=y /group=side lineattrs=(color=red);

xaxis display=none;

yaxis display=none;

run;quit;

用SAS畫曲線有多種方式:

1)用ScatterPlot畫圖是最自然的方式,不過需要10000個點心形圖看上去才比較舒服。因為曲線的邊有些鋸齒,所以我不太喜歡。

2)用Series畫圖,這個需要點技巧。對資料不做特殊處理的話,畫出來的是這樣,不過也很美很特別。

3)我建議大家動手試試其他的語句,比如pbsplineplot,會有各種驚喜。

為了能出下面這個心形圖,我特意加了side變數,然後通過lineattrs=(color=red)強行將兩組資料用紅色畫線,而不是預設的一藍一紅。

Do you love SAS? Yes, I DO.(行動才是愛!)

Python

愛心的python表示:

1.第一種

"""

'17*x^2 - 16*|x|*y + 17*y^2 = 225'

"""

import numpy as np

import matplotlib.pyplot as plt

X = np.arange(-5.0, 5.0, 0.1)

Y = np.arange(-5.0, 5.0, 0.1)

x, y = np.meshgrid(X, Y)

f = 17 * x ** 2 - 16 * np.abs(x) * y + 17 * y ** 2 - 225

fig = plt.figure()

cs = plt.contour(x, y, f, 0, colors = 'r')

plt.show()

2.第二種

"""

'(x^2+y^2+y)^2 = x^2 + y^2'

"""

import numpy as np
import matplotlib.pyplot as plt
X = np.arange(-2.0, 2.0, 0.05)
Y = np.arange(-2.0, 2.0, 0.05)
x, y = np.meshgrid(X, Y)
f = (x ** 2 + y ** 2 + y) ** 2 - x ** 2 - y ** 2
fig = plt.figure()
cs = plt.contour(x, y, f, 0, colors = 'r')
plt.show()

3.第三種

"""
'8*x^2 - 9*|x|*y + 8*y^2 = 17'
"""
import numpy as np
import matplotlib.pyplot as plt
X = np.arange(-2.5, 2.5, 0.05)
Y = np.arange(-2.5, 2.5, 0.05)
x, y = np.meshgrid(X, Y)
f = 8 * x ** 2 - 9 * np.abs(x) * y + 8 * y ** 2 - 17
fig = plt.figure()
cs = plt.contour(x, y, f, 0, colors = 'r')

4.第四種

"""
'(x^2 + y^2 - 1)^3 - x^2*y^3 = 0'
"""
import numpy as np
import matplotlib.pyplot as plt
import math
X = np.arange(-2.0, 2.0, 0.05)
Y = np.arange(-2.0, 2.0, 0.05)
x, y = np.meshgrid(X, Y)
f = (x ** 2 + y ** 2 - 1) ** 2 * (x ** 2 + y ** 2 - 1)- x ** 2 *  y ** 2 * y
fig = plt.figure()
cs = plt.contour(x, y, f, 0, colors = 'r')
plt.show()

最後,感謝那些做出巨大貢獻的程式猿,沒有他們的程式碼,表白怎能如此風騷!!!

經管之家綜合自網路