python-for-data-and-media-communication-gitbook icon indicating copy to clipboard operation
python-for-data-and-media-communication-gitbook copied to clipboard

How to set colours when we draw a chart?

Open FLYSTEPHEN opened this issue 6 years ago • 4 comments

FLYSTEPHEN avatar Nov 30 '18 15:11 FLYSTEPHEN

I just solved it...I found some solutions online. For instance, if we draw a simple chart like this.

from matplotlib import pyplot as plt
fig, ax=plt.subplots()
x = range(10)
y = range(10)
sc = ax.scatter(x,y, c=y, cmap='Blues')
fig.colorbar(sc, orientation="horizontal")
plt.show()

Output: screenshot 2018-12-01 at 00 16 12

  • The question actually is how to replace 'Blues' in cmap='Blues'
  1. Use built-in colormaps
import matplotlib.cm as cm
cm.cmap_d.keys()

Output:

dict_keys(['Blues', 'BrBG', 'BuGn', 'BuPu', 'CMRmap', 'GnBu', 'Greens', 'Greys', 'OrRd', 'Oranges', 'PRGn', 'PiYG', 'PuBu', 'PuBuGn', 'PuOr', 'PuRd', 'Purples', 'RdBu', 'RdGy', 'RdPu', 'RdYlBu', 'RdYlGn', 'Reds', 'Spectral', 'Wistia', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd', 'afmhot', 'autumn', 'binary', 'bone', 'brg', 'bwr', 'cool', 'coolwarm', 'copper', 'cubehelix', 'flag', 'gist_earth', 'gist_gray', 'gist_heat', 'gist_ncar', 'gist_rainbow', 'gist_stern', 'gist_yarg', 'gnuplot', 'gnuplot2', 'gray', 'hot', 'hsv', 'jet', 'nipy_spectral', 'ocean', 'pink', 'prism', 'rainbow', 'seismic', 'spring', 'summer', 'terrain', 'winter', 'Accent', 'Dark2', 'Paired', 'Pastel1', 'Pastel2', 'Set1', 'Set2', 'Set3', 'tab10', 'tab20', 'tab20b', 'tab20c', 'Blues_r', 'BrBG_r', 'BuGn_r', 'BuPu_r', 'CMRmap_r', 'GnBu_r', 'Greens_r', 'Greys_r', 'OrRd_r', 'Oranges_r', 'PRGn_r', 'PiYG_r', 'PuBu_r', 'PuBuGn_r', 'PuOr_r', 'PuRd_r', 'Purples_r', 'RdBu_r', 'RdGy_r', 'RdPu_r', 'RdYlBu_r', 'RdYlGn_r', 'Reds_r', 'Spectral_r', 'Wistia_r', 'YlGn_r', 'YlGnBu_r', 'YlOrBr_r', 'YlOrRd_r', 'afmhot_r', 'autumn_r', 'binary_r', 'bone_r', 'brg_r', 'bwr_r', 'cool_r', 'coolwarm_r', 'copper_r', 'cubehelix_r', 'flag_r', 'gist_earth_r', 'gist_gray_r', 'gist_heat_r', 'gist_ncar_r', 'gist_rainbow_r', 'gist_stern_r', 'gist_yarg_r', 'gnuplot_r', 'gnuplot2_r', 'gray_r', 'hot_r', 'hsv_r', 'jet_r', 'nipy_spectral_r', 'ocean_r', 'pink_r', 'prism_r', 'rainbow_r', 'seismic_r', 'spring_r', 'summer_r', 'terrain_r', 'winter_r', 'Accent_r', 'Dark2_r', 'Paired_r', 'Pastel1_r', 'Pastel2_r', 'Set1_r', 'Set2_r', 'Set3_r', 'tab10_r', 'tab20_r', 'tab20b_r', 'tab20c_r', 'magma', 'magma_r', 'inferno', 'inferno_r', 'plasma', 'plasma_r', 'viridis', 'viridis_r', 'cividis', 'cividis_r'])
  • Then choose one name in the dict to replace 'Blues'. I post the colormap's pattern here: builtin
  1. Set up a new colormap
from matplotlib import pyplot as plt
from matplotlib import colors as mcl

colors = [[0.0, "blue"],[0.8, "yellow"],[1.0, "red"]]
my_cmap = mcl.LinearSegmentedColormap.from_list("", colors)

fig, ax=plt.subplots()
x = range(10)
y = range(10)
sc = ax.scatter(x,y, c=y, cmap=my_cmap)
fig.colorbar(sc, orientation="horizontal")
plt.show()

Output: screenshot 2018-12-01 at 00 08 42

Hope it can help anyone!

FLYSTEPHEN avatar Nov 30 '18 16:11 FLYSTEPHEN

BYW we can use these lines to check the name of basic colours, we may need them to construct a colormap.

import matplotlib.cm as cm
cm.colors.cnames.keys()

Output:

dict_keys(['aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure', 'beige', 'bisque', 'black', 'blanchedalmond', 'blue', 'blueviolet', 'brown', 'burlywood', 'cadetblue', 'chartreuse', 'chocolate', 'coral', 'cornflowerblue', 'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan', 'darkgoldenrod', 'darkgray', 'darkgreen', 'darkgrey', 'darkkhaki', 'darkmagenta', 'darkolivegreen', 'darkorange', 'darkorchid', 'darkred', 'darksalmon', 'darkseagreen', 'darkslateblue', 'darkslategray', 'darkslategrey', 'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue', 'dimgray', 'dimgrey', 'dodgerblue', 'firebrick', 'floralwhite', 'forestgreen', 'fuchsia', 'gainsboro', 'ghostwhite', 'gold', 'goldenrod', 'gray', 'green', 'greenyellow', 'grey', 'honeydew', 'hotpink', 'indianred', 'indigo', 'ivory', 'khaki', 'lavender', 'lavenderblush', 'lawngreen', 'lemonchiffon', 'lightblue', 'lightcoral', 'lightcyan', 'lightgoldenrodyellow', 'lightgray', 'lightgreen', 'lightgrey', 'lightpink', 'lightsalmon', 'lightseagreen', 'lightskyblue', 'lightslategray', 'lightslategrey', 'lightsteelblue', 'lightyellow', 'lime', 'limegreen', 'linen', 'magenta', 'maroon', 'mediumaquamarine', 'mediumblue', 'mediumorchid', 'mediumpurple', 'mediumseagreen', 'mediumslateblue', 'mediumspringgreen', 'mediumturquoise', 'mediumvioletred', 'midnightblue', 'mintcream', 'mistyrose', 'moccasin', 'navajowhite', 'navy', 'oldlace', 'olive', 'olivedrab', 'orange', 'orangered', 'orchid', 'palegoldenrod', 'palegreen', 'paleturquoise', 'palevioletred', 'papayawhip', 'peachpuff', 'peru', 'pink', 'plum', 'powderblue', 'purple', 'rebeccapurple', 'red', 'rosybrown', 'royalblue', 'saddlebrown', 'salmon', 'sandybrown', 'seagreen', 'seashell', 'sienna', 'silver', 'skyblue', 'slateblue', 'slategray', 'slategrey', 'snow', 'springgreen', 'steelblue', 'tan', 'teal', 'thistle', 'tomato', 'turquoise', 'violet', 'wheat', 'white', 'whitesmoke', 'yellow', 'yellowgreen'])

FLYSTEPHEN avatar Nov 30 '18 16:11 FLYSTEPHEN

This issue is worth a stand alone tutorial. 👍 . Let's keep it in "discussion" tag first, in case others meet similar problems. We can come back to consolidate materials later.

hupili avatar Nov 30 '18 17:11 hupili

Just a little finding.

  • Sometimes we don't need to create a brand new colormap but adjust the original ones. e.g. The colormap Blues_r ends with an almost pure white, which means a display problem on white backgrounds: blue_r
  • We can use this lines to change some certain parts of a default colormap:
import numpy as np
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
Blues_r = cm.get_cmap('Blues_r', 6)  #first get a model of colormap
newcolors = Blues_r(np.linspace(0, 1, 6))  #divide this map into 6 sectors
c6 = np.array([235/256, 240/256, 250/256, 1])  #use xxx/256 because this is RGB scale
newcolors[5:6, :] = c6  #this part of cmap is too light, exchange it with c6
newcmp = ListedColormap(newcolors)
  • Here is the comparison, the lightest blue looks better now: compare

FLYSTEPHEN avatar Dec 16 '18 04:12 FLYSTEPHEN