」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何在 Matplotlib 中指定浮點值的刻度標籤格式?

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

發佈於2024-11-07
瀏覽:537

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

格式化浮點值的刻度標籤

在matplotlib 中,您可以指定浮點值的刻度標籤的格式以顯示特定的小數位或抑制科學計數notation.

要實現此目的,您可以使用matplotlib.ticker 模組中的FormatStrFormatter 類別。此格式化程式允許您為標籤指定格式字串。

例如,要在 y 軸上顯示兩位小數,您可以使用以下程式碼:

import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter

fig, ax = plt.subplots()

ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))

要抑制科學記數法,請使用格式字串,例如:

ax.yaxis.set_major_formatter(FormatStrFormatter('%f'))

ax.yaxis.set_major_formatter」 (FormatStrFormatter(' %f'))

# ... (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'))

# ...(與在原始片段中)... axarr[0].yaxis.set_major_formatter(FormatStrFormatter('%.2f')) axarr[1].yaxis.set_major_formatter(FormatStrFormatter('%.2f')) axarr[2].yaxis.set_major_formatter(FormatStrFormatter('%.2f'))

透過使用FormatStrFormatter,您可以精確控制刻度標籤的格式以滿足您的特定視覺化需求。 How to Specify the Format of Tick Labels for Floating-Point Values in Matplotlib?

版本聲明 本文轉載於:1729564096如有侵犯,請洽[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3