Python pyplot:以 e 为底的 loglog()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23312803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:43:54  来源:igfitidea点击:

pyplot: loglog() with base e

pythonmatplotlib

提问by gogurt

Python (and matplotlib) newbie here coming over from R, so I hope this question is not too idiotic. I'm trying to make a loglog plot on a natural log scale. But after some googling I cannot somehow figure out how to force pyplot to use a base e scale on the axes. The code I have currently:

Python(和 matplotlib)新手从 R 过来,所以我希望这个问题不是太白痴。我正在尝试在自然对数刻度上制作对数图。但是经过一些谷歌搜索后,我无法以某种方式弄清楚如何强制 pyplot 在轴上使用基数 e 比例。我目前拥有的代码:

import matplotlib.pyplot as pyplot 
import math

e = math.exp(1)
pyplot.loglog(range(1,len(degrees)+1),degrees,'o',basex=e,basey=e)

Where degreesis a vector of counts at each value of range(1,len(degrees)+1). For some reason when I run this code, pyplot keeps giving me a plot with powers of 2 on the axes. I feel like this ought to be easy, but I'm stumped...

其中degrees是 的每个值处的计数向量range(1,len(degrees)+1)。出于某种原因,当我运行此代码时,pyplot 不断给我一个在轴上具有 2 次幂的图。我觉得这应该很容易,但我很难过......

Any advice is greatly appreciated!

任何意见是极大的赞赏!

采纳答案by Ffisegydd

When plotting using plt.loglogyou can pass the keyword arguments basexand baseyas shown below.

使用绘图时,plt.loglog您可以传递关键字参数basexbasey如下所示。

From numpy you can get the econstant with numpy.e(or np.eif you import numpy as np)

从 numpy 你可以得到e常数numpy.e(或者np.e如果你import numpy as np

import numpy as np
import matplotlib.pyplot as plt

# Generate some data.
x = np.linspace(0, 2, 1000)
y = x**np.e

plt.loglog(x,y, basex=np.e, basey=np.e)
plt.show()

Edit

编辑

Additionally if you want pretty looking ticks you can use matplotlib.tickerto choose the format of your ticks, an example of which is given below.

此外,如果您想要漂亮的刻度线,您可以使用它matplotlib.ticker来选择刻度线的格式,下面给出了一个示例。

import numpy as np

import matplotlib.pyplot as plt
import matplotlib.ticker as mtick

x = np.linspace(1, 4, 1000)

y = x**3

fig, ax = plt.subplots()

ax.loglog(x,y, basex=np.e, basey=np.e)

def ticks(y, pos):
    return r'$e^{:.0f}$'.format(np.log(y))

ax.xaxis.set_major_formatter(mtick.FuncFormatter(ticks))
ax.yaxis.set_major_formatter(mtick.FuncFormatter(ticks))

plt.show()

Plot

阴谋

回答by kashanipour

It can also works for semilogx and semilogy to show them in e and also change their name.

它也可以用于 semilogx 和 semilogy 以在 e 中显示它们并更改它们的名称。

import matplotlib.ticker as mtick
fig, ax = plt.subplots()
def ticks(y, pos):
    return r'$e^{:.0f}$'.format(np.log(y))

plt.semilogy(Time_Series, California_Pervalence ,'gray', basey=np.e  )
ax.yaxis.set_major_formatter(mtick.FuncFormatter(ticks))

plt.show()

Take a look at the image.enter image description here

看一下图像。在此处输入图片说明