Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not getting the heatmap in the background using Matplotlib Python

I have tried this and got the result as in the image:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
cmap = LinearSegmentedColormap.from_list("", ["red","grey","green"])
df = pd.read_csv('t.csv', header=0)

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax = ax1.twiny()
# Scatter plot of positive points, coloured blue (C0)
ax.scatter(np.argwhere(df['real'] > 0), df.loc[df['real'] > 0, 'real'], color='C2')
# Scatter plot of negative points, coloured red (C3)
ax.scatter(np.argwhere(df['real'] < 0), df.loc[df['real'] < 0, 'real'], color='C3')
# Scatter neutral values in grey (C7)
ax.scatter(np.argwhere(df['real'] == 0), df.loc[df['real'] == 0, 'real'], color='C7')

ax.set_ylim([df['real'].min(), df['real'].max()])
index = len(df.index)
ymin = df['prediction'].min()
ymax= df['prediction'].max()
ax1.imshow([np.arange(index),df['prediction']],cmap=cmap,
                        extent=(0,index-1,ymin, ymax), alpha=0.8)
plt.show()

Image:
output

I was expecting one output where the color is placed according to the figure. I am getting green color and no reds or greys.

I want to get the image or contours spread as the values are. How I can do that? See the following image, something similar:
expected output

Please let me know how I can achieve this. The data I used is here: t.csv
For a live version, have a look at Tensorflow Playground

like image 304
Jaffer Wilson Avatar asked Feb 22 '19 13:02

Jaffer Wilson


2 Answers

There are essentially 2 tasks required in a solution like this:

  • Plot the heatmap as the background;
  • Plot the scatter data;

Output:

Source code:

import numpy as np
import matplotlib.pyplot as plt

###
# Plot heatmap in the background
###

# Setting up input values
x = np.arange(-6.0, 6.0, 0.1)
y = np.arange(-6.0, 6.0, 0.1)
X, Y = np.meshgrid(x, y)

# plot heatmap colorspace in the background
fig, ax = plt.subplots(nrows=1)
im = ax.imshow(X, cmap=plt.cm.get_cmap('RdBu'), extent=(-6, 6, -6, 6), interpolation='bilinear')
cax = fig.add_axes([0.21, 0.95, 0.6, 0.03]) # [left, bottom, width, height]
fig.colorbar(im, cax=cax, orientation='horizontal')  # add colorbar at the top

###
# Plot data as scatter
###
# generate the points
num_samples = 150
theta = np.linspace(0, 2 * np.pi, num_samples)

# generate inner points
circle_r = 2
r = circle_r * np.random.rand(num_samples)
inner_x, inner_y = r * np.cos(theta), r * np.sin(theta)

# generate outter points
circle_r = 4
r = circle_r + np.random.rand(num_samples)
outter_x, outter_y = r * np.cos(theta), r * np.sin(theta)

# plot data
ax.scatter(inner_x, inner_y, s=30, marker='o', color='royalblue', edgecolors='white', linewidths=0.8)
ax.scatter(outter_x, outter_y, s=30, marker='o', color='crimson', edgecolors='white', linewidths=0.8)
ax.set_ylim([-6,6])
ax.set_xlim([-6,6])

plt.show()

To keep things simple, I kept the colorbar range (-6, 6) to match the data range.

I'm sure this code can be changed to suit your specific needs. Good luck!

like image 160
karlphillip Avatar answered Sep 28 '22 06:09

karlphillip


Here is a possible solution.

A few notes and questions:

  • What are the 'prediction' values in your data file? They do not seem to correlate with the values in the 'real' column.
  • Why do you create a second axis? What is represented on the bottom X-axis in your plot? I removed the second axis and labelled the remaining axes (index and real).
  • When you slice a pandas DataFrame, the index comes with it. You don't need to create a separate index (argwhere and arange(index) in your code). I simplified the first part of the code, where scatterplots are produced.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
cmap = LinearSegmentedColormap.from_list("", ["red","grey","green"])
df = pd.read_csv('t.csv', header=0)
print(df)

fig = plt.figure()
ax = fig.add_subplot(111)

# Data limits
xmin = 0
xmax = df.shape[0]
ymin = df['real'].min()
ymax = df['real'].max()

# Scatter plots
gt0 = df.loc[df['real'] > 0, 'real']
lt0 = df.loc[df['real'] < 0, 'real']
eq0 = df.loc[df['real'] == 0, 'real']
ax.scatter(gt0.index, gt0.values, edgecolor='white', color='C2')
ax.scatter(lt0.index, lt0.values, edgecolor='white', color='C3')
ax.scatter(eq0.index, eq0.values, edgecolor='white', color='C7')
ax.set_ylim((ymin, ymax))
ax.set_xlabel('index')
ax.set_ylabel('real')

# We want 0 to be in the middle of the colourbar, 
# because gray is defined as df['real'] == 0
if abs(ymax) > abs(ymin):
    lim = abs(ymax)
else:
    lim = abs(ymin)

# Create a gradient that runs from -lim to lim in N number of steps,
# where N is the number of colour steps in the cmap.
grad = np.arange(-lim, lim, 2*lim/cmap.N)

# Arrays plotted with imshow must be 2D arrays. In this case it will be
# 1 pixel wide and N pixels tall. Set the aspect ratio to auto so that
# each pixel is stretched out to the full width of the frame.
grad = np.expand_dims(grad, axis=1)
im = ax.imshow(grad, cmap=cmap, aspect='auto', alpha=1, origin='bottom',
               extent=(xmin, xmax, -lim, lim))
fig.colorbar(im, label='real')
plt.show()

This gives the following result: scatterplot with gradient in background

like image 22
zan Avatar answered Sep 28 '22 06:09

zan