반응형
graphsetting_reset

그래프 설정 리셋

가끔가다 그래프 설정의 변경으로 잘 나오던 그래프가 깨지는 경우가 종종 나온다. matplotlib과 seaborn 등 여러가지를 막 섞어 쓰다보면 가끔 발생한다.

다음과 같이 잘 나오던 그래프가
image

어느 순간 다시 똑같은 코드를 실행했는데, 이런 결과가…
(위와 아래는 똑같은 코드다.)
image

Jupyter Lab에서 커널을 다시 기동해야지만 다시 원래대로 나오는데, 마찬가지로 중간에 어떤 작업으로 인해 그래프 설정이 변경되면 위와 같이 다시 이상하게 출력되었다. 나중에 원인을 찾아보니… 아래의 sns.set() 코드였다.
image
이후 부터는 이상한 그래프가 출력된 것이다. 원상 복구를 하는 것을 찾아보다가 reset해 주는 코드를 찾았다.
또한 설정값 변경된 것을 확인해 주는 코드도 있다.

image
위와 같이 pltconfig_default() 함수를 만들어 사용하면 원래 설정대로 복원된다.

참고로 다음은 plot 설정값이 디폴트에서 변경된 내역을 찾아주는 코드다.

# plot 환경설정 기본값과 달라진 점
class DictDiffer(object):
    """
    Calculate the difference between two dictionaries as:
    (1) items added
    (2) items removed
    (3) keys same in both but changed values
    (4) keys same in both and unchanged values
    """
    def __init__(self, current_dict, past_dict):
        self.current_dict, self.past_dict = current_dict, past_dict
        self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys())
        self.intersect = self.set_current.intersection(self.set_past)
    def added(self):
        return self.set_current - self.intersect 
    def removed(self):
        return self.set_past - self.intersect 
    def changed(self):
        return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
    def unchanged(self):
        return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])
    
def pltconfig_check():
    d=DictDiffer(plt.rcParams, plt.rcParamsDefault)
    for it in d.changed():
        print(it, plt.rcParamsDefault[it], plt.rcParams[it])

위에서 sns 환경설정 변경으로 기존의 세팅과 변경된 내역을 확인해 보자.
image
상당히 많은 설정값들이 변경된 것을 확인하였다.

image

다시 원래 디폴트로 돌아온 것이다. 위에서 나온 변경내역은 matplotlib inline으로 인해 변경된 내역이므로 신경쓰지 않아도 된다.

Author: crazyj7@gmail.com
Written with StackEdit.

'Python' 카테고리의 다른 글

초간단 python 서버만들기2  (0) 2019.09.25
초간단 웹API서버만들기  (0) 2019.09.25
JupyterLab에서 Python Script(.py)실행하기  (0) 2019.09.19
Jupyter Notebook 멀티라인출력  (0) 2019.09.19
진행(progress)바/ tqdm  (2) 2019.08.28

+ Recent posts