Skip to content Skip to sidebar Skip to footer

How To Display Several Matplotlib Plots In One Django Template Page

I am working on a small app with Django framework. In one of my templates, I have a for loop in which I call an image of a matplotlib plot. But Django is mixing all of my data : so

Solution 1:

I find a suitable solution by myself if anyone here is interested, I am using the RLock() function.

from threading import RLock
verrou = RLock()

def GraphsTS(request, h):
    with verrou:
        h = list(map(float, TS.split("-"))) # h is passed as text and transform to a list
        f = plt.figure(figsize=(5,2))
        plt.title('Title')
        plt.xlabel('Date')
        plt.ylabel('YLABEL')
        plt.xticks(rotation='vertical')
        bar1 = plt.plot(h, color='Green',alpha=0.65)

        canvas = FigureCanvasAgg(f)    
        response = HttpResponse(content_type='image/jpg')
        canvas.print_jpg(response)
        matplotlib.pyplot.close(f)
        return response

Post a Comment for "How To Display Several Matplotlib Plots In One Django Template Page"