Skip to content

Instantly share code, notes, and snippets.

@ExpHP
Last active May 27, 2020 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ExpHP/0b02849899d0e55058ce5c6d4d532812 to your computer and use it in GitHub Desktop.
Save ExpHP/0b02849899d0e55058ce5c6d4d532812 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib as mpl
x = np.linspace(0, 10, 500)
y = np.cos(x)
value = 10 * np.cos(x / 3)
alpha = y**2
fig, ax = plt.subplots(figsize=(4, 3))
cmap = cm.get_cmap('hsv')
norm = mpl.colors.Normalize(vmin=-10, vmax=10)
C = np.zeros((len(x), 4))
C[:, :3] = cmap(norm(value))[:, :3]
C[:, 3] = alpha
ax.set(title='plot with correct colorbar and alpha')
ax.scatter(x, y, c=C)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
plt.colorbar(sm)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment