0

I'm trying to convert a list of lists from a text file to a heatmap plot and save them as multiple frames. I intend to make these frames into a video next but I need to get around this problem first.

The plotting portion of this works if I explicitly type in a line from my file like this...

[[20.48400116,20.89500046,20.40100098,20.36100197,20.40100098],[20.32699966,20.6590023,20.40799904,20.52900124,20.53800011],[20.41800117,20.34899902,20.77000046,20.37600136,20.61500168],[20.51899719,20.43799973,20.68099976,20.44000053,20.51899719]]

I get this... heatmap

But if instead of explicitly defining the list I iterate over a text file with one of these lists on every line I get a weird error

Traceback (most recent call last):
  File "heatmap.py", line 20, in <module>
    im = ax.imshow(line)
  File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\__init__.py", line 1565, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\cbook\deprecation.py", line 358, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\cbook\deprecation.py", line 358, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\axes\_axes.py", line 5615, in imshow
    im.set_data(X)
  File "C:\Users\me\AppData\Local\Programs\Python\Python37-32\lib\site-packages\matplotlib\image.py", line 694, in set_data
    "float".format(self._A.dtype))
TypeError: Image data of dtype <U248 cannot be converted to float

Here is my code...

import numpy as np
import pylab
import matplotlib
import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rc('image', cmap='inferno')
rows = ["Row 1", "Row 2", "Row 3", "Row 4"]
columns = ["Column 1", "Column 2", "Column 3", "Column 4", "Column 5"]
suffix = 10001
title = ("Thermal Data From Coldplate in TVAC-3")
suffix = 10001
with open('tvac3_thermal_model.txt', 'r') as file:
    lines = file.readlines()
    lines = [line.rstrip() for line in lines]
    for line in lines:
        #print(line)
        fig, ax=plt.subplots()
        im = ax.imshow(line)
        ax.set_xticks(np.arange(len(columns)))
        ax.set_yticks(np.arange(len(rows)))
        ax.set_xticklabels(columns)
        ax.set_yticklabels(rows)
        ax.set_title(title)
        plt.setp(ax.get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor")
        plt.plot()
        plt.savefig("frame" + suffix + '.png')
        suffix += 1

If anyone can take a look and give me a hint it would be awesome. Thanks in advance.

DBarnett
  • 13
  • 4
  • Try `lines = [ast.literal_eval(line.rstrip()) for line in lines]`, where `ast.literal_eval` should convert the string back to a list. Also update `savefig` to `plt.savefig(f'frame_{suffix}.png')`. – Trenton McKinney Sep 16 '21 at 17:36
  • 1
    Solved! Thank you so much! Also, you couldn't have known but my file has more than 5000 rows so I ended up needing to add mpl.rc('figure', max_open_warning = 0) and a plt.close() to my loop to get through them all without the whole thing choking. – DBarnett Sep 16 '21 at 19:59

0 Answers0