如何在Matplotlib等高线图中设置破折号长度 [英] How can I set the dash length in a matplotlib contour plot

查看:69
本文介绍了如何在Matplotlib等高线图中设置破折号长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making some contour plots in matplotlib and the length of the dashes are too long. The dotted line also doesn't look good. I'd like to manually set the length of the dash. I can set the exact dash length when I'm making a simple plot using plt.plot(), however I cannot figure out how to do the same thing with a contour plot.

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

I think that the following code should work, but I get the error:

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示例改编而成的:

Here is a sample of what I'm trying to do, adapted from the MPL examples:

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(),那么您会发现的(这就是我所做的).

If you had put a print c.get_dashes() there, you would have found out (it's what I did).

也许线条样式的定义有些变化,而您是在一个较旧的示例中工作的.

Perhaps the definition of the line style has changed a bit, and you were working from an older example.

收藏文档有这样的说法:

  • set_dashes(ls)

  • set_dashes(ls)

set_linestyle 的别名

alias for set_linestyle

set_linestyle(ls)

set_linestyle(ls)

设置集合的线型.

接受:[稳定" |破折号",破折号",虚线" |(offset, on-off-dash-seq) ]

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

所以在[(0, (2.0, 2.0))]中,0是偏移量,然后元组就是开关重复模式.

So in [(0, (2.0, 2.0))], 0 is the offset, and then the tuple is the on-off repeating pattern.

这篇关于如何在Matplotlib等高线图中设置破折号长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆