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

Change the range of the axes' plots #60

Closed
Andersongeofisco opened this issue Sep 20, 2019 · 8 comments
Closed

Change the range of the axes' plots #60

Andersongeofisco opened this issue Sep 20, 2019 · 8 comments
Labels
mesh-creation Topics around creating a PyVista mesh

Comments

@Andersongeofisco
Copy link

Hello everyone! I hope that everyone is fine. I have a doubt :) I'd like to know if it is possible to change the range of the axes in a volume plot for example. The axes of my plots always start from 0 to length of my 3d numpy array, I'd like to plot for example something like, -5 to 5 (-5,-4,-3,-2,-1,0,1,2,3,4,5) instead (0,1,2,3,4,5,6,7,8,9,10) at the axes.

Thanks!

@banesullivan
Copy link
Member

banesullivan commented Sep 20, 2019

Hi, @Andersongeofisco! Do you have any code to go with your question so that I can help you implement this with your workflow?

It sounds like you are using PyVista strictly for plotting and passing a 3D numpy array to the Plotter's add_mesh or add_volume methods to render that dataset. Behind the scenes when you do this, an UniformGrid object is created of your 3D data. I'd recommend creating a PyVista dataset before plotting and properly set the spatial reference of that dataset. For example:

import numpy as np
import pyvista as pv

# Create a 3D numpy array like what is used
arr = np.arange(1000).reshape((10,10,10))

# Create a PyVista UniformGrid for the 3D array
grid = pv.UniformGrid()
# Set the spatial reference
grid.dimensions = np.array(arr.shape) + 1 #dim + 1 because cells
grid.origin = (-5, -5, -5)
grid.spacing = (1, 1, 1)
# Add data arrays
grid['data'] = arr.ravel(order='F')

# Now plot the properly referenced dataset
grid.plot(show_grid=True, show_edges=True)

download

@banesullivan banesullivan added the mesh-creation Topics around creating a PyVista mesh label Sep 20, 2019
@Andersongeofisco
Copy link
Author

Andersongeofisco commented Sep 20, 2019

Hi @banesullivan ! Here is a example of my code.

import pyvista as pv
import numpy as np
ns=401
nv=21
nps=52
dado1=open("dvp3.ad",'rb')
d=np.fromfile(dado1,dtype=np.float32,count=ns*nv*nps).reshape([ns,nv,nps],order='F')

a=pv.wrap(d)
outline = a.outline()
slices = a.slice_orthogonal(x=401,y=5,z=52)
p = pv.Plotter()
p.add_mesh(outline)
p.add_mesh(slices,cmap='jet',opacity=1.,stitle="Semblance")
p.add_volume(a,cmap='jet',stitle="Semblance")
p.add_text("Semblance",position='upper_edge')
p.show_bounds(xlabel='Time',ylabel='Velocity',zlabel='Slope',location='outer')
p.show()

@banesullivan
Copy link
Member

So the a object is a pv.UniformGrid in your example. You create in manually without wrap like:

a = pv.UniformGrid()
# Set the spatial reference
a.dimensions = np.array(arr.shape) + 1 #dim + 1 because cells
a.origin = (-5, -5, -5)
a.spacing = (1, 1, 1)
# Add data arrays
a['Semblance'] = d.ravel(order='F')

@Andersongeofisco
Copy link
Author

Thanks @banesullivan for your help! You helped a lot this young phd student :)

@Andersongeofisco
Copy link
Author

I will try it and if everything works well I will return to say thanks.

@Andersongeofisco
Copy link
Author

Hi @banesullivan ! I tried your tip, but I get another problem now. If I try to use the a object that was created to add_volume or slice I have this error message:

ERROR:root:Cell Scalars not supported
Falha de segmentação (imagem do núcleo gravada)

Here is my code:

import pyvista as pv
from pyvista import examples
import numpy as np
import matplotlib.pyplot as plt

ns=401
nv=21
nps=52
dado1=open("dvp3.ad",'rb')
d=np.fromfile(dado1,dtype=np.float32,count=nsnvnps).reshape([ns,nv,nps],order='F')

a = pv.UniformGrid()

a.dimensions = np.array(d.shape) + 1 #dim + 1 because cells
a.origin = (-5, -5, -5)
a.spacing = (1, 1, 1)
#Add data arrays
a['Semblance'] = d.ravel(order='F')

outline = a.outline()
slices = a.slice_orthogonal(x=401,y=5,z=52,contour='True')
p = pv.Plotter()
p.add_mesh(outline)
p.add_mesh(slices,cmap='jet',opacity=1.,stitle="Semblance")
p.add_volume(a,cmap='jet',stitle="Semblance")
p.add_text("Semblance",position='upper_edge')
p.show_bounds(xlabel='Time',ylabel='Velocity',zlabel='Slope',location='outer')
p.show()

@banesullivan
Copy link
Member

Ah, my bad. Take away that + 1 when setting the dimensions. I order for it to stretch from -5 to 5, you may need to adjust the spacing to something other than 1

Sent with GitHawk

@Andersongeofisco
Copy link
Author

Andersongeofisco commented Sep 20, 2019

Thanks again @banesullivan! Everything is working well now. Thank you very much!

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