본문 바로가기

Informations/python4

python에서 특정 폴더에 존재하는 txt파일 가져오기 import os import glob # 파일을 찾을 경로 path = '/경로/여기에/파일을/찾을/폴더' # 해당 경로에서 확장자가 ".txt"인 파일 목록 가져오기 files = glob.glob(os.path.join(path, '*.txt')) file_list = [] # 파일 목록 출력 for file in files: file_list.append(file) os와 glob 를 사용하면 된다! 2023. 6. 23.
python, Jupyter notebook 한글 깨짐 해결, figure 한글 쓰기 주피터를 바로 깔고 나서는 한글이 들어간 그림을 그릴 수 없다. 왜냐면 python matplotlib에 기본 폰트중에 한글 지원 가능한 폰트가 없기 때문이다! 그래서 아래와 같이 된다. import matplotlib.pyplot as plt plt.figure(figsize=(5,5)) x=list(range(10)) y=x plt.scatter(x,y) plt.grid() plt.title('한글을 쓰고싶어!!!') plt.show() 해결은 간단하다. 라이브러리에 한글폰트를 넣어주면 되는것. 1) 우선 한글 폰트를 하나 가져오자. 본인 컴퓨터에 있는걸 가져와도 되고, 적당히 인터넷에서 나눔명조 다운로드를 써서 .~.ttf 파일을 받아온다. 2) 본인 matplotlib에 ttf파일 모아놓은 곳이 .. 2023. 6. 23.
matplot colormap, cmap 사용자 정의 팔렛트 만들기 # 그림용 데이터 프레임 생성 tmp = pd.DataFrame({'팔렛트만들기':list(range(38))}) tmp = tmp.T # 사용할 팔렛트 불러오기 cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ['#cc3300','#ff9966','#ffcc00','#99cc33','#339900']) # 그림그리기 plt.figure(figsize=(15,1)) ax = sns.heatmap(tmp, cmap=cmap, linewidths=1, linecolor='white') colorbar = ax.collections[0].colorbar ax.set_xticklabels([]) plt.show() 2023. 6. 23.
matplotlib cmap 뒤집기 cmap.reversed() # 그림용 데이터 프레임 생성 tmp = pd.DataFrame({'GnBu':list(range(38))}) tmp = tmp.T # 사용할 팔렛트 불러오기 cmap = plt.cm.get_cmap("GnBu") # 그림그리기 plt.figure(figsize=(15,1)) ax = sns.heatmap(tmp, cmap=cmap, linewidths=1, linecolor='white') colorbar = ax.collections[0].colorbar ax.set_xticklabels([]) plt.show() # 그림용 데이터 프레임 생성 tmp = pd.DataFrame({'reverse_GnBu':list(range(38))}) tmp = tmp.T # 사용할 팔렛트 불러오기 cmap = p.. 2023. 6. 23.