user979974
user979974

Reputation: 953

Python plot heatmap from csv pixel file with panda

I would like to plot a heatmap from a csv file which contains pixels position. This csv file has this shape:

0 0 8.400000e+01
1 0 8.500000e+01
2 0 8.700000e+01
3 0 8.500000e+01
4 0 9.400000e+01
5 0 7.700000e+01
6 0 8.000000e+01
7 0 8.300000e+01
8 0 8.900000e+01
9 0 8.500000e+01
10 0 8.300000e+01

I try to write some lines in Python, but it returns me an error. I guess it is the format of column 3 which contains string. Is there any way to plot this kind of file?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

path_to_csv= "/run/media/test.txt"
df= pd.read_csv(path_to_csv ,sep='\t')
plt.imshow(df,cmap='hot',interpolation='nearest')

plt.show(df)

I tried also seaborn but with no success. Here the error returned:

TypeError: Image data of dtype object cannot be converted to float

Upvotes: 0

Views: 130

Answers (1)

Timeless
Timeless

Reputation: 37787

You can set dtype=float as a keyword argument of pandas.read_csv :

df = pd.read_csv(path_to_csv, sep='\t', dtype=float)

Or use pandas.DataFrame.astype :

plt.imshow(df.astype(float), cmap='hot', interpolation='nearest', aspect='auto')

plt.show()

# Output :

enter image description here

Upvotes: 1

Related Questions