Python plot legend is not draggble. gives an error
Question: I have data and I am trying for produce the plot. It does produce plot but
legend is not draggable. It gives me an error mentioned below.

    
    
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib
    import ast
    ...................
    ...................
    .
    .
    .
    .
    .   
    
    fig, ax = plt.subplots()
    
    for f in datafile:
        plt.plot(f[0],f[1])
    
    
    
    
    plt.tick_params(axis='both', which='major', labelsize=15)
    plt.legend(datanames)
    plt.legend(loc='best', numpoints=1)
    plt.legend().draggable()    
    
    
    plt.grid()  
    plt.show()
    

I am trying plot and it gives me an error

    
    
    AttributeError: 'NoneType' object has no attribute 'draggable'
    

How can it be fixed? Thanks.


Answer: Every time you call `plt.legend` it creates a _new_ legend (discarding the
old). None of you artists have a label, so no legend gets created and
`plt.legend` returns `None`. Just do it like so:

    
    
    legend = plt.legend(datanames, loc='best', numpoints=1)
    legend.draggable() 
    

