簡體   English   中英

在日志中修復 x 軸和 y 軸 plot python

[英]Fixing x-axis and y-axis in a log plot python

我想要 x 軸和 y 軸

情節 1

看起來像那些

情節 2

下面的代碼來自我用來顯示數據的 python 腳本。 我正在使用 python 3.9.5。

日志軸顯示如下[1x10^-4, 2x10^-4] ,我希望它們看起來像[1, 2] x 10^-4

plt.figure(1) 
plt.plot( timeA, testA, label='P' )  
plt.plot( timeA, meanA, label='meanP')
plt.legend() 
plt.xlabel('Time')
plt.ylabel('P') 
plt.yscale('log')
plt.xscale('log')

嘗試使用 ´´´ 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.

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