pyplot的:loglog()与基地e [英] pyplot: loglog() with base e

查看:29
本文介绍了pyplot的:loglog()与基地e的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

import matplotlib.pyplot as pyplot 
import math

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

其中 degreesrange(1,len(degrees)+1) 的每个值处的计数向量.出于某种原因,当我运行此代码时,pyplot不断给我一个坐标为2的幂的坐标图.我觉得这应该很容易,但是我很困惑...

Where degrees is 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...

非常感谢任何建议!

推荐答案

使用plt.loglog 你可以传递关键字参数 basexbasey 如下所示.

When plotting using plt.loglog you can pass the keyword arguments basex and basey as shown below.

从 numpy 你可以获得 e 常量和 numpy.e (或者 np.e 如果你 import numpy as np)

From numpy you can get the e constant with numpy.e (or np.e if you 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()

编辑

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

Edit

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

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()

这篇关于pyplot的:loglog()与基地e的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆