簡體   English   中英

Matplotlib - 對數刻度,但需要非對數標簽

[英]Matplotlib - logarithmic scale, but require non-logarithmic labels

如何阻止y軸在y軸上顯示對數表示法標簽?

我對對數刻度感到滿意,但想要在Y軸上顯示絕對值,例如[500,1500,4500,11000,110000]。 我不想明確標記每個標記,因為標簽可能在將來發生變化(我嘗試過不同的格式化程序,但沒有成功地使它們工作)。 示例代碼如下。

謝謝,

-collern2

import matplotlib.pyplot as plt
import numpy as np

a = np.array([500, 1500, 4500, 11000, 110000])
b = np.array([10, 20, 30, 40, 50])

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_yscale('log')

plt.plot(b, a)
plt.grid(True)
plt.show()

如果我理解正確,

ax.set_yscale('log')

任何

ax.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.yaxis.set_major_formatter(matplotlib.ticker.FormatStrFormatter('%d'))
ax.yaxis.set_major_formatter(matplotlib.ticker.FuncFormatter(lambda x, pos: str(int(round(x)))))

應該管用。 如果刻度標簽位置在4.99之類的地方結束,'%d'將會出現問題,但您明白了。

請注意,您可能需要對次格式化程序set_minor_formatter執行相同set_minor_formatter ,具體取決於軸的限制。

使用ticker.FormatStrFormatter

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import ticker

a = np.array([500, 1500, 4500, 11000, 110000])
b = np.array([10, 20, 30, 40, 50])

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.set_yscale('symlog')

ax.yaxis.set_major_formatter(ticker.FormatStrFormatter("%d"))

plt.plot(b, a)
plt.grid(True)

plt.show()

暫無
暫無

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

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