簡體   English   中英

使用 CSV 繪制平行坐標

[英]Using CSV to plot parallel coordinates

我正在嘗試使用來自 csv 文件的數據繪制平行坐標圖。 csv 文件包含四列。 數據的一個例子是:

A, B, C, D
1, 2, 4, 8
5, 6, 1, 5
6, 5, 5, 10

我收到錯誤No valid input arrays specified 我不知道我做錯了什么。 這是我到目前為止的代碼。

from vtk import *
import csv

FILENAME_PARA_COORD = "htc_2.csv"

# Load the paracoordinates table from CSV file
csv_source = vtkDelimitedTextReader()
csv_source.SetFieldDelimiterCharacters(",")
csv_source.DetectNumericColumnsOn()
csv_source.SetHaveHeaders(True)
csv_source.SetFileName(FILENAME_PARA_COORD)

# Set up the parallel coordinates Representation to be used in this view
rep = vtk.vtkParallelCoordinatesRepresentation()

# Plug your reader in here for your own data
if vtk.VTK_MAJOR_VERSION <= 5:
   rep.SetInput(csv_source.GetOutput())

else:
   rep.SetInputConnection(csv_source.GetOutputPort())

# List all of the attribute arrays you want plotted in parallel coordinates
rep.SetInputArrayToProcess(0, 0, 0, 0, 'A')
rep.SetInputArrayToProcess(1, 0, 0, 0, 'B')
rep.SetInputArrayToProcess(2, 0, 0, 0, 'C')
rep.SetInputArrayToProcess(3, 0, 0, 0, 'D')

rep.SetUseCurves(0) # set to 1 to use smooth curves
rep.SetLineOpacity(0.5)

#Set up the Parallel Coordinates View and hook in the Representation
view = vtk.vtkParallelCoordinatesView()
view.SetRepresentation(rep)

# Inspect Mode determines whether your interactions manipulate the axes or
# select data
view.SetInspectMode(1) # VTK_INSPECT_SELECT_DATA =1

# Brush Mode determines the type of interaction you perform to select data
view.SetBrushModeToLasso()


# Brush Operator determines how each new selection interaction changes
# selected lines

view.SetBrushOperatorToReplace()

def ToggleInspectors(obj, event):
    # Define the callback routine which toggles between "Inspect Modes"
    if (view.GetInspectMode() == 0):
        view.SetInspectMode(1)
    else:
        view.SetInpsectMode(0)

# Hook up the callback to togle between inspect modes
# (manip axes & select data)
view.GetInteractor().AddObserver("UserEvent", ToggleInspectors)

# Set up render window
view.GetRenderWindow().SetSize(600, 300)
view.ResetCamera()
view.Render()

# Start interaction event loop
view.GetInteractor().Start()

一種方法是為每一列創建列表,然后將其轉換為數組並繪制:

例如:

A = ['1','2','3','4']     #list of strings
A = [int(i) for i in A]   #to 
O/p A = [1,2,3,4]         #list of integers

然后你可以繪制平行坐標。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM