2012-09-15 43 views
6

我正在matplotlib中创建一些轮廓图,并且短划线的长度过长。虚线也不太好看。我想手动设置短划线的长度。当我使用plt.plot()绘制一个简单的图时,我可以设置确切的短划线长度,但我无法弄清楚如何用等高线图做同样的事情。如何在matplotlib轮廓图中设置短划线长度

我认为下面的代码应该工作,但我得到的错误:

File "/Library/Python/2.7/site-packages/matplotlib-1.2.x-py2.7-macosx-10.8-intel.egg/matplotlib/backends/backend_macosx.py", line 80, in draw_path_collection 
    offset_position) 
TypeError: failed to obtain the offset and dashes from the linestyle 

这里是我想要做一个样品,改编自MPL例子:

import numpy as np 
import matplotlib.mlab as mlab 
import matplotlib.pyplot as plt 


delta = 0.025 
x = np.arange(-3.0, 3.0, delta) 
y = np.arange(-2.0, 2.0, delta) 
X, Y = np.meshgrid(x, y) 
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) 
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) 
# difference of Gaussians 
Z = 10.0 * (Z2 - Z1) 

plt.figure() 

CS = plt.contour(X, Y, Z, 6, colors='k',linestyles='dashed') 

for c in CS.collections: 
    c.set_dashes([2,2]) 

plt.show() 

谢谢!

回答

9

差不多。

它是:

for c in CS.collections: 
    c.set_dashes([(0, (2.0, 2.0))]) 

如果你已经把一个print c.get_dashes()那里,你会发现(这是我做了什么)。

也许线条样式的定义已经发生了一些变化,并且您正在从一个较旧的示例开始工作。

collections documentation有这样说:

  • set_dashes(ls)

    alias for set_linestyle

  • set_linestyle(ls)

    Set the linestyle(s) for the collection.

    ACCEPTS: [‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ | (offset, on-off-dash-seq) ]

所以在[(0, (2.0, 2.0))],0是偏移量,然后该元组是将开闭重复图案。

+0

非常感谢!我厌倦了(偏移量,(开,关))格式,但我并没有意识到我需要括号内的括号。我的情节现在看起来很棒。你让我今天很开心。谢谢,丹 – DanHickstein