简体   繁体   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 ? 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 ? I'm aware that there is no log(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()

You can use the method yscale (or xscale respectively):您可以使用方法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")

线性与对数比例图像

Note that I have used two different options to set the scale.请注意,我使用了两个不同的选项来设置比例。 The first is the function plt.yscale("log") that uses the current active axis.第一个是使用当前活动轴的 function plt.yscale("log") The second (out-commented) way is to use set_yscale as a method of the AxesSubplot -object.第二种(注释掉的)方法是使用set_yscale作为AxesSubplot -object 的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM