>  기사  >  백엔드 개발  >  Matplotlib에서 부동 소수점 값에 대한 눈금 레이블 형식을 지정하는 방법은 무엇입니까?

Matplotlib에서 부동 소수점 값에 대한 눈금 레이블 형식을 지정하는 방법은 무엇입니까?

Patricia Arquette
Patricia Arquette원래의
2024-10-22 10:33:02744검색

How to Specify the Format of Tick Labels for Floating-Point Values in Matplotlib?

부동 소수점 값에 대한 눈금 레이블 형식 지정

matplotlib에서는 부동 소수점 값에 대한 눈금 레이블 형식을 지정하여 특정 소수 자릿수를 표시하거나 과학적 표시를 억제할 수 있습니다. 표기법.

이를 달성하려면 matplotlib.ticker 모듈의 FormatStrFormatter 클래스를 사용할 수 있습니다. 이 포맷터를 사용하면 레이블의 형식 문자열을 지정할 수 있습니다.

예를 들어 y축에 소수점 두 자리를 표시하려면 다음 코드를 사용할 수 있습니다.

<code class="python">import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter

fig, ax = plt.subplots()

ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))</code>

과학적 표기법을 억제하려면 다음과 같은 형식 문자열을 사용하세요.

<code class="python">ax.yaxis.set_major_formatter(FormatStrFormatter('%f'))</code>

이 포맷터를 코드에 적용하면 서브플롯의 y축에서 원하는 형식을 얻을 수 있습니다.

<code class="python"># ... (same code as in the original snippet) ...

axarr[0].yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
axarr[1].yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
axarr[2].yaxis.set_major_formatter(FormatStrFormatter('%.2f'))</code>

FormatStrFormatter를 사용하면 특정 시각화 요구 사항에 맞게 눈금 레이블의 형식을 정확하게 제어할 수 있습니다.

위 내용은 Matplotlib에서 부동 소수점 값에 대한 눈금 레이블 형식을 지정하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.