Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the dash length in a matplotlib contour plot

Tags:

People also ask

How do I create a dashed line in matplotlib?

The syntax for this is given below:matplotlib. pyplot. plot(x, y, linestyle='dashed', marker=None, markersize=None,markeredgecolor=None ... ) In this above example, we use the markers in dashed line graph by using markers as argument and we also tune the marker according to your need.

How do you change the dotted line in Python?

You can directly specify the dashes length/space using the dashes=(length, interval space) argument inside the plot command.

What is the difference between contour and Contourf?

contour() and contourf() draw contour lines and filled contours, respectively. Except as noted, function signatures and return values are the same for both versions. contourf() differs from the MATLAB version in that it does not draw the polygon edges. To draw edges, add line contours with calls to contour() .


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

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()

Thanks!