import vtk
from math import *
# Set up a 2D scene, add an XY chart to it
colors = vtk.vtkNamedColors()
view = vtk.vtkContextView()
view.GetRenderWindow().SetSize(1280, 1024)
view.GetRenderWindow().SetWindowName("ChartMatrix")
matrix = vtk.vtkChartMatrix()
view.GetScene().AddItem(matrix)
matrix.SetSize(vtk.vtkVector2i(2, 2))
matrix.SetGutter(vtk.vtkVector2f(30.0, 30.0))
# Create a table with some points in it
table = vtk.vtkTable()
arrX = vtk.vtkFloatArray()
arrX.SetName("X Axis")
table.AddColumn(arrX)
arrC = vtk.vtkFloatArray()
arrC.SetName("Cosine")
table.AddColumn(arrC)
arrS = vtk.vtkFloatArray()
arrS.SetName("Sine")
table.AddColumn(arrS)
arrS2 = vtk.vtkFloatArray()
arrS2.SetName("Sine2")
table.AddColumn(arrS2)
tangent = vtk.vtkFloatArray()
tangent.SetName("Tangent")
table.AddColumn(tangent)
numPoints = 42
inc = 7.5 / (numPoints - 1)
table.SetNumberOfRows(numPoints)
for i in range(numPoints):
table.SetValue(i, 0, i * inc)
table.SetValue(i, 1, cos(i * inc))
table.SetValue(i, 2, sin(i * inc))
table.SetValue(i, 3, sin(i * inc) + 0.5)
table.SetValue(i, 4, tan(i * inc))
# Add multiple line plots, setting the colors etc
# lower left plot, a point chart
chart = matrix.GetChart(vtk.vtkVector2i(0, 0))
plot = chart.AddPlot(vtk.vtkChart.POINTS)
plot.SetInputData(table, 0, 1)
plot.SetMarkerStyle(vtk.vtkPlotPoints.DIAMOND)
plot.GetXAxis().GetGridPen().SetColorF(colors.GetColor3d("warm_grey"))
plot.GetYAxis().GetGridPen().SetColorF(colors.GetColor3d("warm_grey"))
plot.SetColor(colors.GetColor3ub("sea_green").GetRed(),
colors.GetColor3ub("sea_green").GetGreen(),
colors.GetColor3ub("sea_green").GetBlue(), 255)
# upper left plot, a point chart
chart = matrix.GetChart(vtk.vtkVector2i(0, 1))
plot = chart.AddPlot(vtk.vtkChart.POINTS)
plot.SetInputData(table, 0, 2)
plot.GetXAxis().GetGridPen().SetColorF(colors.GetColor3d("warm_grey"))
plot.GetYAxis().GetGridPen().SetColorF(colors.GetColor3d("warm_grey"))
plot.SetColor(colors.GetColor3ub("rose_madder").GetRed(),
colors.GetColor3ub("rose_madder").GetGreen(),
colors.GetColor3ub("rose_madder").GetBlue(), 255)
chart = matrix.GetChart(vtk.vtkVector2i(1, 0))
plot = chart.AddPlot(vtk.vtkChart.LINE)
plot.SetInputData(table, 0, 1)
#plot.GetLegend().SetVisible(True)
plot = chart.AddPlot(vtk.vtkChart.POINTS)
plot.SetInputData(table, 0, 3)
plot.GetXAxis().GetGridPen().SetColorF(colors.GetColor3d("warm_grey"))
plot.GetYAxis().GetGridPen().SetColorF(colors.GetColor3d("warm_grey"))
plot.SetColor(colors.GetColor3ub("rose_madder").GetRed(),
colors.GetColor3ub("rose_madder").GetGreen(),
colors.GetColor3ub("rose_madder").GetBlue(), 255)
chart = matrix.GetChart(vtk.vtkVector2i(1, 1))
plot = chart.AddPlot(vtk.vtkChart.BAR)
plot.SetInputData(table, 0, 4)
plot.GetXAxis().GetGridPen().SetColorF(colors.GetColor3d("warm_grey"))
plot.GetYAxis().GetGridPen().SetColorF(colors.GetColor3d("warm_grey"))
plot.SetColor(colors.GetColor3ub("rose_madder").GetRed(),
colors.GetColor3ub("rose_madder").GetGreen(),
colors.GetColor3ub("rose_madder").GetBlue(), 255)
# ...
# Add other plot configurations as necessary
# Finally render the scene and compare the image to a reference image
view.GetRenderer().SetBackground(colors.GetColor3d("white"))
view.GetRenderWindow().Render()
view.GetInteractor().Initialize()
view.GetInteractor().Start()
04-05
215
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-22
2405
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-02
6639
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)