[英]Fixing x-axis and y-axis in a log plot python
嘗試使用 ´´´ Axes.set_yscale ´´´ 方法,使用此方法,您可以在創建 Axes 后更改比例。
嘗試使用這個:´´´ ax.set_yscale('log')
'''
使用 ScalarFormatter 控制指數顯示並更改刻度設置。 請參閱此頁面了解更多信息。
import numpy as np
import random
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
x = np.linspace(620000,680000,200)
step = np.random.choice([-1500,1500],200)
y = np.cumsum(step)
fig, ax = plt.subplots()
ax.plot(x, y, label='P' )
ax.plot(x, [np.mean(y)]*len(y), label='meanP')
ax.legend()
ax.set_xlabel('Time')
ax.set_ylabel('P')
ax.yaxis.set_major_formatter(ScalarFormatter(useMathText=True))
ax.xaxis.set_major_formatter(ScalarFormatter(useMathText=True))
ax.ticklabel_format(style="sci", axis="both", scilimits=(0,0))
plt.show()
要重現您的示例,您不需要使用對數刻度,您需要軸刻度的科學記數法(有關matplotlib 文檔的更多信息):
import matplotlib.pyplot as plt
import numpy as np
time = np.linspace(6e-4,7e-4,200)
y = np.random.rand(200)*1e4
plt.figure(1)
plt.plot(time, y, 'r',label='P' )
plt.axhline( y=np.mean(y), label='meanP')
plt.legend()
plt.xlabel('Time')
plt.ylabel('P')
plt.ticklabel_format(
style='sci',
axis='x', # x, y or both
scilimits=(-1,1), # to force it scientific notation
useOffset=True, # to use offset text with the exponent
useMathText=True, # to make it 'x 10^n' instead of '1en'
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.