-
-
Save tsherwen/e84ef03c5df97e728df0077d1f6a4343 to your computer and use it in GitHub Desktop.
Bivariate colourplot edit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment