軸線地塊的變化格式的值我在matplotlib新的,我想改變x軸的值的格式,如:在matplotlib
X軸:250000 500000等更改爲格式0.250,0.500等。
我能爲此做些什麼?
謝謝
軸線地塊的變化格式的值我在matplotlib新的,我想改變x軸的值的格式,如:在matplotlib
X軸:250000 500000等更改爲格式0.250,0.500等。
我能爲此做些什麼?
謝謝
您通常只會在繪圖前縮放數據。因此,您可以使用plt.plot(x,y/1e6)
而不是plt.plot(x,y)
。
要格式化小數點後3位的值,請使用matplotlib.ticker.StrMethodFormatter
並提供一個帶3位小數的格式,在這種情況下爲"{x:.3f}"
。
import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np; np.random.seed(42)
x = np.arange(5)
y = np.array([5e5,2e5,0,3e5,4e5])
plt.plot(x,y/1e6)
plt.gca().yaxis.set_major_formatter(matplotlib.ticker.StrMethodFormatter("{x:.3f}"))
plt.show()
謝謝,我做到了: def numfmt(x,pos):#你自定義的格式化函數:除以100.0 s ='{}'格式(x/1000000.0) return s;和xfmt = tkr.FuncFormatter(numfmt) plt.gca()。xaxis.set_major_formatter(xfmt) –
這個答案有什麼問題? – ImportanceOfBeingErnest
我想邀請您在提問時閱讀[問]和[mcve]並堅持這些指南。 – ImportanceOfBeingErnest