1

I have been able to plot scatter with color palette representing the continuous variable using following script:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

x, y, z = np.random.rand(3, 50)
cmap = sns.cubehelix_palette(start=5, light=1, as_cmap=True)

fig, ax = plt.subplots()
points = ax.scatter(x, y, c=z, s=20, cmap=cmap)
fig.colorbar(points)

Output univariate palette

However, I need to create a map with 'bivariate color palette'. I will ultimately be creating map like this but at the time being I am looking for plotting scatter in such a way. (Source : https://www.linkedin.com/feed/update/urn:li:activity:6378928737907466240) Bivariate palette I am looking to plot scatter such that the color represents both variables RMSE and R as in the map.

Suwash
  • 23
  • 5

1 Answers1

2

You could create a bivariate color map by taking the average of two different colour maps. It's good to use different colour schemes, you can experiment with the range shown here.

import numpy as np
import matplotlib.pyplot as plt

def colorFromBivariateData(Z1,Z2,cmap1 = plt.cm.YlOrRd, cmap2 = plt.cm.PuBuGn):
    # Rescale values to fit into colormap range (0->255)
    Z1_plot = np.array(255*(Z1-Z1.min())/(Z1.max()-Z1.min()), dtype=np.int)
    Z2_plot = np.array(255*(Z2-Z2.min())/(Z2.max()-Z2.min()), 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

z1 = np.random.random((50,100))
z2 = np.random.random((50,100))
Z_color = colorFromBivariateData(z1,z2)

xx, yy = np.mgrid[0:100,0:100]
C_map = colorFromBivariateData(xx,yy)

fig = plt.figure(figsize=(10,5))

ax1 = fig.add_subplot(1,2,1)
ax1.imshow(Z_color)
ax1.set_title('Data')

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')

fig.tight_layout()
fig.show()

The output is:

enter image description here

There is plenty of information about plotting on a map, as well as embedding a smaller axis onto a larger one, so it shouldn't be too hard to extend this idea to create the image you linked to in your question.

Let me know if this answers your question!

Robbie
  • 4,672
  • 1
  • 19
  • 24
  • I have edited the script to work with scatterplot data and to reverse the y axis within the colourmap. This shows how these work in a better way. https://gist.github.com/wolfiex/64d2faa495f8f0e1b1a68cdbdf3817f1#file-bivariate-py – user2589273 Feb 06 '20 at 15:41