Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading