7

I have read some questions on this subject but I have been unable to find a specific answer to my question.

Let consider the image below:

Color map example

My goal is just to change the limit colors of the map, e.g. in this case the color map goes from dark red to dark blue, let's say I would like it to go from dark green to dark blue. Specifically, I would it to go from colors #244162 to #DCE6F1 (tonalities of blue) in the same continuous way as in the example above.

How is it possible to do this?


[EDIT]

I have tried the following code:

import matplotlib.pyplot as plt
import matplotlib.colors as clr

some_matrix = ...
cmap = clr.LinearSegmentedColormap('custom blue', ['#244162','#DCE6F1'], N=256)
plt.matshow(some_matrix, cmap=cmap)

But I get the error message TypeError: list indices must be integers, not unicode.

4
  • 1
    Other than "read some questions", have you tried anything? For example, have you looked for an existing colormap that would work or tried LinearSegmentedColormap? Commented May 23, 2016 at 23:05
  • Here are some specific examples in matplotlib documentation. matplotlib.org/examples/color/colormaps_reference.html Commented May 23, 2016 at 23:11
  • I have tried some existing color maps but I would like to define a custom one to keep a uniform formatting throughout a document. I have tried to implement LinearSegmentedColormap with this code cmap = matplotlib.colors.LinearSegmentedColormap('custom blue', ['#244162','#DCE6F1'], N=256) but it hasn't worked. Commented May 23, 2016 at 23:20
  • 1
    @DaneelOlivaw put that information in the question itself, and describe exactly what "it hasn't worked" means. Did the cm not generate? Was it not the colors you were looking for? What happened? Commented May 23, 2016 at 23:36

1 Answer 1

18

LinearSegmentedColormap doesn't take a list of colours, it takes the following argument:

a dictionary with a red, green and blue entries. Each entry should be a list of x, y0, y1 tuples, forming rows in a table. Entries for alpha are optional.

So, you either need to define a dictionary as above, or in your case, I think you just want to use the LinearSegmentedColormap.from_list() method:

import matplotlib.pyplot as plt
import matplotlib.colors as clr
import numpy as np

some_matrix = np.random.rand(10,10)

cmap = clr.LinearSegmentedColormap.from_list('custom blue', ['#244162','#DCE6F1'], N=256)

plt.matshow(some_matrix, cmap=cmap)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Ah brilliant! That's exactly what I wanted, I was missing the .from_list, thank you!
By far the quickest way I found to create gradient cmaps; thanks for sharing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.