用于出版质量图的Python Pylab pcolor选项

时间:2013-04-13 13:40:04

标签: python numpy matplotlib plot wolfram-mathematica

我正在尝试使用python中的pcolor制作DFT(离散傅立叶变换)图。我以前一直在使用Mathematica 8.0来做这个,但我发现mathematica 8.0中的colorbar与我试图表示的数据有一对一的不良关联。例如,这是我正在绘制的数据:

[[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绘制时,结果如下:

DFT plot with mathematica 8.0

颜色条已经关闭,我想我想用python来绘制它。 我的python代码(我从here劫持)是:

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代码绘制为:

DFT plot in python with pcolor

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

  1. 如何使所有代表“0”的“蓝色”像我在mathematica情节中那样消失?
  2. 如何旋转绘图以使左上角有一个鲜红色的斑点?
  3. 我设置“dpi”的方式是正确的吗?
  4. 我应该用什么有用的参考来增强我对python的热爱?
  5. 我已经在这里查看了其他问题以及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)
    

    Improved python code's output DFT plot

2 个答案:

答案 0 :(得分: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()

默认情况下,可用的色彩映射列表为here。你需要一个白色的。

enter image description here

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

答案 1 :(得分:3)

仅供记录,在Mathematica 9.0中:

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

enter image description here