6

我正在尝试pcolor在 python 中使用 DFT(离散傅立叶变换)图。我以前一直在使用 Mathematica 8.0 来执行此操作,但我发现 Mathematica 8.0 中的颜色条与我尝试表示的数据的一对一相关性很差。例如,这是我正在绘制的数据:

[[0.,0.,0.10664,0.,0.,0.,0.0412719,0.,0.,0.],
[0.,0.351894,0.,0.17873,0.,0.,0.,0.,0.,0.],
[0.10663,0.,0.178183,0.,0.,0.,0.0405148,0.,0.,0.],
[0.,0.177586,0.,0.,0.,0.0500377,0.,0.,0.,0.],
[0.,0.,0.,0.,0.0588906,0.,0.,0.,0.,0.],
[0.,0.,0.,0.0493811,0.,0.,0.,0.,0.,0.],
[0.0397341,0.,0.0399249,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.]]

因此,它在 DFT 矩阵中有很多零或少量数字或少量高频能量。

当我使用mathematica 绘制它时,结果如下:

使用mathematica 8.0的DFT图

彩条已关闭,我想我想用 python 来绘制它。我的python代码(我从这里劫持的)是:

from numpy import corrcoef, sum, log, arange
from numpy.random import rand
#from pylab import pcolor, show, colorbar, xticks, yticks
from pylab import *


data = np.array([[0.,0.,0.10664,0.,0.,0.,0.0412719,0.,0.,0.],
[0.,0.351894,0.,0.17873,0.,0.,0.,0.,0.,0.], 
[0.10663,0.,0.178183,0.,0.,0.,0.0405148,0.,0.,0.],
[0.,0.177586,0.,0.,0.,0.0500377,0.,0.,0.,0.],
[0.,0.,0.,0.,0.0588906,0.,0.,0.,0.,0.],
[0.,0.,0.,0.0493811,0.,0.,0.,0.,0.,0.],
[0.0397341,0.,0.0399249,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.]], np.float)

pcolor(data)
colorbar()
yticks(arange(0.5,10.5),range(0,10))
xticks(arange(0.5,10.5),range(0,10))
#show()
savefig('/home/mydir/foo.eps',figsize=(4,4),dpi=100)

这个python代码绘制为:

python中的DFT图与pcolor

现在这是我的问题/问题列表:我喜欢python如何绘制这个并且想使用它但是......

  1. 如何让所有代表“0”的“蓝色”消失,就像在我的数学图中一样?
  2. 如何旋转绘图以使左上角有亮红色的点?
  3. 我设置“dpi”的方式对吗?
  4. 有什么有用的参考资料可以用来加强我对 python 的热爱吗?

我查看了此处的其他问题和 numpy 的用户手册,但没有找到太多帮助。

我计划发布这些数据,而且我把所有的点点滴滴都做好是相当重要的!:)

编辑:

修改后的python代码和结果图!有人会建议对此进行哪些改进以使其值得出版?

from numpy import corrcoef, sum, log, arange, save
from numpy.random import rand

from pylab import *


data = np.array([[0.,0.,0.10664,0.,0.,0.,0.0412719,0.,0.,0.],
[0.,0.351894,0.,0.17873,0.,0.,0.,0.,0.,0.],
[0.10663,0.,0.178183,0.,0.,0.,0.0405148,0.,0.,0.],   
[0.,0.177586,0.,0.,0.,0.0500377,0.,0.,0.,0.],
[0.,0.,0.,0.,0.0588906,0.,0.,0.,0.,0.],
[0.,0.,0.,0.0493811,0.,0.,0.,0.,0.,0.],
[0.0397341,0.,0.0399249,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.],
[0.,0.,0.,0.,0.,0.,0.,0.,0.,0.]], np.float)

v1 = abs(data).max()
v2 = abs(data).min()
pcolor(data, cmap="binary")
colorbar()
#xlabel("X", fontsize=12, fontweight="bold")
#ylabel("Y", fontsize=12, fontweight="bold")
xticks(arange(0.5,10.5),range(0,10),fontsize=19)
yticks(arange(0.5,10.5),range(0,10),fontsize=19)
axis([0,7,0,7])
#show()


savefig('/home/mydir/Desktop/py_dft.eps',figsize=(4,4),dpi=600)

改进了 python 代码的输出 DFT 图

4

2 回答 2

9

以下将使您更接近您想要的:

import matplotlib.pyplot as plt

plt.pcolor(data, cmap=plt.cm.OrRd)
plt.yticks(np.arange(0.5,10.5),range(0,10))
plt.xticks(np.arange(0.5,10.5),range(0,10))
plt.colorbar()
plt.gca().invert_yaxis()
plt.gca().set_aspect('equal')
plt.show()

默认情况下可用的颜色图列表在这里。你需要一个一开始是白色的。

在此处输入图像描述

如果这些都不适合您的需求,您可以尝试生成自己的,首先查看LinearSegmentedColormap.

于 2013-04-13T15:11:59.743 回答
3

仅作记录,在 Mathematica 9.0 中:

GraphicsGrid@{{MatrixPlot[l, 
    ColorFunction -> (ColorData["TemperatureMap"][Rescale[#, {Min@l, Max@l}]] &), 
    ColorFunctionScaling -> False], BarLegend[{"TemperatureMap", {0, Max@l}}]}}

在此处输入图像描述

于 2013-04-13T16:02:34.530 回答