如何在 Matplotlib 中指定浮點值的刻度標籤格式?

Patricia Arquette
發布: 2024-10-22 10:33:02
原創
743 人瀏覽過

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中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!