LaTeX

Matplotlib 으로 scatter plot 그리기 (점그래프?!)

ForceCore 2011. 1. 29. 15:21
#!/usr/bin/env python2
from pylab import *

params = {
'axes.labelsize': 15,
'text.fontsize': 15,
'legend.fontsize': 15,
'xtick.labelsize': 12,
'ytick.labelsize': 12,
# 'text.usetex': True,
# 'figure.figsize': fig_size
}
rcParams.update(params)



def load_data( fname ):
f = open( fname )
data = np.loadtxt( f, delimiter=',' )
f.close
x, y = zip( *data )
return x, y



def plot_kind( k, m, c ):
x,y = load_data( "h1h2/" + k )
#plot( x, y, m )
scatter( x, y, marker=m, color=c, label=k )


#plot_kind( "tm_f", 'x' )
if 1:
plot_kind( "gm_f",  's', 'r' )
plot_kind( "lf_m",  's', 'g' )
plot_kind( "mv_f",  's', 'b' )
plot_kind( "pe_m",  's', 'c' )
plot_kind( "pf_f",  'o', 'm' )
plot_kind( "pg_f",  'o', 'y' )
plot_kind( "tg_f",  'o', 'k' )
plot_kind( "tg_m",  'o', 'r' )
plot_kind( "tm_f",  '+', 'g' )
plot_kind( "tm_m",  '+', 'b' )
plot_kind( "toc_f", '+', 'c' )
plot_kind( "toc_m", '+', 'm' )

legend( ncol=1, loc='center right',
bbox_to_anchor=(1.3,0.5), scatterpoints=1 )

# Axis stuff
#axis([1,7,52,70])
xlabel("Hue (Most frequent)")
ylabel("Hue (Second most frequent)")

# adjust margins
subplots_adjust( left=0.13, right=0.8, bottom=0.1, top=0.95 )

if 1:
# show on screen
show()
else:
# save as eps
savefig( "h1h2.png" )

plot_kind안의 함수를 주목할 것. scatter()를 썼다. plot()아님.
점으로 그래프를 그리는거 자체가 문제는 아닌데,

1. scatter()함수는 모양이 안 예쁘다는 치명적인 문제가 있다. 그래서 점 모양은 뭘로 할지, 색은 뭘로할지 수동으로 지정해줘야 좀 볼만한 모양이 나온다.

2. legend()도, scatterpoints=1 을 지정하지 않으면, legend에 점이 세 개 그려져서 나온다. 하나면 충분하구만, 디폴트가 3이라 모양이 여러모로 구려진다.

결과물:


이렇게 됨.