Ejemplo n.º 1
0
def simple_plot(request):
    
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    from matplotlib.dates import DateFormatter
    c = Country.objects.all().order_by('?')[:1].get()
    res=[]
    year=[]
    aids = Aid.objects.filter(pcountry=c)
    for aid in aids:
        res.append(aid.dollars)
        year.append(aid.datesigned)
    fig=Figure(facecolor="#f4faf6")
    fig.set_figsize_inches( ( 4.50 ,3.472) )
    ax=fig.add_subplot(111)
    ax.set_axis_bgcolor("#f4faf6")
    x=[]
    y=[]
    
    
    ax.plot_date(year, res, '.-')
    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    ax.set_xlabel('date received', alpha=0.5)
    ax.set_ylabel('amount in Dollars', alpha=0.5)
    ax.set_title('Total amount of USAID tenders for work in '+c.country)



    fig.autofmt_xdate()
    

    canvas=FigureCanvas(fig)
    response=HttpResponse(content_type='image/png')
    canvas.print_png(response)
    return response
Ejemplo n.º 2
0
 def on_paint_request(self, printer):
     from matplotlib.figure import Figure
     from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
     rect = printer.pageRect( QtGui.QPrinter.Inch )
     dpi = printer.resolution()
     fig = Figure( facecolor='#ffffff')
     fig.set_figsize_inches( (rect.width(),rect.height()) )
     fig.set_dpi( dpi )
     self._value.plot_on_figure( fig )
     canvas = FigureCanvas(fig)
     canvas.render( printer )
Ejemplo n.º 3
0
 def on_paint_request(self, printer):
     from matplotlib.figure import Figure
     from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
     rect = printer.pageRect(QtGui.QPrinter.Inch)
     dpi = printer.resolution()
     fig = Figure(facecolor='#ffffff')
     fig.set_figsize_inches((rect.width(), rect.height()))
     fig.set_dpi(dpi)
     self._value.plot_on_figure(fig)
     canvas = FigureCanvas(fig)
     canvas.render(printer)
Ejemplo n.º 4
0
class PlotPanel(wx.Panel):
    """
    The PlotPanel has a Figure and a Canvas. OnSize events simply set a 
    flag, and the actually redrawing of the
    figure is triggered by an Idle event.
    """
    def __init__(self, parent, id = -1, color = None,\
        dpi = None, style = wx.NO_FULL_REPAINT_ON_RESIZE, **kwargs):
        wx.Panel.__init__(self, parent, id=id, style=style, **kwargs)
        self.figure = Figure(None, dpi)
        self.canvas = NoRepaintCanvas(self, -1, self.figure)
        self.SetColor(color)
        self.Bind(wx.EVT_IDLE, self._onIdle)
        self.Bind(wx.EVT_SIZE, self._onSize)
        self._resizeflag = True
        self._SetSize()

    def SetColor(self, rgbtuple):
        """Set figure and canvas colours to be the same"""
        if not rgbtuple:
            rgbtuple = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE).Get()
        col = [c / 255.0 for c in rgbtuple]
        self.figure.set_facecolor(col)
        self.figure.set_edgecolor(col)
        self.canvas.SetBackgroundColour(wx.Colour(*rgbtuple))

    def _onSize(self, event):
        self._resizeflag = True

    def _onIdle(self, evt):
        if self._resizeflag:
            self._resizeflag = False
            self._SetSize()
            self.draw()

    def _SetSize(self, pixels=None):
        """
        This method can be called to force the Plot to be a desired size, which defaults to
        the ClientSize of the panel
        """
        if not pixels:
            pixels = self.GetClientSize()
        self.canvas.SetSize(pixels)
        self.figure.set_figsize_inches(pixels[0] / self.figure.get_dpi(),
                                       pixels[1] / self.figure.get_dpi())

    def draw(self):
        """Where the actual drawing happens"""
        pass
Ejemplo n.º 5
0
cubeTexture.InterpolateOn()
cubeTexture.SetInput(imflip.GetOutput())
cubeActor.SetTexture(cubeTexture)

ren = vtkRenderer()
ren.AddActor(cubeActor)

win = plugInManager['VtkWindow'].winManager.newWindow()
win.vtkWidget.GetRenderWindow().AddRenderer(ren)

# Now create our plot
fig = Figure()
canvas = FigureCanvasAgg(fig)
ax = fig.add_subplot(111)
ax.grid(True)
ax.set_xlabel('Hello from VTK!', size=16)
ax.bar(xrange(10), p.rand(10))

# Powers of 2 image to be clean
w,h = 1024, 1024
dpi = canvas.figure.get_dpi()
fig.set_figsize_inches(w / dpi, h / dpi)
canvas.draw() # force a draw

# This is where we tell the image importer about the mpl image
extent = (0, w - 1, 0, h - 1, 0, 0)
importer.SetWholeExtent(extent)
importer.SetDataExtent(extent)
importer.SetImportVoidPointer(canvas.buffer_rgba(0,0), 1)
importer.Update()
Ejemplo n.º 6
0
# longitudes go from 20 to 380.
etopo = nx.array(load('etopo20data.gz'),'d')
lons = nx.array(load('etopo20lons.gz'),'d')
lats = nx.array(load('etopo20lats.gz'),'d')
# create figure.
fig = Figure()
canvas = FigureCanvas(fig)
# create axes instance, leaving room for colorbar at bottom.
ax = fig.add_axes([0.125,0.175,0.75,0.75])
# create Basemap instance for Robinson projection.
# set 'ax' keyword so pylab won't be imported.
m = Basemap(projection='robin',lon_0=0.5*(lons[0]+lons[-1]),ax=ax)
# reset figure size to have same aspect ratio as map.
# fig will be 8 inches wide.
# (don't use createfigure, since that imports pylab).
fig.set_figsize_inches((8,m.aspect*8.))
# make filled contour plot.
x, y = m(*meshgrid(lons, lats))
cs = m.contourf(x,y,etopo,30,cmap=cm.jet)
# draw coastlines.
m.drawcoastlines()
# draw a line around the map region.
m.drawmapboundary()
# draw parallels and meridians.
m.drawparallels(nx.arange(-60.,90.,30.),labels=[1,0,0,0],fontsize=10)
m.drawmeridians(nx.arange(0.,420.,60.),labels=[0,0,0,1],fontsize=10)
# add a title.
ax.set_title('Robinson Projection')
# add a colorbar.
cax = fig.add_axes([0.125, 0.05, 0.75, 0.05],frameon=False)
fig.colorbar(cs, cax=cax, tickfmt='%d', orientation='horizontal',clabels=cs.levels[::3]) 
Ejemplo n.º 7
0
def meshWithData2(**kwargs):
    import pyvista
    import vtk

    import matplotlib
    matplotlib.use('Agg')
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_agg import FigureCanvasAgg
    fig = Figure()
    canvas = FigureCanvasAgg(fig)
    ax = fig.add_subplot(111)
    ax.grid(True)
    ax.set_xlabel('Hello from VTK!', size=16)
    ax.bar(xrange(10), p.rand(10))
    # The pyvistamageImporter will treat a python string as a void pointer
    importer = pyvistamageImport()
    importer.SetDataScalarTypeToUnsignedChar()
    importer.SetNumberOfScalarComponents(4)

    # It's upside-down when loaded, so add a flip filter
    imflip = pyvistamageFlip()
    imflip.SetInput(importer.GetOutput())
    imflip.SetFilteredAxis(1)

    # Map the plot as a texture on a cube
    cube = vtkCubeSource()

    cubeMapper = vtkPolyDataMapper()
    cubeMapper.SetInput(cube.GetOutput())

    cubeActor = vtkActor()
    cubeActor.SetMapper(cubeMapper)

    # Create a texture based off of the image
    cubeTexture = vtkTexture()
    cubeTexture.InterpolateOn()
    cubeTexture.SetInput(imflip.GetOutput())
    cubeActor.SetTexture(cubeTexture)

    ren = vtkRenderer()
    ren.AddActor(cubeActor)

    renWin = vtkRenderWindow()
    renWin.AddRenderer(ren)

    iren = vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)
    # Powers of 2 image to be clean
    w, h = 1024, 1024
    dpi = canvas.figure.get_dpi()
    fig.set_figsize_inches(w / dpi, h / dpi)
    canvas.draw()  # force a draw

    # This is where we tell the image importer about the mpl image
    extent = (0, w - 1, 0, h - 1, 0, 0)
    importer.SetWholeExtent(extent)
    importer.SetDataExtent(extent)
    importer.SetImportVoidPointer(canvas.buffer_rgba(0, 0), 1)
    importer.Update()

    iren.Initialize()
    iren.Start()
matplotlib.use('Agg')

from mpl_toolkits.basemap import Basemap
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

if __name__ == "__main__":
    # Create the basemap
    m = Basemap(llcrnrlon=1, \
                llcrnrlat=40.6, \
                urcrnrlon=8.8, \
                urcrnrlat = 49.6, \
                resolution = 'l', \
                projection = 'tmerc', \
                       lon_0 = 4.9, \
                       lat_0 = 45.1)

    # Set Up the Canvas
    fig = Figure()
    canvas = FigureCanvas(fig)
    m.ax = fig.add_axes([0, 0, 1, 1])
    fig.set_figsize_inches((8 / m.aspect, 8.))

    # Draw
    m.drawcoastlines(color='gray')
    m.drawcountries(color='gray')
    m.fillcontinents(color='beige')
    x, y = m(lons, lats)
    m.plot(x, y, 'bo')
    #canvas.print_figure('map.png', dpi=100)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Now create our plot
fig = Figure()
canvas = FigureCanvasAgg(fig)
ax = fig.add_subplot(111)
ax.grid(True)
ax.set_xlabel('Hello from VTK!', size=16)
ax.bar(xrange(10), p.rand(10))

# Powers of 2 image to be clean
w,h = 1024, 1024
dpi = canvas.figure.get_dpi()
fig.set_figsize_inches(w / dpi, h / dpi)
canvas.draw() # force a draw

# This is where we tell the image importer about the mpl image
extent = (0, w - 1, 0, h - 1, 0, 0)
importer.SetWholeExtent(extent)
importer.SetDataExtent(extent)
importer.SetImportVoidPointer(canvas.buffer_rgba(0,0), 1)
importer.Update()

iren.Initialize()
iren.Start()

# <markdowncell>

# To have the plot be a billboard:
matplotlib.use('Agg')

from mpl_toolkits.basemap import Basemap
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure

if __name__ == "__main__":
	# Create the basemap
	m = Basemap(llcrnrlon=1, \
        	    llcrnrlat=40.6, \
	            urcrnrlon=8.8, \
	            urcrnrlat = 49.6, \
	            resolution = 'l', \
	            projection = 'tmerc', \
                    lon_0 = 4.9, \
                    lat_0 = 45.1)

	# Set Up the Canvas
	fig = Figure()
	canvas = FigureCanvas(fig)
	m.ax = fig.add_axes([0, 0, 1, 1])
	fig.set_figsize_inches((8/m.aspect, 8.))

	# Draw
	m.drawcoastlines(color='gray')
	m.drawcountries(color='gray')
	m.fillcontinents(color='beige')
	x, y = m(lons, lats)
	m.plot(x, y, 'bo')
	#canvas.print_figure('map.png', dpi=100)