Home > Enterprise >  Black to Red Datapoints in MatPlotLib
Black to Red Datapoints in MatPlotLib

Time:04-21

I need to create a table that follows the color scheme black to red as seen here (Red and Black data) I have managed to get mine to go from black to white with red in the middle using a color map (my graph), but have been struggling with identifying how to use an array to get this exact color scheme. Any help would be great

CURRENT CODE:

colors = np.linspace(0,1,25, endpoint = True)

size = np.linspace(10,100,25, endpoint = True)

limits = [-2.25, 2.25, -2.25, 2.25]

plt.figure(figsize=(20, 5))
x_orig = list(data['x'])
y_orig = list(data['y'])

plt.subplot(1, 2, 1)
plt.scatter(x_orig, y_orig, s=size, c=colors, cmap='gist_heat')
plt.title('Original Data')
plt.xlabel('x')
plt.ylabel('y')
plt.xlim([limits[0], limits[1]])
plt.ylim([limits[2], limits[3]])

Snippet of the data

CodePudding user response:

Create your own colormap from midway of 'gist_heat'. Following code may help

from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
from matplotlib import cm
from matplotlib.colors import ListedColormap, LinearSegmentedColormap


x = [0, 0, 0, 0, 0, .24, .24, .24, .24, .24, .49, .49, .49, .49, .49]
y = [0, .24, .49, .74, .99, 0, .24, .49, .74, .99, 0, .24, .49, .74]

data = pd.DataFrame(list(zip(x,y)), columns = ['x', 'y'])

gist_heat = cm.get_cmap('gist_heat', len(data))
newColors= gist_heat(range(28))[:round(len(data)/2)]
newCmap = ListedColormap(newColors)

colors = np.linspace(0,1,len(data), endpoint = True)
size = np.linspace(10,100,len(data), endpoint = True)

limits = [-2.25, 2.25, -2.25, 2.25]

plt.figure(figsize=(20, 5))
x_orig = list(data['x'])
y_orig = list(data['y'])
plt.subplot(1, 2, 1)
plt.scatter(x_orig, y_orig, s=size, c=colors, cmap=newCmap)
plt.title('Original Data')
plt.xlabel('x')
plt.ylabel('y')
plt.xlim([limits[0], limits[1]])
plt.ylim([limits[2], limits[3]])
plt.show()
  • Related