Skip to content

Instantly share code, notes, and snippets.

@wolfiex
Last active March 19, 2022 19:41
Show Gist options
  • Save wolfiex/64d2faa495f8f0e1b1a68cdbdf3817f1 to your computer and use it in GitHub Desktop.
Save wolfiex/64d2faa495f8f0e1b1a68cdbdf3817f1 to your computer and use it in GitHub Desktop.
Bivariate colourplot edit
import numpy as np
import matplotlib.pyplot as plt
def colorFromBivariateData(Z1,Z2,cmap1 = plt.cm.Blues, cmap2 = plt.cm.Reds, preset = False):
if preset:
z1mn = 0.
z2mn = 0.
z1mx = 1.
z2mx = 1.
else:
z1mn = Z1.min()
z2mn = Z2.min()
z1mx = Z1.max()
z2mx = Z2.max()
# Rescale values to fit into colormap range (0->255)
Z1_plot = np.array(255*(Z1-z1mn)/(z1mx-z1mn), dtype=np.int)
Z2_plot = np.array(255*(Z2-z2mn)/(z2mx-z2mn), dtype=np.int)
Z1_color = cmap1(Z1_plot)
Z2_color = cmap2(Z2_plot)
# Color for each point
Z_color = np.sum([Z1_color, Z2_color], axis=0)/2.0
return Z_color
xx, yy = np.mgrid[0:50,0:50]
C_map = colorFromBivariateData(xx,yy)
fig = plt.figure(figsize=(10,5))
ax1 = fig.add_subplot(1,2,1)
x = xx.flatten()
y = yy.flatten()
cflat = C_map.reshape((len(x),4))
plt.scatter(x,y,c = cflat,s=8)
ax1.set_title('Data')
xx, yy = np.mgrid[0:9,0:9]
C_map = colorFromBivariateData(xx,yy)
ax2 = fig.add_subplot(1,2,2)
ax2.imshow(C_map)
ax2.set_title('Bivariate Color Map')
ax2.set_xlabel('Variable 1')
ax2.set_ylabel('Variable 2')
ax2.set_ylim((-0.5,0.5+(yy.max()-yy.min())))
fig.tight_layout()
fig.show()
@wolfiex
Copy link
Author

wolfiex commented Feb 6, 2020

Screen Shot 2020-02-06 at 15 56 51

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment