본문 바로가기

IT/Python

[Matplotlib] AttributeError: 'Axes' object has no attribute 'get_zticklabels'

기존에 그래프를 정리하는 부분에서 일괄적으로 아래처럼 처리했다.

    for tick in ax.get_xticklabels():
        tick.set_fontname("Arial")
        tick.set_fontsize(7)
    for tick in ax.get_yticklabels():
        tick.set_fontsize(7)
        tick.set_fontname("Arial")
    for tick in ax.get_zticklabels():
        tick.set_fontsize(7)
        tick.set_fontname("Arial")

 

문제는 3d graph가 아닌 경우 zticklabel이 없음.

 

이 부분을 확인해야하는데 chatGPT한테 물어보니 멍청한 짓을 하고 있음.

 

죄송합니다. 제가 실수했습니다. Matplotlib의 3D 그래프에서 Z 축의 눈금값을 가져오는 메서드는 get_zticks()가 아닙니다. 대신에 get_zticks() 대신 get_zticks() 메서드를 사용해야 합니다. 이를 수정하여 다시 시도해 보겠습니다.

 

좀 웃기는게, matplotlib 기본 documentation에는 z_axis 관련된 내용이 없음.

 

Axes3D 항목을 타고 들어가야 있는데, mpl_toolkits.mplot3d.axes3d.Axes3D — Matplotlib 3.8.4 documentation

 

mpl_toolkits.mplot3d.axes3d.Axes3D — Matplotlib 3.8.4 documentation

a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array and two offsets from the bottom left corner of the image

matplotlib.org

 

그래서 2가지 방법이 있을 수 있음.

1. ax의 타입이 mpl_toolkits.mplot3d.axes3d.Axes3D 인지 기본 axe인 확인 하는 방법

if type(ax) == mpl_toolkits.mplot3d.axes3d.Axes3D:

2. ax 에 attribute가 zaxis 가 있는지 확인하는 방법

if hasattr(ax, 'zaxis'):

 

큰 차이는 없을 것 같음..