Modifying viridis colormap (replacing some colors)

2024/9/21 4:26:30

I've searched around and found things that came close to working but nothing exactly suiting what I need.

Basically, I really like the viridis colormap as a starting point. However, I would like to replace the purple at the lowest end of the map with white.

I tried using set_under() but that doesn't suite my needs. I need to simply replace purple with white.

For example, I tried the following (from here Matplotlib discrete colorbar) -

cmap = plt.get_cmap('jet')
cmaplist = [cmap(i) for i in range(cmap.N)]
cmaplist[0] = (1.0,1.0,1.0,1.0)
cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)

Which works perfectly and does exactly what I need with 'jet' but when I replace 'jet' with 'viridis' I get the following error

AttributeError: 'ListedColormap' object has no attribute 'from_list'

How can I get around this and simply do what I want?

Answer

The from_list() method is a static method of the LinearSegmentedColormap class. It may not make too much sense to call it on an instance of the class, as you do in case of the jet map (although it does work of course).

Now, 'viridis' is implemented as a ListedColormap instead of a LinearSegmentedColormap, which implies that it does not have this method.

In any case it makes more sense to call the static method from the class itself.

import matplotlib.colorscmap = plt.cm.viridis
cmaplist = [cmap(i) for i in range(cmap.N)]
cmaplist[0] = (1.0,1.0,1.0,1.0)
cmap = matplotlib.colors.LinearSegmentedColormap.from_list('mcm',cmaplist, cmap.N)

In this way, it will work for just any colormap, not only those which are segmented ones.

https://en.xdnf.cn/q/72087.html

Related Q&A

gtk minimum size

Is there an easy way to request that a GTK widget have a minimum width/height? I know you can do it on the column of a TreeView, but is it available for general widgets?

How do I convert a json file to a python class?

Consider this json file named h.json I want to convert this into a python dataclass. {"acc1":{"email":"[email protected]","password":"acc1","name&…

PyTorch how to compute second order Jacobian?

I have a neural network thats computing a vector quantity u. Id like to compute first and second-order jacobians with respect to the input x, a single element. Would anybody know how to do that in PyTo…

Tensorflow setup on RStudio/ R | CentOS

For the last 5 days, I am trying to make Keras/Tensorflow packages work in R. I am using RStudio for installation and have used conda, miniconda, virtualenv but it crashes each time in the end. Install…

Cant import soundfile

Im using Anaconda and Im trying to import soundfile/pysoundfile. I installed the package by running conda install -c conda-forge pysoundfile and I think it succeeded because when I run conda list it sh…

Most efficient way to multiply a small matrix with a scalar in numpy

I have a program whose main performance bottleneck involves multiplying matrices which have one dimension of size 1 and another large dimension, e.g. 1000: large_dimension = 1000a = np.random.random((1…

MultiValueDictKeyError / request.POST

I think I hav a problem at request.POST[title]MultiValueDictKeyError at /blog/add/post/"title"Request Method: GETRequest URL: http://119.81.247.69:8000/blog/add/post/Django Version: 1.8.…

How can I auto run py.test once a relative command has been change?

Via autonose or nosy, it will automatically run the nosetests once the some tests file or the relative files have been changes. I would like to ask that whether py.test provides the similar function fo…

Publish a post using XML-RPC WordPress API and Python with category

Im doing a migration from a website to another one which use Wordpress. I created new custom types for my needs (with the plugin Custom Post Types), and I created categories for each custom type.I then…

Django registration email not sending

Ive been trying to get the django-registration-redux account activation email to send to newly registered users.Ive gotten all non-email related parts to work, such as loggin in/out and actually regist…