简体   繁体   中英

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'm aware that there is no 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):

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. The second (out-commented) way is to use set_yscale as a method of the AxesSubplot -object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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