Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a poly line from point data #34

Closed
laserman781 opened this issue Jul 30, 2019 · 6 comments
Closed

Create a poly line from point data #34

laserman781 opened this issue Jul 30, 2019 · 6 comments
Assignees
Labels
mesh-creation Topics around creating a PyVista mesh

Comments

@laserman781
Copy link

I am trying to plot a polyline rather than simply plotting the points. I was able to plot a spline but this is not what I want. How can I accomplish this?

This is what I have written so far:

xyz = pd.read_csv('line_points.csv')
coords = xyz[['x', 'y', 'z']].values
poly = pv.PolyData(coords)
spline = pv.Spline(coords)
poly.plot(show_grid=False, point_size=3, color='red')
spline.plot(show_grid=False, point_size=3, color='white')
@banesullivan
Copy link
Member

Hi @laserman781 - this question is more appropriate for the pyvista-support repository, so I am moving it there shortly.

The code you have looks appropriate for plotting a PolyLine - using pv.Spline is the right choice. Unfortunately, there isn't much more I can offer without seeing visualizations of why the spline is not what you want or without having the csv file you read.

My guess is that the Spline looks wacky because the points in your csv file are not ordered - you'll likely need to make sure the points are ordered for the PolyLine/Spline to work.

@banesullivan banesullivan transferred this issue from pyvista/pyvista Jul 30, 2019
@laserman781
Copy link
Author

@banesullivan The spline comes out correctly with pv.Spline, although I want to avoid the interpolation of spline. Any suggestions?

@banesullivan
Copy link
Member

banesullivan commented Jul 30, 2019

Ah yes, the Spline is parametric and interpolates the points which might not be desired.

Try this solution with lines_from_points():

import pyvista as pv
import numpy as np
pv.set_plot_theme('doc')

def make_points():
    """Helper to make XYZ points"""
    theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
    z = np.linspace(-2, 2, 100)
    r = z**2 + 1
    x = r * np.sin(theta)
    y = r * np.cos(theta)
    return np.column_stack((x, y, z))


def lines_from_points(points):
    """Given an array of points, make a line set"""
    poly = pv.PolyData()
    poly.points = points
    cells = np.full((len(points)-1, 3), 2, dtype=np.int)
    cells[:, 1] = np.arange(0, len(points)-1, dtype=np.int)
    cells[:, 2] = np.arange(1, len(points), dtype=np.int)
    poly.lines = cells
    return poly

points = make_points()

line = lines_from_points(points)

p = pv.Plotter()
p.add_mesh(points, color='red')
p.add_mesh(line, color='blue',)
p.show()

download

@banesullivan
Copy link
Member

Also, note that a very similar function exists in PyVista: https://docs.pyvista.org/utilities/utilities.html#pyvista.lines_from_points

However, it requires the points array to be explicit about which lines make each segment - if the points are already ordered, then you can probably stick with the method I shared in the post above.

@banesullivan banesullivan added the mesh-creation Topics around creating a PyVista mesh label Jul 30, 2019
@laserman781
Copy link
Author

@banesullivan This is excellent information thank you!

@banesullivan
Copy link
Member

I'm assuming this solution worked? If so, would you please close the issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mesh-creation Topics around creating a PyVista mesh
Projects
None yet
Development

No branches or pull requests

2 participants