【问题标题】:Is there a possibillity to show the 0 on an logarithmic scale with matplotlib?是否有可能使用 matplotlib 在对数刻度上显示 0?
【发布时间】:2023-03-24 00:48:01
【问题描述】:

我必须在 x 轴上以对数刻度在 python 中显示带有matplotlib 的函数图,我想知道是否有可能显示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()

【问题讨论】:

  • y 轴上的刻度是多少?你说f(x) = 0,好像是y = 0的意思。
  • 哦,你是对的。在 y 轴上我想使用线性刻度,在 x 轴对数刻度上。我想说的是 x=0
  • log(0) 未定义。所以不可能在图表上绘制它。不过,将0 放在轴上是另一回事。
  • 好吧,显然不是。当您在x-axis 上靠近x = 0 时,值会越来越小,趋向于0,但从未达到它。在线性刻度上,我认为中心是0,左边有负值,右边有正值。在对数刻度上,我认为中心是1,左边是小数值,右边是倍数。

标签: python python-3.x numpy matplotlib math


【解决方案1】:

您可以使用该方法 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")

请注意,我使用了两个不同的选项来设置比例。第一个是使用当前活动轴的函数plt.yscale("log")。第二种(注释掉的)方法是使用 set_yscale 作为 AxesSubplot-object 的方法。

【讨论】:

    猜你喜欢
    • 2015-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 2013-02-21
    相关资源
    最近更新 更多