0

I want to show image created by matplotlib dynamically in a panel. The server side code is this:

...
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
...
def lineCurve2D(request):
    x = np.linspace(-10, 10, 50)
    y = np.linspace(-10, 10, 50)
    x,y = np.meshgrid(x, y)
    z = x**2 + 3*y**2 + x*y - 5*x + 3*y
    fig = Figure()
    plt.contour(x, y, z)
    canvas = FigureCanvas(fig)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)
    return response

I should say, that if I do it in python shell and instead use plt.show() then I see a nice image. Next, on the client side my code looks like this:

function lineCurve2D(me){
    Ext.Ajax.request({
    url:'/myapp/lineCurve2D',
    method:'GET',
    success:function(res,req){
        var plot=me.down('#LineCurve2D'); // it is a fieldset inside my panel
        plot.items.each(function(el){
            el.destroy(); // I destroy what was previously added 
        });
        plot.add({xtype:'panel',layout:'fit',html:'<img src="data:image/png;base64,'+res.responseText+'"/>'}) // main problem
    }
    });
}

This is the function that triggers django to return an image. On return it creates image inside my panel. However, what I see now is an "empty" image, as if its source is incorrect.

Jacobian
  • 10,122
  • 29
  • 128
  • 221
  • I think you want to pass a `StringIO` object to `print_png` and then launder _that_ through the `HttpResponse` – tacaswell Nov 16 '14 at 19:59
  • Apparently, I am wrong, see http://stackoverflow.com/questions/1874642/how-to-use-matplotlib-in-django/1874740#1874740 – tacaswell Nov 16 '14 at 20:02
  • I really don't understand what of the things I said fixed anything ;) Can you please answer your own question explaining it. – tacaswell Nov 16 '14 at 20:10
  • @tcaswell "I think you want to pass a StringIO object to print_png and then launder that through the HttpResponse" - that is how I did with minor corrections. But at this point I;m struggling with other tiny problems – Jacobian Nov 16 '14 at 20:14

0 Answers0