GithubHelp home page GithubHelp logo

Lines with cell data about pyvista-support HOT 4 CLOSED

pyvista avatar pyvista commented on July 1, 2024
Lines with cell data

from pyvista-support.

Comments (4)

banesullivan avatar banesullivan commented on July 1, 2024

You should have as many cells in the mesh for each segment as rows in the data frame so doing something like the following should work:

segs.cell_arrays['IDs'] = df['IDs'].values 

Maybe try something like this instead...

import pandas as pd
import numpy as np
import pyvista as pv

##########
# Make a sample dataframe
df = pd.DataFrame()
df['x'] = np.array([0.0, 1.0])
df['y'] = np.array([0.0, 0.0])
df['z'] = np.array([0.0, 0.0])
df['x2'] = np.array([1.0, 1.0])
df['y2'] = np.array([0.0, 1.0])
df['z2'] = np.array([0.0, 0.0])
df['IDs'] = np.array(['Line ID 1', 'Line ID 2'])
##########

weaved = np.empty((len(df) * 2, 3))
weaved[0::2] = df[['x', 'y', 'z']].values
weaved[1::2] = df[['x2', 'y2', 'z2']].values

def lines_from_points(points):
    """Generates line from weaved points."""
    n_points = len(points)
    n_lines = n_points // 2
    lines = np.c_[(2 * np.ones(n_lines, np.int),
                   np.arange(0, n_points-1, step=2),
                   np.arange(1, n_points+1, step=2))]
    poly = pv.PolyData()
    poly.points = points
    poly.lines = lines
    return poly

lines = lines_from_points(weaved)
lines.cell_arrays['IDs'] = df['IDs'].values
lines.plot()

download

from pyvista-support.

laserman781 avatar laserman781 commented on July 1, 2024

This solution works, although by using weave it causes all the lines to be interconnected when they are not...

from pyvista-support.

banesullivan avatar banesullivan commented on July 1, 2024

It shouldn't. For example:

import pandas as pd
import numpy as np
import pyvista as pv

##########
# Make a sample dataframe
df = pd.DataFrame()
df['x'] = np.array([0.0, 2.0])
df['y'] = np.array([0.0, 0.0])
df['z'] = np.array([0.0, 0.0])
df['x2'] = np.array([1.0, 2.0])
df['y2'] = np.array([0.0, 2.0])
df['z2'] = np.array([0.0, 0.0])
df['IDs'] = np.array(['Line ID 1', 'Line ID 2'])
##########

weaved = np.empty((len(df) * 2, 3))
weaved[0::2] = df[['x', 'y', 'z']].values
weaved[1::2] = df[['x2', 'y2', 'z2']].values

def lines_from_points(points):
    """Generates line from weaved points."""
    n_points = len(points)
    n_lines = n_points // 2
    lines = np.c_[(2 * np.ones(n_lines, np.int),
                   np.arange(0, n_points-1, step=2),
                   np.arange(1, n_points+1, step=2))]
    poly = pv.PolyData()
    poly.points = points
    poly.lines = lines
    return poly

lines = lines_from_points(weaved)
lines.cell_arrays['IDs'] = df['IDs'].values
lines.plot(line_width=5)

download

from pyvista-support.

laserman781 avatar laserman781 commented on July 1, 2024

Works great thank you!

from pyvista-support.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.