Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting values needed for a histogram heatmap from an array

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)
like image 873
user2757799 Avatar asked Apr 14 '26 16:04

user2757799


1 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

like image 125
tdy Avatar answered Apr 17 '26 04:04

tdy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!