Spaces:
Sleeping
Sleeping
File size: 7,889 Bytes
ca2c89c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
import matplotlib.pyplot as plt
import seaborn as sns
def apply_custom_css():
"""Load custom CSS for the Flask interface"""
css_file_path = "static/css/style.css"
try:
with open(css_file_path, "r") as f:
return f.read()
except Exception as e:
print(f"Warning: Could not load custom CSS: {e}")
return ""
def setup_mpl_style():
"""Setup matplotlib style for consistent visualizations"""
try:
plt.style.use('seaborn-v0_8-whitegrid')
sns.set_style("whitegrid")
except:
# Fallback if seaborn style is not available
plt.style.use('default')
# Configure matplotlib for better visuals
plt.rcParams['figure.figsize'] = (10, 6)
plt.rcParams['axes.labelsize'] = 12
plt.rcParams['axes.titlesize'] = 14
plt.rcParams['xtick.labelsize'] = 10
plt.rcParams['ytick.labelsize'] = 10
plt.rcParams['legend.fontsize'] = 10
plt.rcParams['axes.spines.top'] = False
plt.rcParams['axes.spines.right'] = False
def create_bar_chart(labels, values, title, xlabel, ylabel, color='#1976D2'):
"""Create a matplotlib bar chart"""
setup_mpl_style()
fig, ax = plt.subplots(figsize=(10, 6))
bars = ax.bar(labels, values, color=color)
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
return fig
def create_horizontal_bar_chart(labels, values, title, xlabel, ylabel, color='#1976D2'):
"""Create a matplotlib horizontal bar chart"""
setup_mpl_style()
fig, ax = plt.subplots(figsize=(10, 6))
bars = ax.barh(labels, values, color=color)
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.invert_yaxis() # To have the highest value at the top
plt.tight_layout()
return fig
def create_pie_chart(labels, values, title, colors=None):
"""Create a matplotlib pie chart"""
setup_mpl_style()
fig, ax = plt.subplots(figsize=(8, 8))
if colors is None:
colors = ['#1976D2', '#4CAF50', '#FF9800', '#F44336', '#9C27B0', '#00BCD4', '#FFC107', '#795548']
wedges, texts, autotexts = ax.pie(values, labels=labels, autopct='%1.1f%%', colors=colors)
ax.set_title(title)
# Improve text readability
for autotext in autotexts:
autotext.set_color('white')
autotext.set_fontweight('bold')
plt.tight_layout()
return fig
def create_line_chart(x_values, y_values, title, xlabel, ylabel, color='#1976D2'):
"""Create a matplotlib line chart"""
setup_mpl_style()
fig, ax = plt.subplots(figsize=(10, 6))
ax.plot(x_values, y_values, color=color, linewidth=2, marker='o')
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.grid(True, alpha=0.3)
plt.tight_layout()
return fig
def create_scatter_plot(x_values, y_values, title, xlabel, ylabel, color='#1976D2'):
"""Create a matplotlib scatter plot"""
setup_mpl_style()
fig, ax = plt.subplots(figsize=(10, 6))
ax.scatter(x_values, y_values, color=color, alpha=0.6, s=50)
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.grid(True, alpha=0.3)
plt.tight_layout()
return fig
def create_heatmap(data, title, xlabel, ylabel, cmap='YlGnBu'):
"""Create a matplotlib heatmap"""
setup_mpl_style()
fig, ax = plt.subplots(figsize=(10, 8))
im = ax.imshow(data, cmap=cmap, aspect='auto')
# Add colorbar
cbar = ax.figure.colorbar(im, ax=ax)
cbar.ax.set_ylabel('Value', rotation=-90, va="bottom")
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
plt.tight_layout()
return fig
def create_word_cloud_placeholder(text, title="Word Cloud"):
"""Create a placeholder for word cloud visualization"""
setup_mpl_style()
fig, ax = plt.subplots(figsize=(10, 6))
# Create a simple text visualization as placeholder
ax.text(0.5, 0.5, f"Word Cloud: {title}\n\n{text[:100]}...",
ha='center', va='center', fontsize=12,
bbox=dict(boxstyle="round,pad=0.3", facecolor="lightblue", alpha=0.7))
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.axis('off')
ax.set_title(title)
plt.tight_layout()
return fig
def create_network_graph(edges, nodes, title="Network Graph"):
"""Create a network graph visualization"""
setup_mpl_style()
fig, ax = plt.subplots(figsize=(12, 8))
# Simple network visualization
if edges and nodes:
# Extract node positions (simplified)
pos = {}
for i, node in enumerate(nodes):
angle = 2 * 3.14159 * i / len(nodes)
pos[node] = (0.5 + 0.3 * np.cos(angle), 0.5 + 0.3 * np.sin(angle))
# Draw edges
for edge in edges:
if len(edge) >= 2:
x1, y1 = pos.get(edge[0], (0, 0))
x2, y2 = pos.get(edge[1], (0, 0))
ax.plot([x1, x2], [y1, y2], 'k-', alpha=0.5, linewidth=1)
# Draw nodes
for node, (x, y) in pos.items():
ax.scatter(x, y, s=200, c='lightblue', edgecolors='black', linewidth=2)
ax.text(x, y, str(node), ha='center', va='center', fontsize=8)
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.axis('off')
ax.set_title(title)
plt.tight_layout()
return fig
def create_gauge_chart(value, max_value=1.0, title="Gauge Chart"):
"""Create a gauge chart visualization"""
setup_mpl_style()
fig, ax = plt.subplots(figsize=(8, 8), subplot_kw=dict(projection='polar'))
# Create gauge
theta = np.linspace(0, np.pi, 100)
r = np.ones_like(theta)
# Color based on value
if value / max_value > 0.7:
color = '#4CAF50' # Green
elif value / max_value > 0.4:
color = '#FF9800' # Orange
else:
color = '#F44336' # Red
ax.fill_between(theta, 0, r, alpha=0.3, color=color)
ax.plot(theta, r, color=color, linewidth=3)
# Add value indicator
indicator_theta = np.pi * (1 - value / max_value)
ax.plot([indicator_theta, indicator_theta], [0, 1], color='black', linewidth=4)
ax.set_ylim(0, 1)
ax.set_title(title, pad=20)
ax.set_xticks([])
ax.set_yticks([])
# Add value text
ax.text(0, 0, f'{value:.2f}', ha='center', va='center', fontsize=20, fontweight='bold')
plt.tight_layout()
return fig
def create_comparison_chart(categories, values1, values2, title, xlabel, ylabel,
label1="Series 1", label2="Series 2", color1='#1976D2', color2='#4CAF50'):
"""Create a comparison bar chart"""
setup_mpl_style()
fig, ax = plt.subplots(figsize=(12, 6))
x = np.arange(len(categories))
width = 0.35
bars1 = ax.bar(x - width/2, values1, width, label=label1, color=color1)
bars2 = ax.bar(x + width/2, values2, width, label=label2, color=color2)
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.set_xticks(x)
ax.set_xticklabels(categories, rotation=45, ha='right')
ax.legend()
# Add value labels on bars
for bars in [bars1, bars2]:
for bar in bars:
height = bar.get_height()
ax.annotate(f'{height:.1f}',
xy=(bar.get_x() + bar.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
plt.tight_layout()
return fig
|