首頁  >  文章  >  後端開發  >  如何在 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