0

in matplotlib(specifically matplotlib.cm), you can call a colour from a colormap, like so

import matplotlib.cm as cm
cm.viridis(0.5) #viridis is the name of a colormap

and it takes the colour from the centre of the colormap(which is (0.127568, 0.566949, 0.550556, 1.0)) basically I want to be able to call a colormap that is taken from the library palletable. and you can import a specific palette like so:

from palettable.colorbrewer.qualitative import Dark2_7

This imports the colormap "Dark2_7" but I don't have any idea how to "call" it as I did with viridis, I'm just starting out and I am literally clueless, thank you, sorry if the wording is weird, ask if you need more details or if this isn't clear enough.

snipers500
  • 101
  • 1

1 Answers1

1

You can address an individual color as Dark2_7.mpl_colors[i] with a value for i from 0 to 6 (as there are 7 colors in that map). Or you can use it just like viridis if you do Dark2_7.mpl_colormap(0.5).

Here is an example to use it in a scatter plot. Notice that the default version is a continuous colormap with all colors interpolated. If you need a discrete colormap, use ListedColormap(Dark2_7.mpl_colors).

from matplotlib import pyplot as plt
import numpy as np
from palettable.colorbrewer.qualitative import Dark2_7
from matplotlib.colors import ListedColormap

x = np.random.uniform(-1, 1, 10000)
y = np.random.uniform(-1, 1, 10000)
z = np.sqrt(x**2+y**2)/1.4
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 5))
scat1 = ax1.scatter(x, y, c=z, vmin=0, vmax=1, s=1, cmap=Dark2_7.mpl_colormap)
plt.colorbar(scat1, ax=ax1)
scat2 = ax2.scatter(x, y, c=z, vmin=0, vmax=1, s=1, cmap=ListedColormap(Dark2_7.mpl_colors))
plt.colorbar(scat2, ax=ax2)
plt.show()

scatter plot

JohanC
  • 71,591
  • 8
  • 33
  • 66