Subject: [Viper-dev] nicer 1d plotting

From: Dag Lindbo <[email-masked]>
Date: Tue, 15 Apr 2008 18:47:59 +0200

Here is an updated version of the progrm I posted earlier. (The other 
original one would mysteriously choose the wrong axis limits sometimes).
/Dag
import vtk
from dolfin import *

# Simple x-y plotting using VTK. For use with DOLFIN
# 
# Dag Lindbo, [email-masked], 2008-04-15

def plot_func_1D(filename,linespec):

    # extract mesh vertices and function value at vertices
    u = Function(filename)
    mesh = u.mesh()
    x = mesh.coordinates()
    y = x.copy()
    u.interpolate(y)
    
    # plot
    plot_xy(x,y,linespec)

def plot_xy(x,y,linespec):
    
    xVal = vtk.vtkFloatArray()
    yVal = vtk.vtkFloatArray()
    
    for i in range(len(x)):
        xVal.InsertNextTuple1(x[i])
        yVal.InsertNextTuple1(y[i])

    curve = vtk.vtkRectilinearGrid()   
    curve.SetDimensions(len(x), 1, 1)
    curve.SetXCoordinates(xVal)
    curve.GetPointData().SetScalars(yVal)

    # Set up the xyplot actor
    xyplot = vtk.vtkXYPlotActor()

    text_prop = xyplot.GetTitleTextProperty()
    text_prop.SetColor(1.0, 1.0, 1.0)
    text_prop.SetFontFamilyToArial()
    
    xyplot.AddInput(curve)
    xyplot.GetProperty().SetColor(1,1,1)
    xyplot.SetBorder(10)
    xyplot.GetPositionCoordinate().SetValue(0.0, 0.0, 0)
    xyplot.GetPosition2Coordinate().SetValue(1.0, 1.0, 0)
    xyplot.GetProperty().SetLineWidth(1)
    xyplot.GetProperty().SetPointSize(5)
    xyplot.SetPlotColor(0,1,0,0)
    xyplot.PlotPointsOff()
    xyplot.PlotLinesOff()
    xyplot.SetXTitle("x")
    xyplot.SetYTitle("u(x)")
    xyplot.SetXValuesToValue()
    xyplot.SetAxisTitleTextProperty(text_prop)
    xyplot.SetAxisLabelTextProperty(text_prop)
    xyplot.SetTitleTextProperty(text_prop)
    xyplot.SetDataObjectXComponent(0,0)
    xyplot.SetDataObjectYComponent(0,1)
    
    # determine line style
    if linespec == ".":
        xyplot.PlotCurvePointsOn()
        xyplot.SetPlotPoints(0,1)
    elif linespec == "-":
        xyplot.PlotCurveLinesOn()
    elif linespec == ".-" or linespec == "-.":
        xyplot.PlotCurvePointsOn()
        xyplot.PlotCurveLinesOn()
        xyplot.SetPlotPoints(0,1)
    else:
        print "Invalid line spec. Exiting"
        exit(0)
    
    # set up the renderer
    ren = vtk.vtkRenderer()
    ren.SetBackground(0.2,0.2,0.2)
    
    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer(ren)
    renWin.SetSize(800,600)

    ren.AddActor2D(xyplot)
    
    # render
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)
    iren.Initialize()
    renWin.Render()
    iren.Start()
    
file = "poisson.xml"
plot_func_1D(file,"-.")