반응형

출처: http://stackoverflow.com/questions/1985856/how-to-make-a-3d-scatter-plot-in-python

 

import matplotlib.pyplot as plt
import pylab
from mpl_toolkits.mplot3d import Axes3D


fig = pylab.figure()
ax = Axes3D(fig)


qid = grouped.query('(user_number == 123)')
x = qid['loc_x'].tolist()
y = qid['loc_y'].tolist()
xy = np.vstack([x,y])
z = gaussian_kde(xy)(xy)


ax.scatter(x, y, z)
plt.show() 

 

-  gaussian_kde를 이용해서 z 축은 확률밀도 값으로 출력 

반응형
반응형

출처: http://pandas.pydata.org/pandas-docs/stable/groupby.html

 

grouped.size()

- 그룹된 결과에 size()함수를 사용하면 그룹에 속한 갯수를 확인할 수 있음

 

grouped.reset_index()

- group된 dataframe 타입에서 dataframe 타입으로 변경할 수 있음

 

grouped['user_name'].apply(lambda x: ', '.join(str(y) for y in x))

- user_name이라는 컬럼의 값이 리스트인 경우 스트링의 연결로 변경해줌

- apply와 lambda함수 사용

 

for c in grouped.groups :

  print c

- 그룹 명을 출력할 때 사용

반응형
반응형

출처: http://stackoverflow.com/questions/20105364/how-can-i-make-a-scatter-plot-colored-by-density-in-matplotlib

 

python matplotlib를 이용해서 scatter plot을 그리는 데 밀도도 반영하고 싶다면 아래와 같이 하면 됨

 

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde


# 출력할 x,y 좌표 랜덤하게 생성
x = np.random.normal(size=1000)
y = x * 3 + np.random.normal(size=1000)


# 좌표에 대한 밀도를 계산하는 부분
xy = np.vstack([x,y])
z = gaussian_kde(xy)(xy)


fig, ax = plt.subplots()


#색깔은 z에 따라서, 점의 크기는 100으로
ax.scatter(x, y, c=z, s=100, edgecolor='')
plt.show()

 

 

참고: http://stackoverflow.com/questions/24976239/axis-limits-for-scatter-plot-matplotlib

 

plt.xlim(-1,1)
plt.ylim(-1,1) 

 

- 이용해서 축의 범위를 정할 수 있음 (추후 확인해보기!)

 

참고: http://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib-so-it-can-be

 

fig.savefig('/path/image.png')

 

- plot 결과를 이미지 파일로 저장할 수 있음

 

출처 : http://blog.bharatbhole.com/creating-boxplots-with-matplotlib/

 

- python에서 boxplot 그리기

반응형

+ Recent posts