James Salamon
James Salamon

Reputation: 1422

plt.savefig(): ValueError: All values in the dash list must be positive

Running the code at the below link results in the error. As far as having something to do with the image, I don't know what the 'dash list' is.

matplotlib.pyplot as plt
...
plt.savefig('tutorial10.png',dpi=300)

Segment of the returned error:

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-21-edce1701d7a3> in <module>()
    60     ax.add_collection(lines)
    61 
--> 62 plt.savefig('tutorial10.png',dpi=300)
    63 
    64 plt.show()

    ...

    C:\Anaconda\lib\site-packages\matplotlib\backend_bases.pyc in set_dashes(self, dash_offset, dash_list)
    902             dl = np.asarray(dash_list)
    903             if np.any(dl <= 0.0):
--> 904                 raise ValueError("All values in the dash list must be positive")
    905         self._dashes = dash_offset, dash_list
    906 

http://www.geophysique.be/2013/02/12/matplotlib-basemap-tutorial-10-shapefiles-unleached-continued/

Upvotes: 3

Views: 4138

Answers (3)

roural
roural

Reputation: 21

I think I solved the issue.

My Matplotlib version is 3.7.2 but I had the same issue with the 3.6.0. Here is what I did:

1.Go to C:\Users\USER_NAME\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib

2.Open for editing: lines.py

3.Look for function _scale_dashes: Initial function looks like:

def _scale_dashes(offset, dashes, lw):
    if not mpl.rcParams['lines.scale_dashes']:
        return offset, dashes
    scaled_offset = offset * lw
    scaled_dashes = ([x * lw if x is not None else None for x in dashes]
                     if dashes is not None else None)
    return scaled_offset, scaled_dashes

then I modified the first if statement as follow:

def _scale_dashes(offset, dashes, lw):
    if not mpl.rcParams['lines.scale_dashes'] or lw == 0:
        return offset, dashes
    scaled_offset = offset * lw
    scaled_dashes = ([x * lw if x is not None else None for x in dashes]
                     if dashes is not None else None)
    return scaled_offset, scaled_dashes

Indeed the issue was the output of _scale_dashes function when lw = 0. In some cases the output would look like: (0, [0, 0]) which would return the error

 raise ValueError(
ValueError: At least one value in the dash list must be positive

because the second element of the tuple is a list with 0 values, and these 0 values come from the multiplication by lw (which is 0) in _scale_dashes function

Upvotes: 1

Vincent Picouet
Vincent Picouet

Reputation: 51

In matplotlib 3.6.2, this error can also appear when plotting a dashed line with a null line width:plt.plot(1, 1, ls='--', lw=0)

Upvotes: 1

Stephan Kulla
Stephan Kulla

Reputation: 5067

In the code, you linked, there are the following lines:

m.drawparallels(np.arange(y1,y2,2.),labels=[1,0,0,0],color='black',dashes=[1,0],labelstyle='+/-',linewidth=0.2) # draw parallels
m.drawmeridians(np.arange(x1,x2,2.),labels=[0,0,0,1],color='black',dashes=[1,0],labelstyle='+/-',linewidth=0.2)

In those lines the argument dashes is set to [1,0]. Concerning to your error message, all values in the array dashes must be strictly positive. That's why you get the exception (your array dashes contains zero).

Upvotes: 4

Related Questions