1

I am facing the problem with loading CSV data into matplotlib.

This is how my code looks like this:

import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import pandas as pd

csv_filename = 'heatmap_data.csv'
df = pd.read_csv(csv_filename)

delta = 0.050
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = Z2 - Z1  # difference of Gaussians

im = plt.imshow(Z, interpolation='bilinear', cmap=cm.bwr,
                origin='lower', extent=[0, 100, 100, 0],
                vmax=abs(Z).max(), vmin=-abs(Z).max())

cb = plt.colorbar()

plt.savefig("heatmap.png")

CSV Data which I want to load:

x_pos,y_pos,type,importance
74,64,blue,-0.011517617893368
68,64,blue,-0.0041303878348102
32,64,red,0.049788810065569
8,64,red,0.12877712212094
88,64,red,0.0050599724578342
84,64,blue,-0.00052412736663743
80,64,blue,-0.020183850819375
78,64,blue,-0.01297132988303
72,64,red,0.080092605800612
64,64,red,0.074683215098353
62,64,blue,-0.011168199648943
58,64,red,0.030129086612831

x_pos is X coordinate y_pos is Y coordinate importance is something like Z coordinate. It means importance of point.

Importance points > 0 should have red color with emphasis to importance.

Importance points < 0 should have blue color with emphasis to importance.

Every coordinate on Heatmap without any data should be white or transparent. Goals is Heatmap similar to this example sample heatmap. Is it possible with this library?

Roman Šimr
  • 81
  • 1
  • 10
  • 1
    Do you have trouble with retrieving data from the .csv file, or with plotting the data assuming you have it already? – Guimoute Oct 15 '18 at 09:03
  • @Guimoute Retriving data is working. Problem for me is connect data to matplotlib plugin. I can't find any info in docs how to do it. – Roman Šimr Oct 15 '18 at 09:05
  • https://seaborn.pydata.org/generated/seaborn.heatmap.html , tried seaborn ? – Sriram Arvind Lakshmanakumar Oct 15 '18 at 09:42
  • @SriramArvindLakshmanakumar Yes, but seaborn heat map shows data in squares, it's not right way how I need to visualize my data – Roman Šimr Oct 15 '18 at 13:35
  • At the end you will need an array of values, one for each coordinate, but the entries at the positions where there is no data can be `np.nan` or masked out via `numpy.ma`. – ImportanceOfBeingErnest Oct 15 '18 at 14:03
  • @RomanŠimr Heat maps always have squares, simply the "pretty" maps have small and numerous squares. One thing you can do if your data is too square to your taste is interpolate values so you have more theoretical squares between your actually measured squares. Hopefully that makes sense. – Guimoute Oct 15 '18 at 14:24

0 Answers0