Dynamically loaded data did not map correctly to the heat map, but manually hardcoded the data into the code and the heat map displayed correctly.
Everyone, please see the magical events I encountered using pyecharts:
The code I used to plot the heat map is as follows:
import time
def aggregate_data_by_weekday_and_hour(data):
"""Date conversion, combined data"""
create_time = np.array([int(item['create_time']) for item in data])
video_play_count = np.array([int(item['video_play_count']) for item in data])
# Initialize list to store aggregated data
aggregated_data_list = []
# Iterate over timestamps and play counts
for ts, count in zip(create_time, video_play_count):
# Convert timestamp to weekday and 12-hour format
weekday = time.strftime("%A", time.localtime(ts)) # Convert milliseconds to seconds
hour = time.strftime("%H", time.localtime(ts)) # Convert milliseconds to seconds
# Combine weekday and hour as key
key = f"{weekday}_{hour}"
# Split key into weekday and hour
weekday, hour = key.split('_')
# Append the result to the list
aggregated_data_list.append([hour, weekday, count])
return aggregated_data_list
def supplement_data(input):
"""Completion of data"""
data_dict = {(str(hour).zfill(2), day): 0 for hour in range(24) for day in
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']}
# Populate raw data
for item in input:
hour, day, value = item
data_dict[(hour, day)] = value
# Converts a dictionary to list form and sorts it by day of the week and hour
filled_data = [[hour, day, value] for (hour, day), value in sorted(data_dict.items())]
return filled_data
def custom_sort(item):
week_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
return int(item[0]), week_order.index(item[1])
from pyecharts.charts import HeatMap
def create_heatmap(aggregated_data):
filled_data = supplement_data(aggregated_data)
heatmap_data = sorted(filled_data, key=custom_sort)
# Define week and time ranges
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
times = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11',
'12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']
max_value = max(heatmap_data, key=lambda x: x[2])[2]
non_zero_values = [x[2] for x in heatmap_data if x[2] != 0]
min_value = min(non_zero_values) if non_zero_values else 0
# Create a HeatMap instance and set the parameters
heatmap = (
HeatMap()
.add_xaxis(times)
.add_yaxis("Views", weekdays, heatmap_data)
.set_global_opts(
title_opts=opts.TitleOpts(title="观看量热力图", subtitle="以12小时制展示"),
visualmap_opts=opts.VisualMapOpts(min_=min_value, max_=max_value),
)
)
return heatmap
However, the heat map does not show any part except 0.
enter image description here
But what if I just comment out the two lines that get the data and print out heatmap_data:
from pyecharts.charts import HeatMap
def create_heatmap(aggregated_data):
# filled_data = supplement_data(aggregated_data)
# heatmap_data = sorted(filled_data, key=custom_sort)
heatmap_data = [['00', 'Monday', 0], ['00', 'Tuesday', 0], ['00', 'Wednesday', 0], ['00', 'Thursday', 0], ['00', 'Friday', 0], ['00', 'Saturday', 0], ['00', 'Sunday', 0], ['01', 'Monday', 0], ['01', 'Tuesday', 0], ['01', 'Wednesday', 0], ['01', 'Thursday', 0], ['01', 'Friday', 0], ['01', 'Saturday', 0], ['01', 'Sunday', 0], ['02', 'Monday', 0], ['02', 'Tuesday', 0], ['02', 'Wednesday', 0], ['02', 'Thursday', 0], ['02', 'Friday', 0], ['02', 'Saturday', 0], ['02', 'Sunday', 0], ['03', 'Monday', 0], ['03', 'Tuesday', 0], ['03', 'Wednesday', 0], ['03', 'Thursday', 0], ['03', 'Friday', 0], ['03', 'Saturday', 0], ['03', 'Sunday', 0], ['04', 'Monday', 0], ['04', 'Tuesday', 0], ['04', 'Wednesday', 0], ['04', 'Thursday', 0], ['04', 'Friday', 0], ['04', 'Saturday', 0], ['04', 'Sunday', 0], ['05', 'Monday', 0], ['05', 'Tuesday', 0], ['05', 'Wednesday', 0], ['05', 'Thursday', 0], ['05', 'Friday', 0], ['05', 'Saturday', 0], ['05', 'Sunday', 0], ['06', 'Monday', 0], ['06', 'Tuesday', 0], ['06', 'Wednesday', 0], ['06', 'Thursday', 0], ['06', 'Friday', 0], ['06', 'Saturday', 0], ['06', 'Sunday', 0], ['07', 'Monday', 0], ['07', 'Tuesday', 0], ['07', 'Wednesday', 0], ['07', 'Thursday', 0], ['07', 'Friday', 0], ['07', 'Saturday', 0], ['07', 'Sunday', 0], ['08', 'Monday', 0], ['08', 'Tuesday', 0], ['08', 'Wednesday', 0], ['08', 'Thursday', 0], ['08', 'Friday', 0], ['08', 'Saturday', 0], ['08', 'Sunday', 0], ['09', 'Monday', 397577], ['09', 'Tuesday', 0], ['09', 'Wednesday', 0], ['09', 'Thursday', 0], ['09', 'Friday', 0], ['09', 'Saturday', 0], ['09', 'Sunday', 0], ['10', 'Monday', 0], ['10', 'Tuesday', 0], ['10', 'Wednesday', 0], ['10', 'Thursday', 0], ['10', 'Friday', 294981], ['10', 'Saturday', 598007], ['10', 'Sunday', 787201], ['11', 'Monday', 0], ['11', 'Tuesday', 0], ['11', 'Wednesday', 114681], ['11', 'Thursday', 0], ['11', 'Friday', 0], ['11', 'Saturday', 251777], ['11', 'Sunday', 0], ['12', 'Monday', 0], ['12', 'Tuesday', 0], ['12', 'Wednesday', 0], ['12', 'Thursday', 0], ['12', 'Friday', 0], ['12', 'Saturday', 362695], ['12', 'Sunday', 13], ['13', 'Monday', 0], ['13', 'Tuesday', 0], ['13', 'Wednesday', 0], ['13', 'Thursday', 0], ['13', 'Friday', 0], ['13', 'Saturday', 0], ['13', 'Sunday', 0], ['14', 'Monday', 0], ['14', 'Tuesday', 0], ['14', 'Wednesday', 0], ['14', 'Thursday', 0], ['14', 'Friday', 0], ['14', 'Saturday', 0], ['14', 'Sunday', 0], ['15', 'Monday', 0], ['15', 'Tuesday', 0], ['15', 'Wednesday', 0], ['15', 'Thursday', 0], ['15', 'Friday', 0], ['15', 'Saturday', 0], ['15', 'Sunday', 0], ['16', 'Monday', 0], ['16', 'Tuesday', 0], ['16', 'Wednesday', 0], ['16', 'Thursday', 0], ['16', 'Friday', 0], ['16', 'Saturday', 0], ['16', 'Sunday', 0], ['17', 'Monday', 0], ['17', 'Tuesday', 0], ['17', 'Wednesday', 0], ['17', 'Thursday', 0], ['17', 'Friday', 392098], ['17', 'Saturday', 0], ['17', 'Sunday', 0], ['18', 'Monday', 0], ['18', 'Tuesday', 0], ['18', 'Wednesday', 0], ['18', 'Thursday', 0], ['18', 'Friday', 0], ['18', 'Saturday', 520310], ['18', 'Sunday', 851239], ['19', 'Monday', 0], ['19', 'Tuesday', 0], ['19', 'Wednesday', 0], ['19', 'Thursday', 0], ['19', 'Friday', 1772419], ['19', 'Saturday', 0], ['19', 'Sunday', 1733823], ['20', 'Monday', 0], ['20', 'Tuesday', 0], ['20', 'Wednesday', 0], ['20', 'Thursday', 0], ['20', 'Friday', 551078], ['20', 'Saturday', 0], ['20', 'Sunday', 141825], ['21', 'Monday', 0], ['21', 'Tuesday', 0], ['21', 'Wednesday', 0], ['21', 'Thursday', 0], ['21', 'Friday', 0], ['21', 'Saturday', 350839], ['21', 'Sunday', 191825], ['22', 'Monday', 0], ['22', 'Tuesday', 0], ['22', 'Wednesday', 0], ['22', 'Thursday', 0], ['22', 'Friday', 0], ['22', 'Saturday', 0], ['22', 'Sunday', 1044084], ['23', 'Monday', 0], ['23', 'Tuesday', 0], ['23', 'Wednesday', 0], ['23', 'Thursday', 0], ['23', 'Friday', 0], ['23', 'Saturday', 0], ['23', 'Sunday', 0]]
# 定义星期和时间范围
weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
times = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11',
'12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']
max_value = max(heatmap_data, key=lambda x: x[2])[2]
non_zero_values = [x[2] for x in heatmap_data if x[2] != 0]
min_value = min(non_zero_values) if non_zero_values else 0
# 创建 HeatMap 实例并设置参数
heatmap = (
HeatMap()
.add_xaxis(times)
.add_yaxis("Views", weekdays, heatmap_data)
.set_global_opts(
title_opts=opts.TitleOpts(title="观看量热力图", subtitle="以12小时制展示"),
visualmap_opts=opts.VisualMapOpts(min_=min_value, max_=max_value),
)
)
return heatmap
Note that the heatmap_data = ...
above was obtained directly via print(heatmap_data) and pasted in from the output box.
After this operation, he can display it normally.
enter image description here
Why is that? Is it wrong to pass it directly instead? And how should I fix it?
repo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.