Spaces:
Sleeping
Sleeping
| import os | |
| os.system("pip install seaborn") | |
| import gradio as gr | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| # Function to generate random visitor data | |
| def generate_random_visitors(num_points): | |
| return np.random.randint(0, 1000, size=num_points) | |
| # Function to generate random energy distribution heatmap data | |
| def generate_random_heatmap_data(size): | |
| return np.random.rand(size, size) * 100 | |
| # Generate random visitor data | |
| num_points = 24 # Assume 24 time points for a day | |
| visitor_data = generate_random_visitors(num_points) | |
| # Function to plot visitor data and heatmap | |
| def plot_data(start_time, end_time, energy_algorithm): | |
| # Convert start_time and end_time to integers | |
| start_time = int(start_time) | |
| end_time = int(end_time) | |
| # Ensure start_time is less than end_time | |
| if start_time > end_time: | |
| return "开始时间点不能晚于结束时间点", None | |
| # Generate visitor data for the selected time range | |
| selected_data = visitor_data[start_time:end_time + 1] | |
| # Plotting the visitor data line chart | |
| fig, ax = plt.subplots(2, 1, figsize=(12, 10)) | |
| # Line chart for visitor data | |
| ax[0].plot(range(start_time, end_time + 1), selected_data, marker='o') | |
| ax[0].set_title("Line chart of the number of visitors to the exhibition hall") | |
| ax[0].set_xlabel("time step") | |
| ax[0].set_ylabel("Number of people") | |
| # Generate heatmap data | |
| heatmap_data = generate_random_heatmap_data(num_points) | |
| # Heatmap for energy distribution | |
| sns.heatmap(heatmap_data, cmap="YlOrRd", ax=ax[1], cbar=True) | |
| ax[1].set_title("Energy distribution heat map of the exhibition hall") | |
| plt.tight_layout() | |
| plt.show() | |
| return f"选择的能源调度算法是: {energy_algorithm}", fig | |
| # Define Gradio interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 展馆人流与能源分配分析") | |
| with gr.Row(): | |
| with gr.Column(): | |
| start_time = gr.Slider(0, num_points - 1, 0, label="开始时间点") | |
| end_time = gr.Slider(0, num_points - 1, num_points - 1, label="结束时间点") | |
| energy_algorithm = gr.Dropdown(["自适应负载调度", "粒子群算法", "蚁群算法"], label="选择能源调度算法") | |
| btn = gr.Button("生成图表") | |
| with gr.Column(): | |
| output_text = gr.Textbox(label="选中的能源调度算法") | |
| plot_output = gr.Plot(label="展馆人流数量折线图与能源分配热力图") | |
| btn.click(plot_data, inputs=[start_time, end_time, energy_algorithm], outputs=[output_text, plot_output]) | |
| demo.launch() | |