簡體   English   中英

"如何在 matplotlib 對數圖上刪除科學記數法"

[英]How to remove scientific notation on a matplotlib log-log plot

我知道以前有人問過這個問題,但是我嘗試了所有可能的解決方案,但沒有一個對我有用。

所以,我在 matplotlib 中有一個對數圖,我想避免在 x 軸上使用科學記數法。

這是我的代碼:

from numpy import array, log, pi
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import matplotlib.ticker as mticker

plt.rc('axes.formatter', useoffset=False)

tc = array([7499680.0, 12508380.0, 23858280.0, 34877020.0, 53970660.0, 89248580.0, 161032860.0, 326814160.0, 784460200.0])

theta = array([70, 60, 50, 45, 40, 35, 30, 25, 20])

plt.scatter(theta,tc)

ax=plt.gca()

ax.set_xscale('log')
ax.set_yscale('log')

ax.xaxis.set_major_formatter(mticker.ScalarFormatter())
ax.xaxis.get_major_formatter().set_scientific(False)
ax.xaxis.get_major_formatter().set_useOffset(False)

plt.show()

這些是 x 軸上的次要刻度(即它們不是 10 的整數冪),而不是主要刻度。 matplotlib<\/code>自動確定它是否應該標記主要或次要刻度 - 在這種情況下,因為您沒有在 x 范圍內顯示任何主要刻度,所以正在標記次要刻度)。 因此,您需要使用set_minor_formatter<\/code>方法:

ax.xaxis.set_minor_formatter(mticker.ScalarFormatter())

以下可以用作解決方法(原始答案<\/a>):

from matplotlib.ticker import StrMethodFormatter, NullFormatter
ax.yaxis.set_major_formatter(StrMethodFormatter('{x:.0f}'))
ax.yaxis.set_minor_formatter(NullFormatter())

如果您只想將 xaxis 設置為不再使用科學記數法,則需要更改 fromatter,然后您可以將其設置為 plain。

ax.xaxis.set_minor_formatter(mticker.ScalarFormatter())
ax.ticklabel_format(style='plain', axis='x')

如果你想同時禁用偏移量和科學記數法,你可以使用ax.ticklabel_format(useOffset=False, style='plain')<\/code>

"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM