본문 바로가기
데이터시각화

[Plotly] Plotly subplot label정보 추가하기

by 느리게 걷는 즐거움 2022. 8. 29.
728x90
반응형

Plotly x, y label 정보 업데이트 

Plotly에 subplot graph를 작성하고 각 subplot 그래프의 x,y label을 추가하는 경우 update_xaxes 정보를 반복적으로 많이 사용해야 했다.

코드의 가독성을 떨어뜨리고 변경이 어려운 문제가 있어서 효율적으로 정보를 수정하기 위한 wrapper function을 만들어 본다. 유용할 만한 코드를 모아 놓고 관리하면 나중에 편하기 쓸 수 있을 것 같다.

def add_subplot_labels(fig, axis, label_name, num_row, num_col):
    axis_name = label_name
    num_col = num_col
    
    if axis == 'x':
        fn = lambda title_text, col, row: fig.update_xaxes(title_text = title_text, col = col, row = row)
    elif axis == 'y':
        fn = lambda title_text, col, row: fig.update_yaxes(title_text = title_text, col = col, row = row)

       
    for index, _name in enumerate(axis_name):
        _col = int(index%num_col)
        _row = int(index/num_col)

        fn(
            title_text=_name,
            col = _col+1,
            row = _row+1,
        )

 

ToDos

- 결국 updata_xaxes에 업데이트할 정보가 label정보 외에도 있을텐데 그 경우 입력파라메터가 늘어나야 하고 그렇다면 정보를 figure외의 정보는 dict 형식으로 받는 것이 좋을 것 같기도 하다.

- doc string에 익숙해질 필요하 있음. 함수를 만들때 doc string을 안만든다....

728x90
반응형