VTK绘图Demo01

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()

在这里插入图片描述

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值