-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault.py
More file actions
176 lines (150 loc) · 5.37 KB
/
default.py
File metadata and controls
176 lines (150 loc) · 5.37 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
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
"""
area-basic: Basic Area Chart
Implementation for: plotly
Variant: default
Python: 3.10+
"""
from typing import TYPE_CHECKING, Optional
import pandas as pd
import plotly.graph_objects as go
if TYPE_CHECKING:
pass
def create_plot(
data: pd.DataFrame,
x: str,
y: str,
title: Optional[str] = None,
xlabel: Optional[str] = None,
ylabel: Optional[str] = None,
color: str = "rgba(99, 110, 250, 0.5)",
line_color: Optional[str] = None,
line_width: float = 2.0,
fill_to: str = "tozeroy",
height: int = 900,
width: int = 1600,
**kwargs,
) -> go.Figure:
"""
Create a basic area chart showing quantitative data over a continuous interval.
The area between the line and the x-axis is filled with color, emphasizing
the magnitude of values. Ideal for showing trends and cumulative totals.
Args:
data: Input DataFrame with required columns
x: Column name for x-axis values (typically time or sequential data)
y: Column name for y-axis values (numeric)
title: Plot title (optional)
xlabel: Custom x-axis label (optional, defaults to column name)
ylabel: Custom y-axis label (optional, defaults to column name)
color: Fill color for the area with alpha (default: semi-transparent blue)
line_color: Color of the line at top of area (default: derived from fill color)
line_width: Width of the line (default: 2.0)
fill_to: Fill mode - 'tozeroy', 'tonexty', 'none' (default: 'tozeroy')
height: Figure height in pixels (default: 900)
width: Figure width in pixels (default: 1600)
**kwargs: Additional parameters passed to plotly Scatter trace
Returns:
Plotly Figure object
Raises:
ValueError: If data is empty
KeyError: If required columns not found
Example:
>>> data = pd.DataFrame({
... 'Month': ['Jan', 'Feb', 'Mar', 'Apr', 'May'],
... 'Sales': [100, 150, 130, 180, 200]
... })
>>> fig = create_plot(data, x='Month', y='Sales', title='Monthly Sales')
"""
# Input validation
if data.empty:
raise ValueError("Data cannot be empty")
# Check required columns
for col in [x, y]:
if col not in data.columns:
available = ", ".join(data.columns)
raise KeyError(f"Column '{col}' not found. Available columns: {available}")
# Derive line color from fill color if not provided
if line_color is None:
# Use a solid version of the fill color (darker)
line_color = "rgb(99, 110, 250)"
# Create the figure
fig = go.Figure()
# Add the area trace
fig.add_trace(
go.Scatter(
x=data[x],
y=data[y],
mode="lines",
fill=fill_to,
fillcolor=color,
line={"color": line_color, "width": line_width},
name=y,
hovertemplate=f"<b>{x}</b>: %{{x}}<br><b>{y}</b>: %{{y:,.2f}}<extra></extra>",
**kwargs,
)
)
# Update layout for professional appearance
fig.update_layout(
title={
"text": title or "Area Chart",
"font": {"size": 18, "family": "Arial, sans-serif"},
"x": 0.5,
"xanchor": "center",
},
xaxis={
"title": {"text": xlabel or x, "font": {"size": 14}},
"showgrid": True,
"gridcolor": "rgba(128, 128, 128, 0.3)",
"gridwidth": 1,
"zeroline": False,
"showline": True,
"linewidth": 1,
"linecolor": "rgba(128, 128, 128, 0.5)",
},
yaxis={
"title": {"text": ylabel or y, "font": {"size": 14}},
"showgrid": True,
"gridcolor": "rgba(128, 128, 128, 0.3)",
"gridwidth": 1,
"zeroline": True,
"zerolinewidth": 1,
"zerolinecolor": "rgba(128, 128, 128, 0.5)",
"showline": True,
"linewidth": 1,
"linecolor": "rgba(128, 128, 128, 0.5)",
},
plot_bgcolor="white",
paper_bgcolor="white",
height=height,
width=width,
showlegend=False,
hovermode="x unified",
hoverlabel={"bgcolor": "white", "font_size": 12, "font_family": "Arial, sans-serif"},
margin={"l": 80, "r": 40, "t": 80, "b": 60},
)
return fig
if __name__ == "__main__":
import numpy as np
# Sample data: Monthly website traffic over a year
np.random.seed(42)
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
# Generate realistic traffic pattern with seasonal variation
base_traffic = 10000
seasonal_factor = [0.8, 0.85, 0.95, 1.0, 1.1, 1.15, 1.2, 1.25, 1.1, 1.0, 0.9, 0.95]
noise = np.random.normal(0, 500, 12)
traffic = [int(base_traffic * sf + n) for sf, n in zip(seasonal_factor, noise, strict=False)]
data = pd.DataFrame({"Month": months, "Visitors": traffic})
# Create the area chart
fig = create_plot(
data,
x="Month",
y="Visitors",
title="Monthly Website Visitors (2024)",
xlabel="Month",
ylabel="Number of Visitors",
color="rgba(99, 110, 250, 0.4)",
line_color="rgb(99, 110, 250)",
line_width=2.5,
)
# Save as PNG
fig.write_image("plot.png", width=1600, height=900, scale=2)
print("Plot saved to plot.png")