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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
+
import plotly.express as px
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
from mpl_toolkits.mplot3d import Axes3D
|
| 7 |
+
import matplotlib.colors as mcolors
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Set the style for better visualization
|
| 11 |
+
plt.style.use('seaborn-v0_8-darkgrid')
|
| 12 |
+
|
| 13 |
+
# Read the data
|
| 14 |
+
df = pd.read_csv('محاسبات 2.csv')
|
| 15 |
+
|
| 16 |
+
# Function to clean and prepare the data
|
| 17 |
+
def prepare_data(df):
|
| 18 |
+
# Split the data into two dataframes (area and production)
|
| 19 |
+
area_df = df.iloc[:33].copy()
|
| 20 |
+
production_df = df.iloc[34:].copy()
|
| 21 |
+
|
| 22 |
+
# Clean the data
|
| 23 |
+
area_df = area_df.dropna(subset=['اداره'])
|
| 24 |
+
production_df = production_df.dropna(subset=['تولید'])
|
| 25 |
+
|
| 26 |
+
# Rename columns for consistency
|
| 27 |
+
production_df = production_df.rename(columns={'تولید': 'اداره'})
|
| 28 |
+
|
| 29 |
+
# Melt the dataframes to get long format
|
| 30 |
+
area_melted = pd.melt(area_df,
|
| 31 |
+
id_vars=['اداره', 'سن'],
|
| 32 |
+
value_vars=['CP69', 'CP73', 'CP48', 'CP57', 'CP65', 'CP70', 'IR01-412', 'IRC99-07', 'IRC00-14'],
|
| 33 |
+
var_name='واریته',
|
| 34 |
+
value_name='مساحت')
|
| 35 |
+
|
| 36 |
+
production_melted = pd.melt(production_df,
|
| 37 |
+
id_vars=['اداره', 'سن'],
|
| 38 |
+
value_vars=['CP69', 'CP73', 'CP48', 'CP57', 'CP65', 'CP70', 'IR01-412', 'IRC99-07', 'IRC00-14'],
|
| 39 |
+
var_name='واریته',
|
| 40 |
+
value_name='تولید')
|
| 41 |
+
|
| 42 |
+
# Merge the dataframes
|
| 43 |
+
merged_df = pd.merge(area_melted, production_melted, on=['اداره', 'سن', 'واریته'])
|
| 44 |
+
|
| 45 |
+
# Convert numeric columns to float
|
| 46 |
+
merged_df['مساحت'] = pd.to_numeric(merged_df['مساحت'], errors='coerce')
|
| 47 |
+
merged_df['تولید'] = pd.to_numeric(merged_df['تولید'], errors='coerce')
|
| 48 |
+
|
| 49 |
+
# Fill NaN values with 0
|
| 50 |
+
merged_df = merged_df.fillna(0)
|
| 51 |
+
|
| 52 |
+
return merged_df
|
| 53 |
+
|
| 54 |
+
# Prepare the data
|
| 55 |
+
data = prepare_data(df)
|
| 56 |
+
|
| 57 |
+
# Create output directory if it doesn't exist
|
| 58 |
+
os.makedirs('visualizations', exist_ok=True)
|
| 59 |
+
|
| 60 |
+
# 1. 3D Surface Histogram for Area by Department, Age, and Variety
|
| 61 |
+
def create_3d_surface_area():
|
| 62 |
+
# Create a pivot table for the 3D surface
|
| 63 |
+
pivot_data = data.pivot_table(
|
| 64 |
+
values='مساحت',
|
| 65 |
+
index='سن',
|
| 66 |
+
columns='واریته',
|
| 67 |
+
aggfunc='sum'
|
| 68 |
+
).fillna(0)
|
| 69 |
+
|
| 70 |
+
# Create the 3D surface plot
|
| 71 |
+
fig = go.Figure(data=[go.Surface(
|
| 72 |
+
x=pivot_data.columns,
|
| 73 |
+
y=pivot_data.index,
|
| 74 |
+
z=pivot_data.values,
|
| 75 |
+
colorscale='Viridis',
|
| 76 |
+
showscale=True,
|
| 77 |
+
colorbar=dict(title='مساحت (هکتار)')
|
| 78 |
+
)])
|
| 79 |
+
|
| 80 |
+
# Update layout
|
| 81 |
+
fig.update_layout(
|
| 82 |
+
title='توزیع مساحت به تفکیک سن و واریته',
|
| 83 |
+
scene=dict(
|
| 84 |
+
xaxis_title='واریته',
|
| 85 |
+
yaxis_title='سن',
|
| 86 |
+
zaxis_title='مساحت (هکتار)',
|
| 87 |
+
camera=dict(
|
| 88 |
+
eye=dict(x=1.5, y=1.5, z=1.5)
|
| 89 |
+
)
|
| 90 |
+
),
|
| 91 |
+
width=1000,
|
| 92 |
+
height=800,
|
| 93 |
+
font=dict(family="Vazirmatn, Arial", size=14)
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Save the figure
|
| 97 |
+
fig.write_html('visualizations/3d_surface_area.html')
|
| 98 |
+
fig.write_image('visualizations/3d_surface_area.png', scale=2)
|
| 99 |
+
|
| 100 |
+
return fig
|
| 101 |
+
|
| 102 |
+
# 2. 3D Surface Histogram for Production by Department, Age, and Variety
|
| 103 |
+
def create_3d_surface_production():
|
| 104 |
+
# Create a pivot table for the 3D surface
|
| 105 |
+
pivot_data = data.pivot_table(
|
| 106 |
+
values='تولید',
|
| 107 |
+
index='سن',
|
| 108 |
+
columns='واریته',
|
| 109 |
+
aggfunc='sum'
|
| 110 |
+
).fillna(0)
|
| 111 |
+
|
| 112 |
+
# Create the 3D surface plot
|
| 113 |
+
fig = go.Figure(data=[go.Surface(
|
| 114 |
+
x=pivot_data.columns,
|
| 115 |
+
y=pivot_data.index,
|
| 116 |
+
z=pivot_data.values,
|
| 117 |
+
colorscale='Plasma',
|
| 118 |
+
showscale=True,
|
| 119 |
+
colorbar=dict(title='تولید (تن)')
|
| 120 |
+
)])
|
| 121 |
+
|
| 122 |
+
# Update layout
|
| 123 |
+
fig.update_layout(
|
| 124 |
+
title='توزیع تولید به تفکیک سن و واریته',
|
| 125 |
+
scene=dict(
|
| 126 |
+
xaxis_title='واریته',
|
| 127 |
+
yaxis_title='سن',
|
| 128 |
+
zaxis_title='تولید (تن)',
|
| 129 |
+
camera=dict(
|
| 130 |
+
eye=dict(x=1.5, y=1.5, z=1.5)
|
| 131 |
+
)
|
| 132 |
+
),
|
| 133 |
+
width=1000,
|
| 134 |
+
height=800,
|
| 135 |
+
font=dict(family="Vazirmatn, Arial", size=14)
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# Save the figure
|
| 139 |
+
fig.write_html('visualizations/3d_surface_production.html')
|
| 140 |
+
fig.write_image('visualizations/3d_surface_production.png', scale=2)
|
| 141 |
+
|
| 142 |
+
return fig
|
| 143 |
+
|
| 144 |
+
# 3. Area Chart by Department and Age
|
| 145 |
+
def create_area_chart_by_dept_age():
|
| 146 |
+
# Group by department and age
|
| 147 |
+
dept_age_data = data.groupby(['اداره', 'سن'])['مساحت'].sum().reset_index()
|
| 148 |
+
|
| 149 |
+
# Create the area chart
|
| 150 |
+
fig = px.area(
|
| 151 |
+
dept_age_data,
|
| 152 |
+
x='سن',
|
| 153 |
+
y='مساحت',
|
| 154 |
+
color='اداره',
|
| 155 |
+
title='مس��حت به تفکیک اداره و سن',
|
| 156 |
+
labels={'مساحت': 'مساحت (هکتار)', 'سن': 'سن', 'اداره': 'اداره'},
|
| 157 |
+
color_discrete_sequence=px.colors.qualitative.Set3
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
# Update layout
|
| 161 |
+
fig.update_layout(
|
| 162 |
+
width=1000,
|
| 163 |
+
height=600,
|
| 164 |
+
font=dict(family="Vazirmatn, Arial", size=14),
|
| 165 |
+
legend=dict(
|
| 166 |
+
orientation="h",
|
| 167 |
+
yanchor="bottom",
|
| 168 |
+
y=1.02,
|
| 169 |
+
xanchor="right",
|
| 170 |
+
x=1
|
| 171 |
+
)
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
# Save the figure
|
| 175 |
+
fig.write_html('visualizations/area_chart_by_dept_age.html')
|
| 176 |
+
fig.write_image('visualizations/area_chart_by_dept_age.png', scale=2)
|
| 177 |
+
|
| 178 |
+
return fig
|
| 179 |
+
|
| 180 |
+
# 4. Area Chart by Department and Variety
|
| 181 |
+
def create_area_chart_by_dept_variety():
|
| 182 |
+
# Group by department and variety
|
| 183 |
+
dept_variety_data = data.groupby(['اداره', 'واریته'])['مساحت'].sum().reset_index()
|
| 184 |
+
|
| 185 |
+
# Create the area chart
|
| 186 |
+
fig = px.area(
|
| 187 |
+
dept_variety_data,
|
| 188 |
+
x='واریته',
|
| 189 |
+
y='مساحت',
|
| 190 |
+
color='اداره',
|
| 191 |
+
title='مساحت به تفکیک اداره و واریته',
|
| 192 |
+
labels={'مساحت': 'مساحت (هکتار)', 'واریته': 'واریته', 'اداره': 'اداره'},
|
| 193 |
+
color_discrete_sequence=px.colors.qualitative.Set3
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
# Update layout
|
| 197 |
+
fig.update_layout(
|
| 198 |
+
width=1000,
|
| 199 |
+
height=600,
|
| 200 |
+
font=dict(family="Vazirmatn, Arial", size=14),
|
| 201 |
+
legend=dict(
|
| 202 |
+
orientation="h",
|
| 203 |
+
yanchor="bottom",
|
| 204 |
+
y=1.02,
|
| 205 |
+
xanchor="right",
|
| 206 |
+
x=1
|
| 207 |
+
)
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
# Save the figure
|
| 211 |
+
fig.write_html('visualizations/area_chart_by_dept_variety.html')
|
| 212 |
+
fig.write_image('visualizations/area_chart_by_dept_variety.png', scale=2)
|
| 213 |
+
|
| 214 |
+
return fig
|
| 215 |
+
|
| 216 |
+
# 5. 3D Bar Chart for Area by Department, Age, and Variety
|
| 217 |
+
def create_3d_bar_chart():
|
| 218 |
+
# Create a figure
|
| 219 |
+
fig = plt.figure(figsize=(15, 10))
|
| 220 |
+
ax = fig.add_subplot(111, projection='3d')
|
| 221 |
+
|
| 222 |
+
# Get unique values
|
| 223 |
+
departments = data['اداره'].unique()
|
| 224 |
+
ages = data['سن'].unique()
|
| 225 |
+
varieties = data['واریته'].unique()
|
| 226 |
+
|
| 227 |
+
# Create a color map
|
| 228 |
+
colors = plt.cm.viridis(np.linspace(0, 1, len(varieties)))
|
| 229 |
+
|
| 230 |
+
# Create the 3D bar chart
|
| 231 |
+
for i, dept in enumerate(departments):
|
| 232 |
+
for j, age in enumerate(ages):
|
| 233 |
+
for k, variety in enumerate(varieties):
|
| 234 |
+
# Get the value
|
| 235 |
+
value = data[(data['اداره'] == dept) &
|
| 236 |
+
(data['سن'] == age) &
|
| 237 |
+
(data['واریته'] == variety)]['مساحت'].values
|
| 238 |
+
|
| 239 |
+
if len(value) > 0 and value[0] > 0:
|
| 240 |
+
# Create the bar
|
| 241 |
+
ax.bar3d(i, j, k, 0.8, 0.8, value[0], color=colors[k], alpha=0.8)
|
| 242 |
+
|
| 243 |
+
# Set labels
|
| 244 |
+
ax.set_xlabel('اداره')
|
| 245 |
+
ax.set_ylabel('سن')
|
| 246 |
+
ax.set_zlabel('مساحت (هکتار)')
|
| 247 |
+
|
| 248 |
+
# Set ticks
|
| 249 |
+
ax.set_xticks(np.arange(len(departments)) + 0.4)
|
| 250 |
+
ax.set_xticklabels(departments)
|
| 251 |
+
|
| 252 |
+
ax.set_yticks(np.arange(len(ages)) + 0.4)
|
| 253 |
+
ax.set_yticklabels(ages)
|
| 254 |
+
|
| 255 |
+
# Set title
|
| 256 |
+
plt.title('توزیع مساحت به تفکیک اداره، سن و واریته', fontsize=16)
|
| 257 |
+
|
| 258 |
+
# Add a colorbar
|
| 259 |
+
sm = plt.cm.ScalarMappable(cmap=plt.cm.viridis, norm=plt.Normalize(vmin=0, vmax=len(varieties)-1))
|
| 260 |
+
sm.set_array([])
|
| 261 |
+
cbar = plt.colorbar(sm, ax=ax, pad=0.1)
|
| 262 |
+
cbar.set_label('واریته')
|
| 263 |
+
cbar.set_ticks(np.arange(len(varieties)))
|
| 264 |
+
cbar.set_ticklabels(varieties)
|
| 265 |
+
|
| 266 |
+
# Save the figure
|
| 267 |
+
plt.tight_layout()
|
| 268 |
+
plt.savefig('visualizations/3d_bar_chart.png', dpi=300, bbox_inches='tight')
|
| 269 |
+
|
| 270 |
+
return fig
|
| 271 |
+
|
| 272 |
+
# 6. Heatmap for Area by Department and Age
|
| 273 |
+
def create_heatmap_area():
|
| 274 |
+
# Create a pivot table for the heatmap
|
| 275 |
+
pivot_data = data.pivot_table(
|
| 276 |
+
values='مساحت',
|
| 277 |
+
index='اداره',
|
| 278 |
+
columns='سن',
|
| 279 |
+
aggfunc='sum'
|
| 280 |
+
).fillna(0)
|
| 281 |
+
|
| 282 |
+
# Create the heatmap
|
| 283 |
+
fig = go.Figure(data=go.Heatmap(
|
| 284 |
+
z=pivot_data.values,
|
| 285 |
+
x=pivot_data.columns,
|
| 286 |
+
y=pivot_data.index,
|
| 287 |
+
colorscale='Viridis',
|
| 288 |
+
colorbar=dict(title='مساحت (هکتار)')
|
| 289 |
+
))
|
| 290 |
+
|
| 291 |
+
# Update layout
|
| 292 |
+
fig.update_layout(
|
| 293 |
+
title='مساحت به تفکیک اداره و سن',
|
| 294 |
+
xaxis_title='سن',
|
| 295 |
+
yaxis_title='اداره',
|
| 296 |
+
width=900,
|
| 297 |
+
height=600,
|
| 298 |
+
font=dict(family="Vazirmatn, Arial", size=14)
|
| 299 |
+
)
|
| 300 |
+
|
| 301 |
+
# Save the figure
|
| 302 |
+
fig.write_html('visualizations/heatmap_area.html')
|
| 303 |
+
fig.write_image('visualizations/heatmap_area.png', scale=2)
|
| 304 |
+
|
| 305 |
+
return fig
|
| 306 |
+
|
| 307 |
+
# 7. Heatmap for Production by Department and Age
|
| 308 |
+
def create_heatmap_production():
|
| 309 |
+
# Create a pivot table for the heatmap
|
| 310 |
+
pivot_data = data.pivot_table(
|
| 311 |
+
values='تولید',
|
| 312 |
+
index='اداره',
|
| 313 |
+
columns='سن',
|
| 314 |
+
aggfunc='sum'
|
| 315 |
+
).fillna(0)
|
| 316 |
+
|
| 317 |
+
# Create the heatmap
|
| 318 |
+
fig = go.Figure(data=go.Heatmap(
|
| 319 |
+
z=pivot_data.values,
|
| 320 |
+
x=pivot_data.columns,
|
| 321 |
+
y=pivot_data.index,
|
| 322 |
+
colorscale='Plasma',
|
| 323 |
+
colorbar=dict(title='تولید (تن)')
|
| 324 |
+
))
|
| 325 |
+
|
| 326 |
+
# Update layout
|
| 327 |
+
fig.update_layout(
|
| 328 |
+
title='تولید به تفکیک اداره و سن',
|
| 329 |
+
xaxis_title='سن',
|
| 330 |
+
yaxis_title='اداره',
|
| 331 |
+
width=900,
|
| 332 |
+
height=600,
|
| 333 |
+
font=dict(family="Vazirmatn, Arial", size=14)
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
+
# Save the figure
|
| 337 |
+
fig.write_html('visualizations/heatmap_production.html')
|
| 338 |
+
fig.write_image('visualizations/heatmap_production.png', scale=2)
|
| 339 |
+
|
| 340 |
+
return fig
|
| 341 |
+
|
| 342 |
+
# 8. Stacked Bar Chart for Area by Department and Variety
|
| 343 |
+
def create_stacked_bar_chart():
|
| 344 |
+
# Create a pivot table for the stacked bar chart
|
| 345 |
+
pivot_data = data.pivot_table(
|
| 346 |
+
values='مساحت',
|
| 347 |
+
index='اداره',
|
| 348 |
+
columns='واریته',
|
| 349 |
+
aggfunc='sum'
|
| 350 |
+
).fillna(0)
|
| 351 |
+
|
| 352 |
+
# Create the stacked bar chart
|
| 353 |
+
fig = go.Figure()
|
| 354 |
+
|
| 355 |
+
for variety in pivot_data.columns:
|
| 356 |
+
fig.add_trace(go.Bar(
|
| 357 |
+
name=variety,
|
| 358 |
+
x=pivot_data.index,
|
| 359 |
+
y=pivot_data[variety],
|
| 360 |
+
text=pivot_data[variety].round(1),
|
| 361 |
+
textposition='auto',
|
| 362 |
+
))
|
| 363 |
+
|
| 364 |
+
# Update layout
|
| 365 |
+
fig.update_layout(
|
| 366 |
+
title='مساحت به تفکیک اداره و واریته',
|
| 367 |
+
xaxis_title='اداره',
|
| 368 |
+
yaxis_title='مساحت (هکتار)',
|
| 369 |
+
barmode='stack',
|
| 370 |
+
width=1000,
|
| 371 |
+
height=600,
|
| 372 |
+
font=dict(family="Vazirmatn, Arial", size=14),
|
| 373 |
+
legend=dict(
|
| 374 |
+
orientation="h",
|
| 375 |
+
yanchor="bottom",
|
| 376 |
+
y=1.02,
|
| 377 |
+
xanchor="right",
|
| 378 |
+
x=1
|
| 379 |
+
)
|
| 380 |
+
)
|
| 381 |
+
|
| 382 |
+
# Save the figure
|
| 383 |
+
fig.write_html('visualizations/stacked_bar_chart.html')
|
| 384 |
+
fig.write_image('visualizations/stacked_bar_chart.png', scale=2)
|
| 385 |
+
|
| 386 |
+
return fig
|
| 387 |
+
|
| 388 |
+
# 9. 3D Scatter Plot for Area, Production, and Age
|
| 389 |
+
def create_3d_scatter_plot():
|
| 390 |
+
# Create the 3D scatter plot
|
| 391 |
+
fig = go.Figure(data=[go.Scatter3d(
|
| 392 |
+
x=data['مساحت'],
|
| 393 |
+
y=data['تولید'],
|
| 394 |
+
z=data['سن'].map(lambda x: {'P': 0, 'R1': 1, 'R2': 2, 'R3': 3, 'R4': 4, 'R5': 5, 'R6': 6, 'R7': 7, 'R8': 8, 'R9': 9}.get(x, 0)),
|
| 395 |
+
mode='markers',
|
| 396 |
+
marker=dict(
|
| 397 |
+
size=8,
|
| 398 |
+
color=data['اداره'].map({'1': 0, '2': 1, '3': 2, '4': 3, 'دهخدا': 4}),
|
| 399 |
+
colorscale='Viridis',
|
| 400 |
+
opacity=0.8
|
| 401 |
+
),
|
| 402 |
+
text=data['واریته'],
|
| 403 |
+
hovertemplate="<b>واریته:</b> %{text}<br>" +
|
| 404 |
+
"<b>مساحت:</b> %{x:.2f} هکتار<br>" +
|
| 405 |
+
"<b>تولید:</b> %{y:.2f} تن<br>" +
|
| 406 |
+
"<b>سن:</b> %{z}<br>" +
|
| 407 |
+
"<extra></extra>"
|
| 408 |
+
)])
|
| 409 |
+
|
| 410 |
+
# Update layout
|
| 411 |
+
fig.update_layout(
|
| 412 |
+
title='رابطه بین مساحت، تولید و سن',
|
| 413 |
+
scene=dict(
|
| 414 |
+
xaxis_title='مساحت (هکتار)',
|
| 415 |
+
yaxis_title='تولید (تن)',
|
| 416 |
+
zaxis_title='سن',
|
| 417 |
+
zaxis=dict(
|
| 418 |
+
ticktext=['P', 'R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8', 'R9'],
|
| 419 |
+
tickvals=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
| 420 |
+
)
|
| 421 |
+
),
|
| 422 |
+
width=1000,
|
| 423 |
+
height=800,
|
| 424 |
+
font=dict(family="Vazirmatn, Arial", size=14)
|
| 425 |
+
)
|
| 426 |
+
|
| 427 |
+
# Save the figure
|
| 428 |
+
fig.write_html('visualizations/3d_scatter_plot.html')
|
| 429 |
+
fig.write_image('visualizations/3d_scatter_plot.png', scale=2)
|
| 430 |
+
|
| 431 |
+
return fig
|
| 432 |
+
|
| 433 |
+
# Generate all visualizations
|
| 434 |
+
def generate_all_visualizations():
|
| 435 |
+
print("Generating 3D Surface Histogram for Area...")
|
| 436 |
+
create_3d_surface_area()
|
| 437 |
+
|
| 438 |
+
print("Generating 3D Surface Histogram for Production...")
|
| 439 |
+
create_3d_surface_production()
|
| 440 |
+
|
| 441 |
+
print("Generating Area Chart by Department and Age...")
|
| 442 |
+
create_area_chart_by_dept_age()
|
| 443 |
+
|
| 444 |
+
print("Generating Area Chart by Department and Variety...")
|
| 445 |
+
create_area_chart_by_dept_variety()
|
| 446 |
+
|
| 447 |
+
print("Generating 3D Bar Chart...")
|
| 448 |
+
create_3d_bar_chart()
|
| 449 |
+
|
| 450 |
+
print("Generating Heatmap for Area...")
|
| 451 |
+
create_heatmap_area()
|
| 452 |
+
|
| 453 |
+
print("Generating Heatmap for Production...")
|
| 454 |
+
create_heatmap_production()
|
| 455 |
+
|
| 456 |
+
print("Generating Stacked Bar Chart...")
|
| 457 |
+
create_stacked_bar_chart()
|
| 458 |
+
|
| 459 |
+
print("Generating 3D Scatter Plot...")
|
| 460 |
+
create_3d_scatter_plot()
|
| 461 |
+
|
| 462 |
+
print("All visualizations have been generated and saved to the 'visualizations' directory.")
|
| 463 |
+
|
| 464 |
+
# Run the function to generate all visualizations
|
| 465 |
+
if __name__ == "__main__":
|
| 466 |
+
generate_all_visualizations()
|