Extracting values needed for a histogram heatmap from an array

Viewed 109

I have a list/array(?) in this format

enter image description here

data = [[1,1,2],[4,3,3,8,1],[2,3,4]]

(The real list is much longer with more days and temperatures)

How do I get x and y needed for this function?

plt.hist2d(x, y)

Background:
I would like to make a time series heatmap like here: How to plot Time Series Heatmap with Python? , where x axis is days, y axis is temperature and hue is the frequency of a specific temperature in that day

Update

Seems like I got it to work by modifying @Guldborg92 's answer

x = []
y = []
for day in range(0, len(data)):
    for temperature in data[day]:
        x.append(day)
        y.append(temperature)

    plt.hist2d(x, y)
2 Answers

This is a more pythonic version using comprehensions instead of loops:

x = [day for day, temps in enumerate(data) for _ in temps]
y = [temp for day in data for temp in day]

plt.hist2d(x, y, bins=[15, 30], cmap='OrRd')

hist2d of temps by day

If I understand your data structure correctly then you can flatten the data in a nested for loop like this:

data = [[1,1,2], [4,3,3,8,1], [2,3,4]]

for day in range(0, len(data)):
    for temperature in data[day]:
        plt.hist2d(day + 1, temperature)
Related