How to rotate colormap for scatters data points

68 Views Asked by At

I got the problem with implementing idea. I need to make colormap be diagonal for scatters, so on the top right corner there would be only green color, down left it will would be red and yellow would go through the plot from top left corner to down right. Right now there only option where colormap is vertical which I cannot use. Plot result that I have right now I attached to this post.

x_target = df_target[x_stat]
y_target = df_target[y_stat]
    
x_rest = df_rest[x_stat]
y_rest = df_rest[y_stat]
    
fig, ax = plt.subplots(figsize=(w, h))
            
cmap = mpl.colors.LinearSegmentedColormap.from_list('dyag', ["#e64c4b", "#FFA500", "#64c0b3"], N=256)
    
# Normalize x values 
norm = plt.Normalize(vmin=df_scatters[x_stat].min(), vmax=df_scatters[x_stat].max())

# Plotting data points
sc = ax.scatter(x_rest, y_rest, c=x_rest, s=50, cmap=cmap, norm=norm)
sc = ax.scatter(x_target, y_target, c=x_target, s=80, cmap=cmap, norm=norm)
            
# Set figure and axes background color
fig.set_facecolor('white')
ax.set_facecolor('white') 

ax.set_xlim((x_min_label, x_max_label))
ax.set_ylim((y_min_label, y_max_label))

I've tried to apply image(colors from red to green in diagonal) to data points in my plot. Hence, I tried somehow change position of the colormap that yellow color would be in diagonal.

2

There are 2 best solutions below

0
Matt Pitkin On BEST ANSWER

Here's a simple example of having the colour map diagonally across the plot. It essentially sets the colour value based on the distance from a diagonal line (with a gradient of -1) resting against the origin:

from matplotlib import pyplot as plt
import matplotlib as mpl

import numpy as np

x = np.random.rand(1000)
y = np.random.rand(1000)

cmap = mpl.colors.LinearSegmentedColormap.from_list('dyag', ["#e64c4b", "#FFA500", "#64c0b3"], N=256)

fig, ax = plt.subplots()
ax.scatter(x, y, c=((x + y) / np.sqrt(2)), cmap=cmap)

fig.show()

enter image description here

0
Anonymous On

Define a list of colours based on your data.

This calculates the distance from a diagonal line. You might need to adjust the calculation based on the data you have.

import numpy as np
import matplotlib.pyplot as plt

x = np.random.rand(100)
y = np.random.rand(100)
colours = (x + y)/np.sqrt(2)

plt.scatter(x, y, c=colours, ec='k', cmap="RdYlGn")

enter image description here