-
Notifications
You must be signed in to change notification settings - Fork 4
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
3D curves in a single plot #11
Comments
With this totally unoptimized snippet: import numpy as np
import pyvista as pv
import csv
from pyvista.utilities import lines_from_points
xfile = open('x_coordinates.csv', 'r')
xreader = csv.reader(xfile, delimiter=',')
x = []
for row in xreader:
x.append(row)
xfile.close()
yfile = open('y_coordinates.csv', 'r')
yreader = csv.reader(yfile, delimiter=',')
y = []
for row in yreader:
y.append(row)
yfile.close()
zfile = open('z_coordinates.csv', 'r')
zreader = csv.reader(zfile, delimiter=',')
z = []
for row in zreader:
z.append(row)
zfile.close()
plotter = pv.Plotter()
plotter.background_color = 'white'
for idx in range(len(x)):
cx = x[idx]
cy = y[idx]
cz = z[idx]
pts = np.column_stack((cx, cy, cz)).astype(np.float)
line = lines_from_points(pts)
plotter.add_mesh(line, show_edges=True)
plotter.show() Did you expect something similar? Also, I think it should be possible to append/create an |
Dear Guillaume,
Thank you very much for your quick response.
It should look like this.
Is it possible to add colours to these curves (Z height as a scalar field)
similar to streamlines and make it tube-like structure (like streamlines)?
Regards
Sunil
…On Tue, Jun 4, 2019 at 2:52 PM Guillaume Favelier ***@***.***> wrote:
With this totally unoptimized snippet:
import numpy as npimport pyvista as pvimport csvfrom pyvista.utilities import lines_from_points
xfile = open('x_coordinates.csv', 'r')
xreader = csv.reader(xfile, delimiter=',')
x = []for row in xreader:
x.append(row)
xfile.close()
yfile = open('y_coordinates.csv', 'r')
yreader = csv.reader(yfile, delimiter=',')
y = []for row in yreader:
y.append(row)
yfile.close()
zfile = open('z_coordinates.csv', 'r')
zreader = csv.reader(zfile, delimiter=',')
z = []for row in zreader:
z.append(row)
zfile.close()
plotter = pv.Plotter()
plotter.background_color = 'white'for idx in range(len(x)):
cx = x[idx]
cy = y[idx]
cz = z[idx]
pts = np.column_stack((cx, cy, cz)).astype(np.float)
line = lines_from_points(pts)
plotter.add_mesh(line, show_edges=True)
plotter.show()
I got the following:
[image: image]
<https://user-images.githubusercontent.com/18143289/58880247-cc963f00-86d7-11e9-8da5-7fa2cb0fa0bb.png>
Did you expect something similar?
Also, I think it should be possible to append/create an UnstructuredGrid
dataset and use only one call to add_mesh(). Note that I had to use
show_edges=True to actually visualize the result.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#11?email_source=notifications&email_token=AEB5O7UOZVWS7UBLJLK2SVLPYZQRHA5CNFSM4HS2PI5KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODW4OZSI#issuecomment-498658505>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AEB5O7QLXMX2UUQD5WH6ZQ3PYZQRHANCNFSM4HS2PI5A>
.
--
Sunil Kumar Yadav
Freie University Berlin
Berlin, Germany
Email ID: *sunil.yadav@fu-berlin.de <sunil@zedat.fu-berlin.de>*
Tel(Office) : +49-30-838-75860
Mob: +49-176-313-14991
|
I modified a bit the previous (unoptimized) snippet: ...
plotter = pv.Plotter()
plotter.background_color = 'white'
for idx in range(len(x)):
cx = x[idx]
cy = y[idx]
cz = z[idx]
pts = np.column_stack((cx, cy, cz)).astype(np.float)
lines = lines_from_points(pts)
lines.lines = lines.faces
tube = lines.tube(0.01).elevation()
plotter.add_mesh(tube, show_edges=False)
plotter.show() The main modifications are There is maybe a better/efficient way to achieve this, what do you think @pyvista/developers ? |
Dear Guillaume,
Thank you very much.
I was looking for an exactly similar structure.
I will try to optimize it.
Regards
Sunil
…On Tue, Jun 4, 2019 at 5:30 PM Guillaume Favelier ***@***.***> wrote:
I modified a bit the previous (unoptimized) snippet:
...
plotter = pv.Plotter()
plotter.background_color = 'white'for idx in range(len(x)):
cx = x[idx]
cy = y[idx]
cz = z[idx]
pts = np.column_stack((cx, cy, cz)).astype(np.float)
lines = lines_from_points(pts)
lines.lines = lines.faces
tube = lines.tube(0.01).elevation()
plotter.add_mesh(tube, show_edges=False)
plotter.show()
[image: image]
<https://user-images.githubusercontent.com/18143289/58891797-7633fb00-86ed-11e9-9d28-ccbed2797000.png>
The main modifications are lines.lines = lines.faces which basically
updates the lines of the PolyData required by the tube filter. The rest
is pretty straightforward, we apply the filters: first the tube with a
radius of 0.01 then the elevation filter to add a scalar field
corresponding to the Z height.
There is maybe a better/efficient way to achieve this, what do you think
@pyvista/developers <https://github.com/orgs/pyvista/teams/developers> ?
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#11?email_source=notifications&email_token=AEB5O7Q24BMTBLQEXORMAOLPY2DADA5CNFSM4HS2PI5KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODW46LGI#issuecomment-498722201>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AEB5O7TV32AVPEPRVKMMSZTPY2DADANCNFSM4HS2PI5A>
.
--
Sunil Kumar Yadav
Freie University Berlin
Berlin, Germany
Email ID: *sunil.yadav@fu-berlin.de <sunil@zedat.fu-berlin.de>*
Tel(Office) : +49-30-838-75860
Mob: +49-176-313-14991
|
Good start @GuillaumeFavelier! Here's a little bit of an improvement using splines, which leads to smoother plotting with no breaks along the line. I'm adding splines to import numpy as np
import pyvista as pv
x = np.loadtxt('x_coordinates.csv', delimiter=',')
y = np.loadtxt('y_coordinates.csv', delimiter=',')
z = np.loadtxt('z_coordinates.csv', delimiter=',')
def make_spline(points, n_points=None):
"""Create a spline from points
Parameters
----------
points : np.ndarray
Array of points to build a spline out of. Array must be 3D
and directionally ordered.
n_points : int, optional
Number of points to interpolate along the points array.
Returns
-------
spline : pyvista.PolyData
Line mesh of spline.
Examples
--------
Construct a spline
>>> import numpy as np
>>> import pyvista as pv
>>> 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)
>>> points = np.column_stack((x, y, z))
>>> spline = pv.Spline(points, 1000)
"""
import vtk
spline = vtk.vtkParametricSpline()
vtk_points = pv.vtk_points(points, False)
spline.SetPoints(vtk_points)
spline.GetPoints().Modified()
spline.Modified()
# get interpolation density
u_res = n_points
if u_res is None:
u_res = points.shape[0]
return pv.geometric_objects.surface_from_para(spline, u_res)
# convert points to splines
splines = []
for idx in range(x.shape[0]):
points = np.column_stack((x[idx], y[idx], z[idx]))
spline = make_spline(points)
# spline = pv.Spline(points) # in the next release
splines.append(spline)
# boring plot
pv.plot(splines)
# exciting plot with scalars
tubes = []
for spline in splines:
# add scalars here...
spline['scalars'] = np.arange(spline.n_points)
tubes.append(spline.tube(0.01))
pv.plot(tubes, smooth_shading=True) |
Dear Alex,
Thank you very much for this elegant solution.
The picture looks really good. However, I am not able to run this code.
I am getting the following error:
return pv.geometric_objects.surface_from_para(spline, u_res)
AttributeError: module 'pyvista.geometric_objects' has no attribute
'surface_from_para'
Please take a look.
Regards
Sunil
…On Wed, Jun 5, 2019 at 9:30 AM Alex Kaszynski ***@***.***> wrote:
Good start @GuillaumeFavelier <https://github.com/GuillaumeFavelier>!
Here's a little bit of an improvement using splines, which leads to
smoother plotting with no breaks along the line.
I'm adding splines to pyvista for the next release, so you won't need to
use make_spline and will instead use pv.Spline
import numpy as npimport pyvista as pv
x = np.loadtxt('x_coordinates.csv', delimiter=',')
y = np.loadtxt('y_coordinates.csv', delimiter=',')
z = np.loadtxt('z_coordinates.csv', delimiter=',')
def make_spline(points, n_points=None):
"""Create a spline from points Parameters ---------- points : np.ndarray Array of points to build a spline out of. Array must be 3D and directionally ordered. n_points : int, optional Number of points to interpolate along the points array. Returns ------- spline : pyvista.PolyData Line mesh of spline. Examples -------- Construct a spline >>> import numpy as np >>> import pyvista as pv >>> 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) >>> points = np.column_stack((x, y, z)) >>> spline = pv.Spline(points, 1000) """
import vtk
spline = vtk.vtkParametricSpline()
vtk_points = pv.vtk_points(points, False)
spline.SetPoints(vtk_points)
spline.GetPoints().Modified()
spline.Modified()
# get interpolation density
u_res = n_points
if u_res is None:
u_res = points.shape[0]
return pv.geometric_objects.surface_from_para(spline, u_res)
# convert points to splines
splines = []for idx in range(x.shape[0]):
points = np.column_stack((x[idx], y[idx], z[idx]))
spline = make_spline(points)
# spline = pv.Spline(points) # in the next release
splines.append(spline)
# boring plot
pv.plot(splines)
# exciting plot with scalars
tubes = []for spline in splines:
# add scalars here...
spline['scalars'] = np.arange(spline.n_points)
tubes.append(spline.tube(0.01))
pv.plot(tubes, smooth_shading=True)
[image: cool]
<https://user-images.githubusercontent.com/11981631/58937943-1a608500-8774-11e9-8f26-c1da1ad0044f.png>
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#11?email_source=notifications&email_token=AEB5O7R3JO2L3OGIS3NC2ALPY5TSBA5CNFSM4HS2PI5KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODW63FDA#issuecomment-498971276>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AEB5O7XWHGYSBKC7RFV2MHDPY5TSBANCNFSM4HS2PI5A>
.
--
Sunil Kumar Yadav
Freie University Berlin
Berlin, Germany
Email ID: *sunil.yadav@fu-berlin.de <sunil@zedat.fu-berlin.de>*
Tel(Office) : +49-30-838-75860
Mob: +49-176-313-14991
|
Are you running the latest pyvista? |
Thank you, I wasn't using the latest version.
Everything is perfect now.
…On Wed, Jun 5, 2019 at 10:16 AM Alex Kaszynski ***@***.***> wrote:
Are you running the latest pyvista?
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#11?email_source=notifications&email_token=AEB5O7WICO5WYM4WAICFVXLPY5Y6HA5CNFSM4HS2PI5KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODW66SCA#issuecomment-498985224>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AEB5O7UG2YGRBXHMHQWCZYTPY5Y6HANCNFSM4HS2PI5A>
.
--
Sunil Kumar Yadav
Freie University Berlin
Berlin, Germany
Email ID: *sunil.yadav@fu-berlin.de <sunil@zedat.fu-berlin.de>*
Tel(Office) : +49-30-838-75860
Mob: +49-176-313-14991
|
Dear Alex,
Can I add a mesh geometry to the same panel ?
e.g. using add_mesh?
…On Wed, Jun 5, 2019 at 10:16 AM Alex Kaszynski ***@***.***> wrote:
Are you running the latest pyvista?
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#11?email_source=notifications&email_token=AEB5O7WICO5WYM4WAICFVXLPY5Y6HA5CNFSM4HS2PI5KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODW66SCA#issuecomment-498985224>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AEB5O7UG2YGRBXHMHQWCZYTPY5Y6HANCNFSM4HS2PI5A>
.
--
Sunil Kumar Yadav
Freie University Berlin
Berlin, Germany
Email ID: *sunil.yadav@fu-berlin.de <sunil@zedat.fu-berlin.de>*
Tel(Office) : +49-30-838-75860
Mob: +49-176-313-14991
|
Not sure what you’re trying to do. Can you provide a code example? |
Description
Hello everyone, I am quite new to PyVista, I want to plot several 3d curves in a single plot, what would be best way to do it ?
A 3D curve can be seen as a polyline with vertex positions and vertex positions are given as a ndarray (nx3) for a curve with n vertices. I have several ndarrays regarding a set of 3D curves and want to plot them. I like the visual representation of streamlines in PyVista. Can these curve be plotted with similar visuals ?
I am looking forward to your valuable suggestions
Example Data
For example, I am attaching 3 csv files regarding x, y, and z coordinates of 24 different curves
3dcurves.zip
The text was updated successfully, but these errors were encountered: