Monday 25 February 2019

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



from Not getting the heatmap in the background using Matplotlib Python

No comments:

Post a Comment