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

Question:

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:Anacondalibsite-packagesmatplotlibbackend_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/

Asked By: James Salamon

||

Answers:

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

Answered By: Stephan Kulla

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)

Answered By: Vincent Picouet