如何在matplotlib等高线图中设置短划线长度

Dan*_*ein 7 python matplotlib

我在matplotlib中制作了一些等高线图,破折号的长度太长了.虚线也不好看.我想手动设置短划线的长度.当我使用plt.plot()制作一个简单的绘图时,我可以设置精确的短划线长度,但是我无法弄清楚如何用等高线图做同样的事情.

我认为以下代码应该可以工作,但我收到错误:

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
Run Code Online (Sandbox Code Playgroud)

以下是我正在尝试做的一个示例,改编自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()
Run Code Online (Sandbox Code Playgroud)

谢谢!

小智 9

几乎.

它的:

for c in CS.collections:
    c.set_dashes([(0, (2.0, 2.0))])
Run Code Online (Sandbox Code Playgroud)

如果你把它放在print c.get_dashes()那里,你会发现(这就是我所做的).

也许线条样式的定义有所改变,而你正在使用一个较旧的例子.

集合文档有这样一段话:

  • set_dashes(LS)

    set_linestyle的别名

  • set_linestyle(LS)

    设置集合的线型.

    ACCEPTS:['solid'| '虚线','dashdot','点'| (偏移,开 - 关 - 破折号)]

因此[(0, (2.0, 2.0))],0是偏移量,然后元组是开关重复模式.