簡體   English   中英

是否有可能使用 matplotlib 在對數刻度上顯示 0?

[英]Is there a possibillity to show the 0 on an logarithmic scale with matplotlib?

I have to show the plot of a function with matplotlib in python with logarithmic scale on the x axis and I wonder if there is any possibility to show x=0 ? 我知道沒有log(0) ...

from matplotlib import pyplot as plt 
def fkt(x,e):
    y=np.sin(1/(x+e))
    return y

def bild_fkt():
    """plottet die Funktion im Intervall[0,1] mit epsilon=1/5, 1/10, 1/20 mit linearer und 
    logarithmischer Skala """
    plt.figure(figsize=(10,10))       
    x=np.linspace(0,1,100)
    plt.subplot(211)   #plots on linear scale
    plt.plot(x, fkt(x,1/5), 'r', lw=2, label='e=1/5')
    plt.plot(x, fkt(x,1/10), '-.', color='b', label='e=1/10')
    plt.plot(x, fkt(x,1/20), ':', color='g', label='e=1/20')
    plt.legend(loc=4)
    plt.grid()

    y=np.logspace(0,0.3010299957,100)   #plots on logarithmic scale
    plt.subplot(212)
    plt.plot(y-1, fkt(y-1,1/5), 'r', lw=2, label='e=1/5')
    plt.plot(y-1, fkt(y-1,1/10), '-.', color='b', label='e=1/10')
    plt.plot(y-1, fkt(y-1,1/20), ':', color='g', label='e=1/20')
    plt.legend(loc=4)
    plt.grid()
    plt.semilogx()

bild_fkt()

您可以使用方法yscale (或xscale分別):

import matplotlib.pyplot as plt

# create dummy data
x = list(range(0,10))
y = [10**i for i in x]

# open figure
fig, ax = plt.subplots(1,2)
# plot linear scale
ax[0].plot(x,y)
# plot logarithmic scale (two options
ax[1].plot(x,y)
plt.yscale("log")
#ax[1].set_yscale("log")

線性與對數比例圖像

請注意,我使用了兩個不同的選項來設置比例。 第一個是使用當前活動軸的 function plt.yscale("log") 第二種(注釋掉的)方法是使用set_yscale作為AxesSubplot -object 的方法。

暫無
暫無

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

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