-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph-03.py
More file actions
66 lines (52 loc) · 2.39 KB
/
Graph-03.py
File metadata and controls
66 lines (52 loc) · 2.39 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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.lines import Line2D
from matplotlib.patches import Patch
# Data
x = np.array(['2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016',
'2017', '2018', '2019', '2020'])
y = np.array([12.4, 29.97, 48.17, 82.62, 165.81, 308.96, 452.93, 580.40,
702.06, 812.67, 894.77, 914.02])
# Custom color map
colors = cm.viridis(np.linspace(0, 1, len(x)))
# Plot
fig, ax = plt.subplots(figsize=(14, 8))
# Bar graph with a custom color gradient and smaller bar width
bars = ax.bar(x, y, color=colors, edgecolor='black', linewidth=0.6, width=0.4)
# Add value labels on each bar
for bar in bars:
height = bar.get_height()
ax.annotate(f'{height:.2f}',
xy=(bar.get_x() + bar.get_width() / 2, height),
xytext=(0, 5), # 5 points vertical offset
textcoords="offset points",
ha='center', va='bottom', fontsize=10, color='black', fontweight='bold')
# Add dots on each bar and line connecting them with a matching color
line_color = 'darkorange'
ax.plot(x, y, 'o-', color=line_color, markersize=8, label='Value Each Year')
# Create a gradient patch for the legend
gradient_patch = Patch(facecolor='black', edgecolor='black', label='Yearly Distribution')
# Improved legend with custom markers
legend_elements = [
Line2D([0], [0], marker='o', color='w', label='Value Each Year', markerfacecolor=line_color, markersize=10),
Patch(facecolor=colors[0], edgecolor='black', label='Start of the year gradient'),
Patch(facecolor=colors[-1], edgecolor='black', label='End of the year gradient')
]
ax.legend(handles=legend_elements, loc='upper left', frameon=True, framealpha=0.9, facecolor='white', edgecolor='black')
# Add finer gridlines
ax.grid(True, which='both', linestyle='--', linewidth=0.5, color='grey', alpha=0.7)
# Add minor ticks for a finer grid
ax.minorticks_on()
ax.grid(which='minor', linestyle=':', linewidth=0.5, color='grey', alpha=0.5)
# Remove top and right spines for a cleaner look
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# Labels and title
ax.set_xlabel('Year', fontsize=14, fontweight='bold')
ax.set_ylabel('Value', fontsize=14, fontweight='bold')
ax.set_title('Yearly Distribution with Values Marked', fontsize=16, fontweight='bold')
# Adjust layout
plt.tight_layout()
# Show the plot
plt.show()