Comments (10)
Hi @gunjan71011! For this situation, you would need to make your own colormap using Matplotlib and pass that colormap as the cmap
argument.
Perhaps try an example like this: https://matplotlib.org/gallery/images_contours_and_fields/custom_cmap.html
And then pass it to PyVista similar to this example: https://docs.pyvista.org/examples/02-plot/cmap.html
FYI: there is a clim
argument for the add_mesh
method so you donβt need to use update_scalar_bar_range()
Sent with GitHawk
from pyvista-support.
Thanks for the clim argument tip! So I payed around with customizing and making my own cmap and putting it into pyvista, however, I still can't get the colors to exactly be between a range of values, more specifically I want it to be between certain ranges of the scalar values used to scale my figure. I can create a colorbar with the exact values I want using matplotlib just fine, but i think you can only use color maps on pyvista using the cmap argument. Would you have any more feedback? Thank you!
from pyvista-support.
Could you provide your code to make the matplotlib colormap?
from pyvista-support.
from matplotlib.colors import ListedColormap
from matplotlib import cm
import numpy as np
viridis = cm.get_cmap('viridis', 256)
newcolors = viridis(np.linspace(0, 1, 256))
blue = np.array([12/256, 238/256, 246/256, 1])
black = np.array([11/256, 11/256, 11/256, 1])
grey = np.array([189/256, 189/256, 189/256, 1])
yellow = np.array([255/256, 247/256, 0/256, 1])
red = np.array([1,0,0,1])
newcolors[0] = black #If my scaler is 0 i want it to be black in color
newcolors[1:30] = blue #If my scaler is between 1 and 30 I want it to be blue in color
newcolors[30:55] = yellow
newcolors[55:80] = grey
newcolors[80:] = red #Above 80 I want it to be red. The thing is I don't think these numbers actually represent the scalar values. But I would like it to.
newcmp = ListedColormap(newcolors)
Got this idea from:
https://matplotlib.org/3.1.1/tutorials/colors/colormap-manipulation.html
from pyvista-support.
Here's an example - the colormap needed a bit more work:
import pyvista as pv
from pyvista import examples
from matplotlib.colors import ListedColormap
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np
pv.set_plot_theme('doc')
mesh = examples.download_st_helens()
mesh['values'] = pv.plotting.normalize(mesh['Elevation']) * 100
# Make the custom colormap
blue = np.array([12/256, 238/256, 246/256, 1])
black = np.array([11/256, 11/256, 11/256, 1])
grey = np.array([189/256, 189/256, 189/256, 1])
yellow = np.array([255/256, 247/256, 0/256, 1])
red = np.array([1,0,0,1])
mapping = np.linspace(mesh['values'].min(), mesh['values'].max(), 256)
newcolors = np.empty((256, 4))
newcolors[mapping >= 80] = red
newcolors[mapping < 80] = grey
newcolors[mapping < 55] = yellow
newcolors[mapping < 30] = blue
newcolors[mapping < 1] = black
mycolormap = ListedColormap(newcolors)
# Plot with Matplotlib for a sanity check
image = mesh['values'].reshape(mesh.dimensions[:2], order='f')
plt.pcolormesh(image, cmap=mycolormap)
plt.colorbar()
# Plot with PyVista in 3D!
mesh.warp_by_scalar('Elevation').plot(scalars='values', cmap=mycolormap)
from pyvista-support.
Please note that you likely won't be able to plot your mesh with Matplotlib - that only works if the input dataset is a 2D pyvista.UniformGrid
object
from pyvista-support.
FYI: I added this to the example gallery: https://docs.pyvista.org/examples/02-plot/cmap.html#custom-made-colormaps
from pyvista-support.
would you know how can fix the issue to normalize my data? The line "mesh['values'] = pv.plotting.normalize(mesh['Elevation']) * 100" keeps giving the error: module 'pyvista.plotting' has no attribute 'normalize'
from pyvista-support.
Hm - you must be on an older version of PyVista. That's a simple function that you could just copy/paste:
def normalize(x, minimum=None, maximum=None):
if minimum is None:
minimum = np.nanmin(x)
if maximum is None:
maximum = np.nanmax(x)
return (x - minimum) / (maximum - minimum)
But note that you will not need that normalizing function when you do this with your data - I simply used that to make an example with similar ranges to yours.
from pyvista-support.
Thanks Bane, that was really helpful!
from pyvista-support.
Related Issues (20)
- create uniform mesh HOT 3
- Change the orientation of a scalar bar's title
- Applying textures on models from .obj files HOT 6
- How to create an Unstructured grid from points and faces HOT 2
- Type of data, plotting with glyphs HOT 3
- Controlling the thickness and orientation of a slice in Pyvista HOT 3
- Controlling the image position in window, without any click
- Mask/Remove Voxels in Air (small intersection with surface) HOT 3
- custom slider parameters
- running pyvista on headless machine with Centos OS HOT 1
- Seismic Horizon with Pyvista HOT 8
- Algorithm in sample function?
- Automatically updates HOT 3
- Slow interaction HOT 1
- read from text HOT 4
- Uniform Grid thresholding question HOT 8
- Take data by an input x,y,x coordinate (csv file) from vtk data file HOT 4
- Program broke when creating a mesh using pv.PolyData() HOT 4
- ATTENTION: Please move questions to PyVista's Discussions HOT 1
- plotter.enable_terrain_style not working for me HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyvista-support.