-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_generator.py
More file actions
76 lines (64 loc) · 2.2 KB
/
Copy pathplot_generator.py
File metadata and controls
76 lines (64 loc) · 2.2 KB
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
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Create a sample DataFrame
data = {
'Year': [2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024],
'Sales': [100, 120, 150, 180, 200, 220, 250, 280, 300, 320],
'Expenses': [80, 90, 100, 110, 120, 130, 140, 150, 160, 170],
'Customers': [50, 60, 75, 90, 100, 110, 125, 140, 150, 160],
'Category': ['A', 'B', 'A', 'C', 'B', 'A', 'C', 'A', 'B', 'C']
}
df = pd.DataFrame(data)
# 1. Line Plot: Sales and Expenses Over Time
plt.figure(figsize=(10, 6))
plt.plot(df['Year'], df['Sales'], marker='o', label='Sales')
plt.plot(df['Year'], df['Expenses'], marker='x', label='Expenses')
plt.title('Sales and Expenses Over Time')
plt.xlabel('Year')
plt.ylabel('Amount (in thousands)')
plt.legend()
plt.grid(True)
plt.savefig('line_plot.png')
plt.close()
print("Generated line_plot.png")
# 2. Bar Chart: Sales per Year
plt.figure(figsize=(10, 6))
plt.bar(df['Year'], df['Sales'], color='skyblue')
plt.title('Sales Per Year')
plt.xlabel('Year')
plt.ylabel('Sales (in thousands)')
plt.xticks(df['Year'])
plt.savefig('bar_chart.png')
plt.close()
print("Generated bar_chart.png")
# 3. Scatter Plot: Customers vs. Sales
plt.figure(figsize=(10, 6))
plt.scatter(df['Customers'], df['Sales'], c=df['Year'], cmap='viridis', s=100, alpha=0.7)
plt.title('Customers vs. Sales')
plt.xlabel('Number of Customers')
plt.ylabel('Sales (in thousands)')
cbar = plt.colorbar()
cbar.set_label('Year')
plt.grid(True)
plt.savefig('scatter_plot.png')
plt.close()
print("Generated scatter_plot.png")
# 4. Histogram: Distribution of Sales
plt.figure(figsize=(10, 6))
plt.hist(df['Sales'], bins=5, color='lightgreen', edgecolor='black')
plt.title('Distribution of Sales')
plt.xlabel('Sales (in thousands)')
plt.ylabel('Frequency')
plt.savefig('histogram.png')
plt.close()
print("Generated histogram.png")
# 5. Pie Chart: Sales by Category
category_sales = df.groupby('Category')['Sales'].sum()
plt.figure(figsize=(8, 8))
plt.pie(category_sales, labels=category_sales.index, autopct='%1.1f%%', startangle=140, colors=['#ff9999','#66b3ff','#99ff99'])
plt.title('Sales by Category')
plt.ylabel('') # Hide the y-label
plt.savefig('pie_chart.png')
plt.close()
print("Generated pie_chart.png")