2010-12-27 9 views
2

나는 django와 함께 matplotlib를 사용하고 있습니다. 나는 막 대형 차트를 만들려고합니다.matplotlib의 그림에 플롯을 추가하는 방법은 무엇입니까?

나는 cookbook을 따랐지만 회색 사각형 상자가 있습니다.

이제 다음 코드를 사용하고 있으며 제목과 축이 있습니다.

어떻게 그림에 막대 그래프를 추가 할 수 있습니까? 현재 축 안에는 실제 데이터가 없습니다. 내가 가진 내보기에서

from matplotlib.backends.backend_agg import FigureCanvasAgg 
from matplotlib.figure import Figure 
import matplotlib.pyplot as plt 

class Chart(object): 

    ## Creates a bar chart of the given data 
    @staticmethod 
    def bar(data): 
     figure = Figure(figsize=(6,6)) 
     ax = figure.add_axes([0.1, 0.1, 0.8, 0.8]) 
     labels = 'Frogs', 'Hogs', 'Dogs', 'Logs' 
     fracs = [15, 30, 45, 10] 
     explode=(0, 0.05, 0, 0) 
     plt.pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True) 
     figure.suptitle('Raining Hogs and Dogs', fontsize=14) 

     canvas = FigureCanvasAgg(figure) 

     return canvas 

:

여기 내 차트 코드의

canvas = Chart.bar(results) 

# turn the returned canvas into an HTTP response 
response=HttpResponse(content_type='image/png') 
canvas.print_png(response) 
return response 

답변

8
fig = Figure() 
    fig = Figure(facecolor='white', edgecolor='white') 
    ax = fig.add_subplot(1,1,1) 

    x = matplotlib.numpy.arange(0, len(dic.keys())) 

    ind = matplotlib.numpy.arange(len(dic.values())) 

    height = 0.8 
    ax.bar(ind, dic.values(), width, color=colors) 

    ax.set_xticks(ind + width/2.0) 
    ax.set_xticklabels(dic.keys()) 

    padding = 0.2 
    ax.set_xlim([x.min() - padding, x.max() + width + padding]) 

    canvas = FigureCanvas(fig) 
    response = django.http.HttpResponse(content_type='image/png') 
    canvas.print_png(response) 
    fig.savefig(filename) 

이 막대 그래프를 생성하고 이미지를 저장합니다. 그냥 함수를 호출해야합니다. 템플릿의 이미지를 엽니 다. 이 함수 (dic)에 사전을 전달했지만 목록을 전달할 수 있습니다, 당신에게 달려 있습니다.

이 경우 키는 x 축이고 값은 y 축입니다.

관련 문제