微信公眾號:數據皮皮俠
如果你覺得該公眾號對你有幫助,歡迎關注、推廣和宣傳
一直覺得學一門計算機語言,應該找到學習它的樂趣,或者知道它的用途。\只有如此,我們才能對其學習有持之以恆的動力。而數據可視化就是這種能夠
帶來樂趣的學習方式。因此,讓我們從Julia的一個庫PyPlot入手,感受其可視化\帶來的快樂。這也是最近在用PyPlot,所以整理了一些現成的PyPlot畫圖的資源,\做個記號,便於隨手使用。
PyPlot的幾種畫圖方法1.畫一個立體球2.繪製折線圖3.繪製手繪風格的曲線圖4.創建條形圖5.繪製水平條形圖6. 繪製餅圖7.繪製散點圖8.繪製方塊圖
PyPlot的幾種畫圖方法首先,我們需要在Julia上按照PyPlot軟體包。
安裝PyPlot包
using Pkg
Pkg.add("PyPlot")
using PyPlot
n = 100
u = linspace(0,2*π,n);
v = linspace(0,π,n);
x = cos(u) * sin(v)';
y = sin(u) * sin(v)';
z = ones(n) * cos(v)';
# The rstride and cstride arguments default to 10
surf(x,y,z, rstride=4, cstride=4,"red")
using PyPlot
x=1:50
xlabel("x")
y=rand(50)
ylabel("y")
title("base plot")
grid("on")
plot(x,y)
x=1:10
xlabel("x")
y=ones(10)
for i=1:1:10
y[i]=-i*i*i
end
ylabel("y")
title("XKCD plot")
xkcd()
plot(x,y)
x=[1,2,3,4,5]
y=[1,2,4,8,16]
bar(x,y,color="blue")
x=[1,2,3,4,5]
y=[1,2,4,8,16]
barh(x,y,color="green")
labels=["google";"apple";"MS";"xiaomi"]
colors=["orange";"blue";"red";"green"]
sizes=[200;900;30;1400]
fig=figure("pyplot_piechart",figsize=(10,10))
p=pie(sizes,labels=labels,shadow=true,startangle=90,colors=colors)
title("pie plot")
fig=figure("scatterplot",figsize=(10,10))
x=rand(50)
y=rand(50)
areas=10000*rand(50)
scatter(x,y,s=areas,alpha=0.5)
grid("on")
title("Scatter plot")
x=rand(100)
y=rand(100)
xlabel("x")
ylabel("y")
title("hist2D plot"
hist2D(x,y,bins=10)
using PyPlot
(X1, Y1) = (rand(6), rand(6));
(X2, Y2) = (rand(6), rand(6));
(X3, Y3) = (rand(6), rand(6));
fig = figure(figsize=(10,10))
# xlabel("My X Label") # optional x label
# ylabel("My Y Label") # optional y label
title("Julia Plots Like a Boss")
R = scatter(X1,Y1,color="red", label = "Red Data", s = 40)
G = scatter(X2,Y2,color="blue", label = "Blue Data", s = 60)
B = scatter(X3,Y3,color="green", label = "Green Data", s = 80)
legend(loc="right")
通過這些圖的練習,可以感受Julia語言的寫作風格,當然,在調試過程會有一些錯誤也是在所難免。