![](/img/trans.png)
[英]how to show an interval in logarithmic scale and other linear in matplotlib
[英]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.