0
# Statistical Plotting
1
2
Interactive statistical plotting capabilities using Plotly for creating bar charts, line charts, histograms, and pie charts integrated with geospatial data workflows and supporting advanced customization options.
3
4
## Capabilities
5
6
### Bar Charts
7
8
Create interactive bar charts with support for grouped bars, horizontal orientation, and extensive customization options.
9
10
```python { .api }
11
def bar_chart(data, x, y, color=None, **kwargs):
12
"""
13
Create interactive bar chart.
14
15
Args:
16
data (pd.DataFrame): Input data
17
x (str): Column name for x-axis (categories)
18
y (str): Column name for y-axis (values)
19
color (str): Column name for color grouping
20
**kwargs: Chart options (title, orientation, etc.)
21
22
Returns:
23
plotly.graph_objects.Figure: Interactive bar chart
24
"""
25
26
def horizontal_bar_chart(data, x, y, **kwargs):
27
"""
28
Create horizontal bar chart.
29
30
Args:
31
data (pd.DataFrame): Input data
32
x (str): Column name for values
33
y (str): Column name for categories
34
**kwargs: Chart customization options
35
36
Returns:
37
plotly.graph_objects.Figure: Interactive horizontal bar chart
38
"""
39
40
def grouped_bar_chart(data, x, y, color, **kwargs):
41
"""
42
Create grouped bar chart.
43
44
Args:
45
data (pd.DataFrame): Input data
46
x (str): Column name for x-axis categories
47
y (str): Column name for y-axis values
48
color (str): Column name for grouping
49
**kwargs: Chart options (barmode, etc.)
50
51
Returns:
52
plotly.graph_objects.Figure: Interactive grouped bar chart
53
"""
54
```
55
56
### Line Charts
57
58
Create line charts for time series data and continuous variables with support for multiple lines and advanced styling.
59
60
```python { .api }
61
def line_chart(data, x, y, color=None, **kwargs):
62
"""
63
Create interactive line chart.
64
65
Args:
66
data (pd.DataFrame): Input data
67
x (str): Column name for x-axis (usually time or continuous variable)
68
y (str): Column name for y-axis (values)
69
color (str): Column name for multiple lines
70
**kwargs: Chart options (title, line_shape, etc.)
71
72
Returns:
73
plotly.graph_objects.Figure: Interactive line chart
74
"""
75
76
def time_series_chart(data, time_col, value_col, **kwargs):
77
"""
78
Create time series chart.
79
80
Args:
81
data (pd.DataFrame): Input data with time series
82
time_col (str): Column name for time/date values
83
value_col (str): Column name for values
84
**kwargs: Time series specific options (range_selector, etc.)
85
86
Returns:
87
plotly.graph_objects.Figure: Interactive time series chart
88
"""
89
90
def multi_line_chart(data, x, y_columns, **kwargs):
91
"""
92
Create chart with multiple y-value lines.
93
94
Args:
95
data (pd.DataFrame): Input data
96
x (str): Column name for x-axis
97
y_columns (list): List of column names for multiple lines
98
**kwargs: Multi-line chart options
99
100
Returns:
101
plotly.graph_objects.Figure: Interactive multi-line chart
102
"""
103
```
104
105
### Histograms
106
107
Create histograms for data distribution analysis with support for overlaid distributions and statistical annotations.
108
109
```python { .api }
110
def histogram(data, column, **kwargs):
111
"""
112
Create interactive histogram.
113
114
Args:
115
data (pd.DataFrame): Input data
116
column (str): Column name for histogram values
117
**kwargs: Histogram options (nbins, histnorm, etc.)
118
119
Returns:
120
plotly.graph_objects.Figure: Interactive histogram
121
"""
122
123
def overlay_histogram(data, column, group_by, **kwargs):
124
"""
125
Create overlaid histograms for different groups.
126
127
Args:
128
data (pd.DataFrame): Input data
129
column (str): Column name for histogram values
130
group_by (str): Column name for grouping
131
**kwargs: Overlay options (opacity, barmode, etc.)
132
133
Returns:
134
plotly.graph_objects.Figure: Interactive overlaid histogram
135
"""
136
137
def box_plot(data, x, y, **kwargs):
138
"""
139
Create interactive box plot.
140
141
Args:
142
data (pd.DataFrame): Input data
143
x (str): Column name for categories (optional)
144
y (str): Column name for values
145
**kwargs: Box plot options (points, etc.)
146
147
Returns:
148
plotly.graph_objects.Figure: Interactive box plot
149
"""
150
```
151
152
### Pie Charts
153
154
Create pie charts for categorical data visualization with support for nested charts and custom styling.
155
156
```python { .api }
157
def pie_chart(data, names, values, **kwargs):
158
"""
159
Create interactive pie chart.
160
161
Args:
162
data (pd.DataFrame): Input data
163
names (str): Column name for category labels
164
values (str): Column name for values
165
**kwargs: Pie chart options (hole, pull, etc.)
166
167
Returns:
168
plotly.graph_objects.Figure: Interactive pie chart
169
"""
170
171
def donut_chart(data, names, values, **kwargs):
172
"""
173
Create donut chart (pie chart with hole).
174
175
Args:
176
data (pd.DataFrame): Input data
177
names (str): Column name for category labels
178
values (str): Column name for values
179
**kwargs: Donut chart options (hole size, etc.)
180
181
Returns:
182
plotly.graph_objects.Figure: Interactive donut chart
183
"""
184
185
def sunburst_chart(data, path, values, **kwargs):
186
"""
187
Create hierarchical sunburst chart.
188
189
Args:
190
data (pd.DataFrame): Input data
191
path (list): List of column names for hierarchy levels
192
values (str): Column name for values
193
**kwargs: Sunburst chart options
194
195
Returns:
196
plotly.graph_objects.Figure: Interactive sunburst chart
197
"""
198
```
199
200
### Scatter Plots
201
202
Create scatter plots for correlation analysis and pattern identification with advanced interaction features.
203
204
```python { .api }
205
def scatter_plot(data, x, y, color=None, size=None, **kwargs):
206
"""
207
Create interactive scatter plot.
208
209
Args:
210
data (pd.DataFrame): Input data
211
x (str): Column name for x-axis values
212
y (str): Column name for y-axis values
213
color (str): Column name for color mapping
214
size (str): Column name for marker size
215
**kwargs: Scatter plot options (trendline, etc.)
216
217
Returns:
218
plotly.graph_objects.Figure: Interactive scatter plot
219
"""
220
221
def bubble_chart(data, x, y, size, color=None, **kwargs):
222
"""
223
Create bubble chart (scatter plot with variable sizes).
224
225
Args:
226
data (pd.DataFrame): Input data
227
x (str): Column name for x-axis values
228
y (str): Column name for y-axis values
229
size (str): Column name for bubble sizes
230
color (str): Column name for color mapping
231
**kwargs: Bubble chart options
232
233
Returns:
234
plotly.graph_objects.Figure: Interactive bubble chart
235
"""
236
237
def correlation_matrix(data, **kwargs):
238
"""
239
Create correlation matrix heatmap.
240
241
Args:
242
data (pd.DataFrame): Input data
243
**kwargs: Heatmap options (colorscale, annotations, etc.)
244
245
Returns:
246
plotly.graph_objects.Figure: Interactive correlation heatmap
247
"""
248
```
249
250
### Statistical Analysis Charts
251
252
Create advanced statistical visualization including distribution fitting and confidence intervals.
253
254
```python { .api }
255
def violin_plot(data, x, y, **kwargs):
256
"""
257
Create violin plot showing distribution shapes.
258
259
Args:
260
data (pd.DataFrame): Input data
261
x (str): Column name for categories
262
y (str): Column name for values
263
**kwargs: Violin plot options (points, box, etc.)
264
265
Returns:
266
plotly.graph_objects.Figure: Interactive violin plot
267
"""
268
269
def density_plot(data, column, **kwargs):
270
"""
271
Create density plot for continuous variable.
272
273
Args:
274
data (pd.DataFrame): Input data
275
column (str): Column name for density calculation
276
**kwargs: Density plot options (bandwidth, etc.)
277
278
Returns:
279
plotly.graph_objects.Figure: Interactive density plot
280
"""
281
282
def qq_plot(data, column, **kwargs):
283
"""
284
Create Q-Q plot for normality testing.
285
286
Args:
287
data (pd.DataFrame): Input data
288
column (str): Column name for Q-Q plot
289
**kwargs: Q-Q plot options
290
291
Returns:
292
plotly.graph_objects.Figure: Interactive Q-Q plot
293
"""
294
```
295
296
## Usage Examples
297
298
### Basic Chart Creation
299
300
```python
301
import leafmap
302
import pandas as pd
303
304
# Load sample data
305
data = pd.read_csv('sample_data.csv')
306
307
# Create bar chart
308
bar_fig = leafmap.bar_chart(
309
data=data,
310
x='category',
311
y='value',
312
title='Sample Bar Chart'
313
)
314
315
# Display chart
316
bar_fig.show()
317
318
# Create line chart for time series
319
line_fig = leafmap.line_chart(
320
data=data,
321
x='date',
322
y='temperature',
323
title='Temperature Over Time'
324
)
325
326
line_fig.show()
327
```
328
329
### Geospatial Data Analysis with Charts
330
331
```python
332
import leafmap
333
import geopandas as gpd
334
335
# Load geospatial data
336
gdf = gpd.read_file('census_data.shp')
337
338
# Create histogram of population density
339
hist_fig = leafmap.histogram(
340
data=gdf,
341
column='pop_density',
342
title='Population Density Distribution',
343
nbins=30
344
)
345
346
# Create scatter plot of income vs education
347
scatter_fig = leafmap.scatter_plot(
348
data=gdf,
349
x='median_income',
350
y='college_rate',
351
color='region',
352
title='Income vs Education by Region',
353
trendline='ols' # Add regression line
354
)
355
356
# Display charts
357
hist_fig.show()
358
scatter_fig.show()
359
360
# Also display data on map
361
m = leafmap.Map()
362
m.add_gdf(gdf,
363
column='pop_density',
364
scheme='quantiles',
365
cmap='viridis',
366
legend=True)
367
m
368
```
369
370
### Multi-Variable Analysis
371
372
```python
373
import leafmap
374
import pandas as pd
375
376
# Load environmental data
377
env_data = pd.read_csv('environmental_monitoring.csv')
378
379
# Create grouped bar chart by station
380
grouped_bar = leafmap.grouped_bar_chart(
381
data=env_data,
382
x='month',
383
y='pollution_level',
384
color='station_id',
385
title='Monthly Pollution Levels by Station'
386
)
387
388
# Create multi-line time series
389
multi_line = leafmap.multi_line_chart(
390
data=env_data,
391
x='date',
392
y_columns=['temperature', 'humidity', 'air_quality'],
393
title='Environmental Parameters Over Time'
394
)
395
396
# Create correlation matrix
397
correlation_fig = leafmap.correlation_matrix(
398
data=env_data[['temperature', 'humidity', 'air_quality', 'pollution_level']],
399
title='Environmental Parameter Correlations'
400
)
401
402
# Display all charts
403
grouped_bar.show()
404
multi_line.show()
405
correlation_fig.show()
406
```
407
408
### Statistical Distribution Analysis
409
410
```python
411
import leafmap
412
import pandas as pd
413
import numpy as np
414
415
# Generate sample data
416
np.random.seed(42)
417
sample_data = pd.DataFrame({
418
'group': np.repeat(['A', 'B', 'C'], 100),
419
'values': np.concatenate([
420
np.random.normal(10, 2, 100), # Group A
421
np.random.normal(15, 3, 100), # Group B
422
np.random.normal(12, 1.5, 100) # Group C
423
])
424
})
425
426
# Create overlaid histograms
427
overlay_hist = leafmap.overlay_histogram(
428
data=sample_data,
429
column='values',
430
group_by='group',
431
title='Distribution Comparison by Group',
432
opacity=0.7
433
)
434
435
# Create box plot
436
box_fig = leafmap.box_plot(
437
data=sample_data,
438
x='group',
439
y='values',
440
title='Value Distribution by Group',
441
points='outliers'
442
)
443
444
# Create violin plot
445
violin_fig = leafmap.violin_plot(
446
data=sample_data,
447
x='group',
448
y='values',
449
title='Distribution Shapes by Group',
450
box=True,
451
points='outliers'
452
)
453
454
# Display statistical plots
455
overlay_hist.show()
456
box_fig.show()
457
violin_fig.show()
458
```
459
460
### Dashboard Creation
461
462
```python
463
import leafmap
464
import pandas as pd
465
from plotly.subplots import make_subplots
466
467
# Load comprehensive dataset
468
data = pd.read_csv('comprehensive_data.csv')
469
470
# Create subplot dashboard
471
fig = make_subplots(
472
rows=2, cols=2,
473
subplot_titles=['Bar Chart', 'Line Chart', 'Histogram', 'Pie Chart'],
474
specs=[[{'type': 'bar'}, {'type': 'scatter'}],
475
[{'type': 'histogram'}, {'type': 'pie'}]]
476
)
477
478
# Add bar chart
479
bar_trace = leafmap.bar_chart(data, x='category', y='count')
480
fig.add_trace(bar_trace.data[0], row=1, col=1)
481
482
# Add line chart
483
line_trace = leafmap.line_chart(data, x='date', y='trend')
484
fig.add_trace(line_trace.data[0], row=1, col=2)
485
486
# Add histogram
487
hist_trace = leafmap.histogram(data, column='distribution')
488
fig.add_trace(hist_trace.data[0], row=2, col=1)
489
490
# Add pie chart
491
pie_trace = leafmap.pie_chart(data, names='segment', values='proportion')
492
fig.add_trace(pie_trace.data[0], row=2, col=2)
493
494
# Update layout
495
fig.update_layout(
496
title='Comprehensive Data Dashboard',
497
height=800,
498
showlegend=False
499
)
500
501
fig.show()
502
```
503
504
### Integration with Map Visualizations
505
506
```python
507
import leafmap
508
import geopandas as gpd
509
510
# Load spatial data
511
cities = gpd.read_file('cities.shp')
512
513
# Create map visualization
514
m = leafmap.Map(center=[40, -100], zoom=4)
515
m.add_gdf(cities,
516
column='population',
517
scheme='quantiles',
518
legend=True,
519
layer_name='Cities by Population')
520
521
# Create supporting charts
522
# Population distribution
523
pop_hist = leafmap.histogram(
524
data=cities,
525
column='population',
526
title='City Population Distribution'
527
)
528
529
# Regional breakdown
530
region_pie = leafmap.pie_chart(
531
data=cities.groupby('region')['population'].sum().reset_index(),
532
names='region',
533
values='population',
534
title='Population by Region'
535
)
536
537
# Population vs Area relationship
538
scatter_fig = leafmap.scatter_plot(
539
data=cities,
540
x='area',
541
y='population',
542
color='region',
543
title='Population vs Area by Region',
544
size='gdp_per_capita'
545
)
546
547
# Display map and charts together
548
m
549
pop_hist.show()
550
region_pie.show()
551
scatter_fig.show()
552
```
553
554
## Chart Customization Options
555
556
### Styling Options
557
558
```python
559
chart_styling = {
560
'title': 'Chart Title', # Chart title
561
'title_font_size': 16, # Title font size
562
'width': 800, # Chart width
563
'height': 600, # Chart height
564
'template': 'plotly_white', # Color template
565
'color_discrete_sequence': ['#FF6B6B', '#4ECDC4', '#45B7D1'], # Custom colors
566
'font_family': 'Arial', # Font family
567
'font_size': 12 # Font size
568
}
569
```
570
571
### Axis Options
572
573
```python
574
axis_options = {
575
'xaxis_title': 'X Axis Label', # X-axis label
576
'yaxis_title': 'Y Axis Label', # Y-axis label
577
'xaxis_tickangle': -45, # X-axis tick angle
578
'yaxis_type': 'log', # Y-axis scale (linear/log)
579
'xaxis_range': [0, 100], # X-axis range
580
'yaxis_range': [0, 200], # Y-axis range
581
'showgrid': True, # Show grid lines
582
'zeroline': True # Show zero line
583
}
584
```
585
586
### Interactive Options
587
588
```python
589
interactive_options = {
590
'hovermode': 'x unified', # Hover behavior
591
'dragmode': 'zoom', # Drag behavior
592
'showlegend': True, # Show legend
593
'legend_orientation': 'h', # Legend orientation
594
'margin': dict(l=50, r=50, t=50, b=50), # Chart margins
595
'annotations': [], # Custom annotations
596
'shapes': [] # Custom shapes
597
}
598
```
599
600
### Export Options
601
602
```python
603
export_options = {
604
'format': 'png', # Export format (png, pdf, svg, html)
605
'width': 1200, # Export width
606
'height': 800, # Export height
607
'scale': 2, # Scale factor
608
'engine': 'kaleido' # Rendering engine
609
}
610
```
611
612
## Advanced Features
613
614
### Statistical Annotations
615
616
- **Trend lines**: Linear, polynomial, and LOWESS regression
617
- **Confidence intervals**: Statistical uncertainty visualization
618
- **R-squared values**: Correlation strength indicators
619
- **P-values**: Statistical significance testing
620
621
### Interactive Features
622
623
- **Zoom and pan**: Navigate large datasets
624
- **Hover tooltips**: Detailed information on demand
625
- **Crossfilter**: Linked brushing across multiple charts
626
- **Animation**: Time-based data exploration
627
628
### Data Processing
629
630
- **Aggregation**: Automatic grouping and summarization
631
- **Filtering**: Interactive data subset selection
632
- **Sorting**: Dynamic data ordering
633
- **Binning**: Automatic histogram bin selection