我在 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
这是我尝试做的示例,改编自 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()
谢谢!
最佳答案
差不多。
它是:
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是偏移量,然后元组就是开关重复模式。
关于python - 如何在 matplotlib 等高线图中设置破折号长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12434426/