Search code examples
pythonmatplotlibinline-styles

python pyplot produce linestyle cycles with different dashes parameters


I'm writing a class in order to produce plot figures that matches my several needs ! In particular I'm focusing on the cycles (colors and linestyle) about the linestyle every pyplot users knows that there is 4 linestyle ('-', '--', '-.', ':') but there is an option .. "dashes=L,ES" (Line, Empty space) with the purpose to create lines with different spaces between to track ... how can I manage this in a linestyle loop ??

Well I know the sintax of tuple... but where I have to use it ?

here is the style that I have defined so far

def linestyles(self, style : str = 'ls1'):           
         linestyle = {}
         linestyle['ls1'] = ['-', '--', ':', '-.','-','--',':','-.'] 
         linestyle['ls10'] = ['-', '--', ':', '-.','-','--',':','-.','-','--'] 
         linestyle['llt8'] = ['-','--','-','--','-','--','-','--']
         linestyle['lp8'] = ['-',':','-',':','-',':','-',':']
         linestyle['llt10'] = ['-','--','-','--','-','--','-','--','-','--']
         linestyle['lp10'] = ['-',':','-',':','-',':','-',':','-',':']
         return linestyle[style]    

Where I have to specify the dashes ??

EDIT the problem is that I don't know how is the right way to cycle into them :

linestyle['ldash'] = ['(0, ()))','(0, (1, 10)))','(0, (1, 5)))','(0, (1, 1)))']

doesn't works if I insert this in my list

EDIT sorry you told me at the begin of your answer could not be string !! I solved !! consider this post close ! thanks a lot


Solution

  • The tuples are not meant to be strings, they should be python tuples. I.e. use (0, (1, 5)) instead of '(0, (1, 5)))'.

    In general, one way to specify a linestyle is via the linestyle argument. You may loop over a list,

    import matplotlib.pyplot as plt
    
    linestyles = ["--", (0,(5,2,5,5,1,4))]
    
    for i,ls in enumerate(linestyles):
        plt.plot([0,1],[i,i], linestyle=ls)
    
    plt.show()
    

    or to create a linestyle cycler,

    import matplotlib.pyplot as plt
    
    linestyles = ["--", (0,(5,2,5,5,1,4))]
    plt.rcParams["axes.prop_cycle"] += plt.cycler("linestyle", linestyles)
    
    for i in range(2):
        plt.plot([0,1],[i,i])
    
    plt.show()
    

    In both cases the resulting plot would look like this

    enter image description here

    Also refer to the linestyles example.