How to use own CSV file in matplotlib

143 views Asked by At

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?

0

There are 0 answers