python - 如何在 matplotlib 等高线图中设置破折号长度

标签 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

这是我尝试做的示例,改编自 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/

相关文章:

python - 如何让敌人跟随玩家? pygame

python - 带日期的 matplotlib

python - 如何在绘图标记上添加标签

python - 如何在代表相同标签的每个标记上方设置标题

list - 合并 2 个元组列表并在元组中添加非唯一值的 Pythonic 方法是什么?

python - 将列表拆分为 pandas DataFrame 中的多列

Python - 使用递归查找嵌套列表中的最大值和最小值之和

python - 将 spacy(python) 标记写入 excel 文件

python - 共享不同大小的子图轴的缩放比例(不共享轴)

python - 单击条形图中的条形可生成该条形图中值的散点图