How to set stroke length in matplotlib contour outline

I am doing some contour plots in matplotlib, and the dash duration is too long. The dashed line also does not look very good. I would like to manually set the length of the dash. I can set the exact stroke length when I make a simple plot using plt.plot (), however I cannot figure out how to do the same with the outline plot.

I think the following code should work, but I get an 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 

Here is an example of what I'm trying to do, adapted from 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() 

Thanks!

+7
source share
2 answers

Nearly.

It:

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

If you put print c.get_dashes() , you would know (this is what I did).

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

The documentation for the documents has the following:

  • set_dashes (LS)

    alias for set_linestyle

  • set_linestyle (LS)

    Set linestyle for collection.

    ACCEPTS: ['solid | Dotted, dashdot, dashed | (offset, on-off-dash-seq)]

So, in [(0, (2.0, 2.0))] , 0 is the offset, and then the tuple is a repeating inclusion pattern.

+9
source

Although this is an old question, I had to deal with it, and the current answer is no longer valid. It is best to use plt.rcParams['lines.dashed_style'] = [2.0, 2.0] in front of your plot.

0
source

Source: https://habr.com/ru/post/925452/


All Articles