[ Web Proxy ]
URL:
Viewing: https://matplotlib.org/users/../examples/pylab_examples/hist_colormapped.html [Back]  [Original]

pylab_examples example code: hist_colormapped.py — Matplotlib 2.0.2 documentation
You are reading an old version of the documentation (v2.0.2). For the latest version see https://matplotlib.org/stable/
forkme_right_orange_ff7600.png [forkme_right_orange_ff7600.png]
matplotlib [matplotlib]
depsy_badge.svg [depsy_badge.svg]
Travis-CI: matplotlib.svg [matplotlib.svg]

This Page

pylab_examples example code: hist_colormapped.py

(Source code, png, pdf)

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

fig, ax = plt.subplots()
Ntotal = 1000
N, bins, patches = ax.hist(np.random.rand(Ntotal), 20)

# I'll color code by height, but you could use any scalar


# we need to normalize the data to 0..1 for the full
# range of the colormap
fracs = N.astype(float)/N.max()
norm = colors.Normalize(fracs.min(), fracs.max())

for thisfrac, thispatch in zip(fracs, patches):
    color = cm.viridis(norm(thisfrac))
    thispatch.set_facecolor(color)


plt.show()

Keywords: python, matplotlib, pylab, example, codex (see Search examples)


Web Proxy Viewer  |  New URL  |  Original Page