GithubHelp home page GithubHelp logo

Comments (10)

banesullivan avatar banesullivan commented on July 1, 2024

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.

gunjan71011 avatar gunjan71011 commented on July 1, 2024

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.

banesullivan avatar banesullivan commented on July 1, 2024

Could you provide your code to make the matplotlib colormap?

from pyvista-support.

gunjan71011 avatar gunjan71011 commented on July 1, 2024
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.

banesullivan avatar banesullivan commented on July 1, 2024

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()

download

# Plot with PyVista in 3D!
mesh.warp_by_scalar('Elevation').plot(scalars='values', cmap=mycolormap)

download

from pyvista-support.

banesullivan avatar banesullivan commented on July 1, 2024

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.

banesullivan avatar banesullivan commented on July 1, 2024

FYI: I added this to the example gallery: https://docs.pyvista.org/examples/02-plot/cmap.html#custom-made-colormaps

from pyvista-support.

gunjan71011 avatar gunjan71011 commented on July 1, 2024

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.

banesullivan avatar banesullivan commented on July 1, 2024

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.

gunjan71011 avatar gunjan71011 commented on July 1, 2024

Thanks Bane, that was really helpful!

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.