【问题标题】:Creating a new array having logarithmic values of elements in another array in Python在Python中创建一个具有另一个数组中元素的对数值的新数组
【发布时间】:2011-03-03 01:01:53
【问题描述】:

嘿..我的python代码遇到了一点问题..我有一组频率和功率谱的值。我需要在对数刻度上绘制频率与功率谱的关系。我试图将频率和功率谱的对数值存储在其他 2 个变量中,然后绘制它们。知道如何做到这一点吗?

【问题讨论】:

  • 向我们展示你目前所拥有的。

标签: python numpy


【解决方案1】:

假设您有一个值列表,您可以使用简单的列表推导:

frequencies = [1, 2, 3, 4, 5]
import math
logOfFrequencies = [ math.log(x) for x in frequencies ]

或者

logOfFrequencies = map(math.log, frequencies)

如果你有一个 numpy 频率数组,因为你使用 Matplotlib / Pylab 创建你的图,你可以这样做:

import numpy
frequencies = numpy.arange(1, 5)
logOfFrequencies = numpy.log(frequencies)

【讨论】:

    【解决方案2】:

    如果您只对在对数尺度上绘制数据感兴趣,请考虑matplotlib 方法,loglogsemilogxsemilogy

    http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.loglog

    http://matplotlib.sourceforge.net/plot_directive/mpl_examples/pylab_examples/log_demo.py

    这将允许您避免计算各种数组的对数,并允许您准确自定义各种数量的显示方式。

    【讨论】:

      猜你喜欢
      • 2021-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 2022-09-30
      • 1970-01-01
      • 2022-01-03
      相关资源
      最近更新 更多