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

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

2341

2342

2343

2344

2345

2346

2347

2348

2349

2350

2351

2352

2353

2354

2355

2356

2357

2358

2359

2360

2361

2362

2363

2364

2365

2366

2367

2368

2369

2370

2371

2372

2373

2374

2375

2376

2377

2378

2379

2380

2381

2382

2383

2384

2385

2386

2387

2388

2389

2390

2391

2392

2393

2394

2395

2396

2397

2398

2399

2400

2401

2402

2403

2404

2405

2406

2407

2408

2409

2410

2411

2412

2413

2414

2415

2416

2417

2418

2419

2420

2421

2422

2423

2424

2425

2426

2427

2428

2429

2430

2431

2432

2433

2434

2435

2436

2437

2438

2439

2440

2441

2442

2443

2444

2445

2446

2447

2448

2449

2450

2451

2452

2453

2454

2455

2456

2457

2458

2459

2460

2461

2462

2463

2464

2465

2466

2467

2468

2469

2470

2471

2472

2473

2474

2475

2476

2477

2478

2479

2480

2481

2482

2483

2484

2485

2486

2487

2488

2489

2490

2491

2492

2493

2494

2495

2496

2497

2498

2499

2500

2501

2502

2503

2504

2505

2506

2507

2508

2509

2510

2511

2512

2513

2514

2515

2516

2517

2518

2519

2520

2521

2522

2523

2524

2525

2526

2527

2528

2529

2530

2531

2532

2533

2534

2535

2536

2537

2538

2539

2540

2541

2542

2543

2544

2545

2546

2547

2548

2549

2550

2551

2552

2553

2554

2555

2556

2557

2558

2559

2560

2561

2562

2563

2564

2565

2566

2567

2568

2569

2570

2571

2572

2573

2574

2575

2576

2577

2578

2579

2580

2581

2582

2583

2584

2585

2586

2587

2588

2589

2590

2591

2592

2593

2594

2595

2596

2597

2598

2599

2600

2601

2602

2603

2604

2605

2606

2607

2608

2609

2610

2611

2612

2613

2614

2615

2616

2617

2618

2619

2620

2621

2622

2623

2624

2625

2626

2627

2628

2629

2630

2631

2632

2633

2634

2635

2636

2637

2638

2639

2640

2641

2642

2643

2644

2645

2646

2647

2648

2649

2650

2651

2652

2653

2654

2655

2656

2657

2658

2659

2660

2661

2662

2663

2664

2665

2666

2667

2668

2669

2670

2671

2672

2673

2674

2675

2676

2677

2678

2679

2680

2681

2682

2683

2684

2685

2686

2687

2688

2689

2690

2691

2692

2693

2694

2695

2696

2697

2698

2699

2700

2701

2702

2703

2704

2705

2706

2707

2708

2709

2710

2711

2712

2713

2714

2715

2716

2717

2718

2719

2720

2721

2722

2723

2724

2725

2726

2727

2728

2729

2730

2731

2732

2733

2734

2735

2736

2737

2738

2739

2740

2741

2742

2743

2744

2745

2746

2747

2748

2749

2750

2751

2752

2753

2754

2755

2756

2757

2758

2759

2760

2761

2762

2763

2764

2765

2766

2767

2768

2769

2770

2771

2772

2773

2774

2775

2776

2777

2778

2779

2780

2781

2782

2783

2784

2785

2786

2787

2788

2789

2790

2791

2792

2793

2794

2795

2796

2797

2798

2799

2800

2801

2802

2803

2804

2805

2806

2807

2808

2809

2810

2811

2812

2813

2814

2815

2816

""" 

GUI neutral widgets 

=================== 

 

Widgets that are designed to work for any of the GUI backends. 

All of these widgets require you to predefine a :class:`matplotlib.axes.Axes` 

instance and pass that as the first arg. matplotlib doesn't try to 

be too smart with respect to layout -- you will have to figure out how 

wide and tall you want your Axes to be to accommodate your widget. 

""" 

 

import copy 

from numbers import Integral 

 

import numpy as np 

 

from . import rcParams 

from .lines import Line2D 

from .patches import Circle, Rectangle, Ellipse 

from .transforms import blended_transform_factory 

 

 

class LockDraw(object): 

""" 

Some widgets, like the cursor, draw onto the canvas, and this is not 

desirable under all circumstances, like when the toolbar is in zoom-to-rect 

mode and drawing a rectangle. To avoid this, a widget can acquire a 

canvas' lock with ``canvas.widgetlock(widget)`` before drawing on the 

canvas; this will prevent other widgets from doing so at the same time (if 

they also try to acquire the lock first). 

""" 

 

def __init__(self): 

self._owner = None 

 

def __call__(self, o): 

"""Reserve the lock for *o*.""" 

if not self.available(o): 

raise ValueError('already locked') 

self._owner = o 

 

def release(self, o): 

"""Release the lock from *o*.""" 

if not self.available(o): 

raise ValueError('you do not own this lock') 

self._owner = None 

 

def available(self, o): 

"""Return whether drawing is available to *o*.""" 

return not self.locked() or self.isowner(o) 

 

def isowner(self, o): 

"""Return whether *o* owns this lock.""" 

return self._owner is o 

 

def locked(self): 

"""Return whether the lock is currently held by an owner.""" 

return self._owner is not None 

 

 

class Widget(object): 

""" 

Abstract base class for GUI neutral widgets 

""" 

drawon = True 

eventson = True 

_active = True 

 

def set_active(self, active): 

"""Set whether the widget is active. 

""" 

self._active = active 

 

def get_active(self): 

"""Get whether the widget is active. 

""" 

return self._active 

 

# set_active is overridden by SelectorWidgets. 

active = property(get_active, lambda self, active: self.set_active(active), 

doc="Is the widget active?") 

 

def ignore(self, event): 

"""Return True if event should be ignored. 

 

This method (or a version of it) should be called at the beginning 

of any event callback. 

""" 

return not self.active 

 

 

class AxesWidget(Widget): 

"""Widget that is connected to a single 

:class:`~matplotlib.axes.Axes`. 

 

To guarantee that the widget remains responsive and not garbage-collected, 

a reference to the object should be maintained by the user. 

 

This is necessary because the callback registry 

maintains only weak-refs to the functions, which are member 

functions of the widget. If there are no references to the widget 

object it may be garbage collected which will disconnect the 

callbacks. 

 

Attributes: 

 

*ax* : :class:`~matplotlib.axes.Axes` 

The parent axes for the widget 

*canvas* : :class:`~matplotlib.backend_bases.FigureCanvasBase` subclass 

The parent figure canvas for the widget. 

*active* : bool 

If False, the widget does not respond to events. 

""" 

def __init__(self, ax): 

self.ax = ax 

self.canvas = ax.figure.canvas 

self.cids = [] 

 

def connect_event(self, event, callback): 

"""Connect callback with an event. 

 

This should be used in lieu of `figure.canvas.mpl_connect` since this 

function stores callback ids for later clean up. 

""" 

cid = self.canvas.mpl_connect(event, callback) 

self.cids.append(cid) 

 

def disconnect_events(self): 

"""Disconnect all events created by this widget.""" 

for c in self.cids: 

self.canvas.mpl_disconnect(c) 

 

 

class Button(AxesWidget): 

""" 

A GUI neutral button. 

 

For the button to remain responsive you must keep a reference to it. 

Call :meth:`on_clicked` to connect to the button. 

 

Attributes 

---------- 

ax : 

The :class:`matplotlib.axes.Axes` the button renders into. 

label : 

A :class:`matplotlib.text.Text` instance. 

color : 

The color of the button when not hovering. 

hovercolor : 

The color of the button when hovering. 

""" 

 

def __init__(self, ax, label, image=None, 

color='0.85', hovercolor='0.95'): 

""" 

Parameters 

---------- 

ax : matplotlib.axes.Axes 

The :class:`matplotlib.axes.Axes` instance the button 

will be placed into. 

 

label : str 

The button text. Accepts string. 

 

image : array, mpl image, Pillow Image 

The image to place in the button, if not *None*. 

Can be any legal arg to imshow (numpy array, 

matplotlib Image instance, or Pillow Image). 

 

color : color 

The color of the button when not activated 

 

hovercolor : color 

The color of the button when the mouse is over it 

""" 

AxesWidget.__init__(self, ax) 

 

if image is not None: 

ax.imshow(image) 

self.label = ax.text(0.5, 0.5, label, 

verticalalignment='center', 

horizontalalignment='center', 

transform=ax.transAxes) 

 

self.cnt = 0 

self.observers = {} 

 

self.connect_event('button_press_event', self._click) 

self.connect_event('button_release_event', self._release) 

self.connect_event('motion_notify_event', self._motion) 

ax.set_navigate(False) 

ax.set_facecolor(color) 

ax.set_xticks([]) 

ax.set_yticks([]) 

self.color = color 

self.hovercolor = hovercolor 

 

self._lastcolor = color 

 

def _click(self, event): 

if self.ignore(event): 

return 

if event.inaxes != self.ax: 

return 

if not self.eventson: 

return 

if event.canvas.mouse_grabber != self.ax: 

event.canvas.grab_mouse(self.ax) 

 

def _release(self, event): 

if self.ignore(event): 

return 

if event.canvas.mouse_grabber != self.ax: 

return 

event.canvas.release_mouse(self.ax) 

if not self.eventson: 

return 

if event.inaxes != self.ax: 

return 

for cid, func in self.observers.items(): 

func(event) 

 

def _motion(self, event): 

if self.ignore(event): 

return 

if event.inaxes == self.ax: 

c = self.hovercolor 

else: 

c = self.color 

if c != self._lastcolor: 

self.ax.set_facecolor(c) 

self._lastcolor = c 

if self.drawon: 

self.ax.figure.canvas.draw() 

 

def on_clicked(self, func): 

""" 

When the button is clicked, call this *func* with event. 

 

A connection id is returned. It can be used to disconnect 

the button from its callback. 

""" 

cid = self.cnt 

self.observers[cid] = func 

self.cnt += 1 

return cid 

 

def disconnect(self, cid): 

"""remove the observer with connection id *cid*""" 

try: 

del self.observers[cid] 

except KeyError: 

pass 

 

 

class Slider(AxesWidget): 

""" 

A slider representing a floating point range. 

 

Create a slider from *valmin* to *valmax* in axes *ax*. For the slider to 

remain responsive you must maintain a reference to it. Call 

:meth:`on_changed` to connect to the slider event. 

 

Attributes 

---------- 

val : float 

Slider value. 

""" 

def __init__(self, ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f', 

closedmin=True, closedmax=True, slidermin=None, 

slidermax=None, dragging=True, valstep=None, **kwargs): 

""" 

Parameters 

---------- 

ax : Axes 

The Axes to put the slider in. 

 

label : str 

Slider label. 

 

valmin : float 

The minimum value of the slider. 

 

valmax : float 

The maximum value of the slider. 

 

valinit : float, optional, default: 0.5 

The slider initial position. 

 

valfmt : str, optional, default: "%1.2f" 

Used to format the slider value, fprint format string. 

 

closedmin : bool, optional, default: True 

Indicate whether the slider interval is closed on the bottom. 

 

closedmax : bool, optional, default: True 

Indicate whether the slider interval is closed on the top. 

 

slidermin : Slider, optional, default: None 

Do not allow the current slider to have a value less than 

the value of the Slider `slidermin`. 

 

slidermax : Slider, optional, default: None 

Do not allow the current slider to have a value greater than 

the value of the Slider `slidermax`. 

 

dragging : bool, optional, default: True 

If True the slider can be dragged by the mouse. 

 

valstep : float, optional, default: None 

If given, the slider will snap to multiples of `valstep`. 

 

Notes 

----- 

Additional kwargs are passed on to ``self.poly`` which is the 

:class:`~matplotlib.patches.Rectangle` that draws the slider 

knob. See the :class:`~matplotlib.patches.Rectangle` documentation for 

valid property names (e.g., `facecolor`, `edgecolor`, `alpha`). 

""" 

AxesWidget.__init__(self, ax) 

 

if slidermin is not None and not hasattr(slidermin, 'val'): 

raise ValueError("Argument slidermin ({}) has no 'val'" 

.format(type(slidermin))) 

if slidermax is not None and not hasattr(slidermax, 'val'): 

raise ValueError("Argument slidermax ({}) has no 'val'" 

.format(type(slidermax))) 

self.closedmin = closedmin 

self.closedmax = closedmax 

self.slidermin = slidermin 

self.slidermax = slidermax 

self.drag_active = False 

self.valmin = valmin 

self.valmax = valmax 

self.valstep = valstep 

valinit = self._value_in_bounds(valinit) 

if valinit is None: 

valinit = valmin 

self.val = valinit 

self.valinit = valinit 

self.poly = ax.axvspan(valmin, valinit, 0, 1, **kwargs) 

self.vline = ax.axvline(valinit, 0, 1, color='r', lw=1) 

 

self.valfmt = valfmt 

ax.set_yticks([]) 

ax.set_xlim((valmin, valmax)) 

ax.set_xticks([]) 

ax.set_navigate(False) 

 

self.connect_event('button_press_event', self._update) 

self.connect_event('button_release_event', self._update) 

if dragging: 

self.connect_event('motion_notify_event', self._update) 

self.label = ax.text(-0.02, 0.5, label, transform=ax.transAxes, 

verticalalignment='center', 

horizontalalignment='right') 

 

self.valtext = ax.text(1.02, 0.5, valfmt % valinit, 

transform=ax.transAxes, 

verticalalignment='center', 

horizontalalignment='left') 

 

self.cnt = 0 

self.observers = {} 

 

self.set_val(valinit) 

 

def _value_in_bounds(self, val): 

""" Makes sure self.val is with given bounds.""" 

if self.valstep: 

val = np.round((val - self.valmin)/self.valstep)*self.valstep 

val += self.valmin 

 

if val <= self.valmin: 

if not self.closedmin: 

return 

val = self.valmin 

elif val >= self.valmax: 

if not self.closedmax: 

return 

val = self.valmax 

 

if self.slidermin is not None and val <= self.slidermin.val: 

if not self.closedmin: 

return 

val = self.slidermin.val 

 

if self.slidermax is not None and val >= self.slidermax.val: 

if not self.closedmax: 

return 

val = self.slidermax.val 

return val 

 

def _update(self, event): 

"""update the slider position""" 

if self.ignore(event): 

return 

 

if event.button != 1: 

return 

 

if event.name == 'button_press_event' and event.inaxes == self.ax: 

self.drag_active = True 

event.canvas.grab_mouse(self.ax) 

 

if not self.drag_active: 

return 

 

elif ((event.name == 'button_release_event') or 

(event.name == 'button_press_event' and 

event.inaxes != self.ax)): 

self.drag_active = False 

event.canvas.release_mouse(self.ax) 

return 

val = self._value_in_bounds(event.xdata) 

if val not in [None, self.val]: 

self.set_val(val) 

 

def set_val(self, val): 

""" 

Set slider value to *val* 

 

Parameters 

---------- 

val : float 

""" 

xy = self.poly.xy 

xy[2] = val, 1 

xy[3] = val, 0 

self.poly.xy = xy 

self.valtext.set_text(self.valfmt % val) 

if self.drawon: 

self.ax.figure.canvas.draw_idle() 

self.val = val 

if not self.eventson: 

return 

for cid, func in self.observers.items(): 

func(val) 

 

def on_changed(self, func): 

""" 

When the slider value is changed call *func* with the new 

slider value 

 

Parameters 

---------- 

func : callable 

Function to call when slider is changed. 

The function must accept a single float as its arguments. 

 

Returns 

------- 

cid : int 

Connection id (which can be used to disconnect *func*) 

""" 

cid = self.cnt 

self.observers[cid] = func 

self.cnt += 1 

return cid 

 

def disconnect(self, cid): 

""" 

Remove the observer with connection id *cid* 

 

Parameters 

---------- 

cid : int 

Connection id of the observer to be removed 

""" 

try: 

del self.observers[cid] 

except KeyError: 

pass 

 

def reset(self): 

"""Reset the slider to the initial value""" 

if self.val != self.valinit: 

self.set_val(self.valinit) 

 

 

class CheckButtons(AxesWidget): 

""" 

A GUI neutral set of check buttons. 

 

For the check buttons to remain responsive you must keep a 

reference to this object. 

 

The following attributes are exposed 

 

*ax* 

The :class:`matplotlib.axes.Axes` instance the buttons are 

located in 

 

*labels* 

List of :class:`matplotlib.text.Text` instances 

 

*lines* 

List of (line1, line2) tuples for the x's in the check boxes. 

These lines exist for each box, but have ``set_visible(False)`` 

when its box is not checked. 

 

*rectangles* 

List of :class:`matplotlib.patches.Rectangle` instances 

 

Connect to the CheckButtons with the :meth:`on_clicked` method 

""" 

def __init__(self, ax, labels, actives=None): 

""" 

Add check buttons to :class:`matplotlib.axes.Axes` instance *ax* 

 

Parameters 

---------- 

ax : `~matplotlib.axes.Axes` 

The parent axes for the widget. 

 

labels : List[str] 

The labels of the check buttons. 

 

actives : List[bool], optional 

The initial check states of the buttons. The list must have the 

same length as *labels*. If not given, all buttons are unchecked. 

""" 

AxesWidget.__init__(self, ax) 

 

ax.set_xticks([]) 

ax.set_yticks([]) 

ax.set_navigate(False) 

 

if actives is None: 

actives = [False] * len(labels) 

 

if len(labels) > 1: 

dy = 1. / (len(labels) + 1) 

ys = np.linspace(1 - dy, dy, len(labels)) 

else: 

dy = 0.25 

ys = [0.5] 

 

axcolor = ax.get_facecolor() 

 

self.labels = [] 

self.lines = [] 

self.rectangles = [] 

 

lineparams = {'color': 'k', 'linewidth': 1.25, 

'transform': ax.transAxes, 'solid_capstyle': 'butt'} 

for y, label, active in zip(ys, labels, actives): 

t = ax.text(0.25, y, label, transform=ax.transAxes, 

horizontalalignment='left', 

verticalalignment='center') 

 

w, h = dy / 2, dy / 2 

x, y = 0.05, y - h / 2 

 

p = Rectangle(xy=(x, y), width=w, height=h, edgecolor='black', 

facecolor=axcolor, transform=ax.transAxes) 

 

l1 = Line2D([x, x + w], [y + h, y], **lineparams) 

l2 = Line2D([x, x + w], [y, y + h], **lineparams) 

 

l1.set_visible(active) 

l2.set_visible(active) 

self.labels.append(t) 

self.rectangles.append(p) 

self.lines.append((l1, l2)) 

ax.add_patch(p) 

ax.add_line(l1) 

ax.add_line(l2) 

 

self.connect_event('button_press_event', self._clicked) 

 

self.cnt = 0 

self.observers = {} 

 

def _clicked(self, event): 

if self.ignore(event) or event.button != 1 or event.inaxes != self.ax: 

return 

for i, (p, t) in enumerate(zip(self.rectangles, self.labels)): 

if (t.get_window_extent().contains(event.x, event.y) or 

p.get_window_extent().contains(event.x, event.y)): 

self.set_active(i) 

break 

 

def set_active(self, index): 

""" 

Directly (de)activate a check button by index. 

 

*index* is an index into the original label list 

that this object was constructed with. 

Raises ValueError if *index* is invalid. 

 

Callbacks will be triggered if :attr:`eventson` is True. 

 

""" 

if 0 > index >= len(self.labels): 

raise ValueError("Invalid CheckButton index: %d" % index) 

 

l1, l2 = self.lines[index] 

l1.set_visible(not l1.get_visible()) 

l2.set_visible(not l2.get_visible()) 

 

if self.drawon: 

self.ax.figure.canvas.draw() 

 

if not self.eventson: 

return 

for cid, func in self.observers.items(): 

func(self.labels[index].get_text()) 

 

def get_status(self): 

""" 

returns a tuple of the status (True/False) of all of the check buttons 

""" 

return [l1.get_visible() for (l1, l2) in self.lines] 

 

def on_clicked(self, func): 

""" 

When the button is clicked, call *func* with button label 

 

A connection id is returned which can be used to disconnect 

""" 

cid = self.cnt 

self.observers[cid] = func 

self.cnt += 1 

return cid 

 

def disconnect(self, cid): 

"""remove the observer with connection id *cid*""" 

try: 

del self.observers[cid] 

except KeyError: 

pass 

 

 

class TextBox(AxesWidget): 

""" 

A GUI neutral text input box. 

 

For the text box to remain responsive you must keep a reference to it. 

 

The following attributes are accessible: 

 

*ax* 

The :class:`matplotlib.axes.Axes` the button renders into. 

 

*label* 

A :class:`matplotlib.text.Text` instance. 

 

*color* 

The color of the text box when not hovering. 

 

*hovercolor* 

The color of the text box when hovering. 

 

Call :meth:`on_text_change` to be updated whenever the text changes. 

 

Call :meth:`on_submit` to be updated whenever the user hits enter or 

leaves the text entry field. 

""" 

 

def __init__(self, ax, label, initial='', 

color='.95', hovercolor='1', label_pad=.01): 

""" 

Parameters 

---------- 

ax : matplotlib.axes.Axes 

The :class:`matplotlib.axes.Axes` instance the button 

will be placed into. 

 

label : str 

Label for this text box. Accepts string. 

 

initial : str 

Initial value in the text box 

 

color : color 

The color of the box 

 

hovercolor : color 

The color of the box when the mouse is over it 

 

label_pad : float 

the distance between the label and the right side of the textbox 

""" 

AxesWidget.__init__(self, ax) 

 

self.DIST_FROM_LEFT = .05 

 

self.params_to_disable = [key for key in rcParams if 'keymap' in key] 

 

self.text = initial 

self.label = ax.text(-label_pad, 0.5, label, 

verticalalignment='center', 

horizontalalignment='right', 

transform=ax.transAxes) 

self.text_disp = self._make_text_disp(self.text) 

 

self.cnt = 0 

self.change_observers = {} 

self.submit_observers = {} 

 

# If these lines are removed, the cursor won't appear the first 

# time the box is clicked: 

self.ax.set_xlim(0, 1) 

self.ax.set_ylim(0, 1) 

 

self.cursor_index = 0 

 

# Because this is initialized, _render_cursor 

# can assume that cursor exists. 

self.cursor = self.ax.vlines(0, 0, 0) 

self.cursor.set_visible(False) 

 

self.connect_event('button_press_event', self._click) 

self.connect_event('button_release_event', self._release) 

self.connect_event('motion_notify_event', self._motion) 

self.connect_event('key_press_event', self._keypress) 

self.connect_event('resize_event', self._resize) 

ax.set_navigate(False) 

ax.set_facecolor(color) 

ax.set_xticks([]) 

ax.set_yticks([]) 

self.color = color 

self.hovercolor = hovercolor 

 

self._lastcolor = color 

 

self.capturekeystrokes = False 

 

def _make_text_disp(self, string): 

return self.ax.text(self.DIST_FROM_LEFT, 0.5, string, 

verticalalignment='center', 

horizontalalignment='left', 

transform=self.ax.transAxes) 

 

def _rendercursor(self): 

# this is a hack to figure out where the cursor should go. 

# we draw the text up to where the cursor should go, measure 

# and save its dimensions, draw the real text, then put the cursor 

# at the saved dimensions 

 

widthtext = self.text[:self.cursor_index] 

no_text = False 

if(widthtext == "" or widthtext == " " or widthtext == " "): 

no_text = widthtext == "" 

widthtext = "," 

 

wt_disp = self._make_text_disp(widthtext) 

 

self.ax.figure.canvas.draw() 

bb = wt_disp.get_window_extent() 

inv = self.ax.transData.inverted() 

bb = inv.transform(bb) 

wt_disp.set_visible(False) 

if no_text: 

bb[1, 0] = bb[0, 0] 

# hack done 

self.cursor.set_visible(False) 

 

self.cursor = self.ax.vlines(bb[1, 0], bb[0, 1], bb[1, 1]) 

self.ax.figure.canvas.draw() 

 

def _notify_submit_observers(self): 

for cid, func in self.submit_observers.items(): 

func(self.text) 

 

def _release(self, event): 

if self.ignore(event): 

return 

if event.canvas.mouse_grabber != self.ax: 

return 

event.canvas.release_mouse(self.ax) 

 

def _keypress(self, event): 

if self.ignore(event): 

return 

if self.capturekeystrokes: 

key = event.key 

 

if(len(key) == 1): 

self.text = (self.text[:self.cursor_index] + key + 

self.text[self.cursor_index:]) 

self.cursor_index += 1 

elif key == "right": 

if self.cursor_index != len(self.text): 

self.cursor_index += 1 

elif key == "left": 

if self.cursor_index != 0: 

self.cursor_index -= 1 

elif key == "home": 

self.cursor_index = 0 

elif key == "end": 

self.cursor_index = len(self.text) 

elif(key == "backspace"): 

if self.cursor_index != 0: 

self.text = (self.text[:self.cursor_index - 1] + 

self.text[self.cursor_index:]) 

self.cursor_index -= 1 

elif(key == "delete"): 

if self.cursor_index != len(self.text): 

self.text = (self.text[:self.cursor_index] + 

self.text[self.cursor_index + 1:]) 

 

self.text_disp.remove() 

self.text_disp = self._make_text_disp(self.text) 

self._rendercursor() 

self._notify_change_observers() 

if key == "enter": 

self._notify_submit_observers() 

 

def set_val(self, val): 

newval = str(val) 

if self.text == newval: 

return 

self.text = newval 

self.text_disp.remove() 

self.text_disp = self._make_text_disp(self.text) 

self._rendercursor() 

self._notify_change_observers() 

self._notify_submit_observers() 

 

def _notify_change_observers(self): 

for cid, func in self.change_observers.items(): 

func(self.text) 

 

def begin_typing(self, x): 

self.capturekeystrokes = True 

# disable command keys so that the user can type without 

# command keys causing figure to be saved, etc 

self.reset_params = {} 

for key in self.params_to_disable: 

self.reset_params[key] = rcParams[key] 

rcParams[key] = [] 

 

def stop_typing(self): 

notifysubmit = False 

# because _notify_submit_users might throw an error in the 

# user's code, we only want to call it once we've already done 

# our cleanup. 

if self.capturekeystrokes: 

# since the user is no longer typing, 

# reactivate the standard command keys 

for key in self.params_to_disable: 

rcParams[key] = self.reset_params[key] 

notifysubmit = True 

self.capturekeystrokes = False 

self.cursor.set_visible(False) 

self.ax.figure.canvas.draw() 

if notifysubmit: 

self._notify_submit_observers() 

 

def position_cursor(self, x): 

# now, we have to figure out where the cursor goes. 

# approximate it based on assuming all characters the same length 

if len(self.text) == 0: 

self.cursor_index = 0 

else: 

bb = self.text_disp.get_window_extent() 

 

trans = self.ax.transData 

inv = self.ax.transData.inverted() 

bb = trans.transform(inv.transform(bb)) 

 

text_start = bb[0, 0] 

text_end = bb[1, 0] 

 

ratio = (x - text_start) / (text_end - text_start) 

 

if ratio < 0: 

ratio = 0 

if ratio > 1: 

ratio = 1 

 

self.cursor_index = int(len(self.text) * ratio) 

 

self._rendercursor() 

 

def _click(self, event): 

if self.ignore(event): 

return 

if event.inaxes != self.ax: 

self.stop_typing() 

return 

if not self.eventson: 

return 

if event.canvas.mouse_grabber != self.ax: 

event.canvas.grab_mouse(self.ax) 

if not self.capturekeystrokes: 

self.begin_typing(event.x) 

self.position_cursor(event.x) 

 

def _resize(self, event): 

self.stop_typing() 

 

def _motion(self, event): 

if self.ignore(event): 

return 

if event.inaxes == self.ax: 

c = self.hovercolor 

else: 

c = self.color 

if c != self._lastcolor: 

self.ax.set_facecolor(c) 

self._lastcolor = c 

if self.drawon: 

self.ax.figure.canvas.draw() 

 

def on_text_change(self, func): 

""" 

When the text changes, call this *func* with event. 

 

A connection id is returned which can be used to disconnect. 

""" 

cid = self.cnt 

self.change_observers[cid] = func 

self.cnt += 1 

return cid 

 

def on_submit(self, func): 

""" 

When the user hits enter or leaves the submission box, call this 

*func* with event. 

 

A connection id is returned which can be used to disconnect. 

""" 

cid = self.cnt 

self.submit_observers[cid] = func 

self.cnt += 1 

return cid 

 

def disconnect(self, cid): 

"""Remove the observer with connection id *cid*.""" 

for reg in [self.change_observers, self.submit_observers]: 

try: 

del reg[cid] 

except KeyError: 

pass 

 

 

class RadioButtons(AxesWidget): 

""" 

A GUI neutral radio button. 

 

For the buttons to remain responsive 

you must keep a reference to this object. 

 

The following attributes are exposed: 

 

*ax* 

The :class:`matplotlib.axes.Axes` instance the buttons are in 

 

*activecolor* 

The color of the button when clicked 

 

*labels* 

A list of :class:`matplotlib.text.Text` instances 

 

*circles* 

A list of :class:`matplotlib.patches.Circle` instances 

 

*value_selected* 

A string listing the current value selected 

 

Connect to the RadioButtons with the :meth:`on_clicked` method 

""" 

def __init__(self, ax, labels, active=0, activecolor='blue'): 

""" 

Add radio buttons to :class:`matplotlib.axes.Axes` instance *ax* 

 

*labels* 

A len(buttons) list of labels as strings 

 

*active* 

The index into labels for the button that is active 

 

*activecolor* 

The color of the button when clicked 

""" 

AxesWidget.__init__(self, ax) 

self.activecolor = activecolor 

self.value_selected = None 

 

ax.set_xticks([]) 

ax.set_yticks([]) 

ax.set_navigate(False) 

dy = 1. / (len(labels) + 1) 

ys = np.linspace(1 - dy, dy, len(labels)) 

cnt = 0 

axcolor = ax.get_facecolor() 

 

# scale the radius of the circle with the spacing between each one 

circle_radius = (dy / 2) - 0.01 

 

# defaul to hard-coded value if the radius becomes too large 

if(circle_radius > 0.05): 

circle_radius = 0.05 

 

self.labels = [] 

self.circles = [] 

for y, label in zip(ys, labels): 

t = ax.text(0.25, y, label, transform=ax.transAxes, 

horizontalalignment='left', 

verticalalignment='center') 

 

if cnt == active: 

self.value_selected = label 

facecolor = activecolor 

else: 

facecolor = axcolor 

 

p = Circle(xy=(0.15, y), radius=circle_radius, edgecolor='black', 

facecolor=facecolor, transform=ax.transAxes) 

 

self.labels.append(t) 

self.circles.append(p) 

ax.add_patch(p) 

cnt += 1 

 

self.connect_event('button_press_event', self._clicked) 

 

self.cnt = 0 

self.observers = {} 

 

def _clicked(self, event): 

if self.ignore(event) or event.button != 1 or event.inaxes != self.ax: 

return 

xy = self.ax.transAxes.inverted().transform_point((event.x, event.y)) 

pclicked = np.array([xy[0], xy[1]]) 

for i, (p, t) in enumerate(zip(self.circles, self.labels)): 

if (t.get_window_extent().contains(event.x, event.y) 

or np.linalg.norm(pclicked - p.center) < p.radius): 

self.set_active(i) 

break 

 

def set_active(self, index): 

""" 

Trigger which radio button to make active. 

 

*index* is an index into the original label list 

that this object was constructed with. 

Raise ValueError if the index is invalid. 

 

Callbacks will be triggered if :attr:`eventson` is True. 

 

""" 

if 0 > index >= len(self.labels): 

raise ValueError("Invalid RadioButton index: %d" % index) 

 

self.value_selected = self.labels[index].get_text() 

 

for i, p in enumerate(self.circles): 

if i == index: 

color = self.activecolor 

else: 

color = self.ax.get_facecolor() 

p.set_facecolor(color) 

 

if self.drawon: 

self.ax.figure.canvas.draw() 

 

if not self.eventson: 

return 

for cid, func in self.observers.items(): 

func(self.labels[index].get_text()) 

 

def on_clicked(self, func): 

""" 

When the button is clicked, call *func* with button label 

 

A connection id is returned which can be used to disconnect 

""" 

cid = self.cnt 

self.observers[cid] = func 

self.cnt += 1 

return cid 

 

def disconnect(self, cid): 

"""remove the observer with connection id *cid*""" 

try: 

del self.observers[cid] 

except KeyError: 

pass 

 

 

class SubplotTool(Widget): 

""" 

A tool to adjust the subplot params of a :class:`matplotlib.figure.Figure`. 

""" 

def __init__(self, targetfig, toolfig): 

""" 

*targetfig* 

The figure instance to adjust. 

 

*toolfig* 

The figure instance to embed the subplot tool into. If 

*None*, a default figure will be created. If you are using 

this from the GUI 

""" 

# FIXME: The docstring seems to just abruptly end without... 

 

self.targetfig = targetfig 

toolfig.subplots_adjust(left=0.2, right=0.9) 

 

class toolbarfmt: 

def __init__(self, slider): 

self.slider = slider 

 

def __call__(self, x, y): 

fmt = '%s=%s' % (self.slider.label.get_text(), 

self.slider.valfmt) 

return fmt % x 

 

self.axleft = toolfig.add_subplot(711) 

self.axleft.set_title('Click on slider to adjust subplot param') 

self.axleft.set_navigate(False) 

 

self.sliderleft = Slider(self.axleft, 'left', 

0, 1, targetfig.subplotpars.left, 

closedmax=False) 

self.sliderleft.on_changed(self.funcleft) 

 

self.axbottom = toolfig.add_subplot(712) 

self.axbottom.set_navigate(False) 

self.sliderbottom = Slider(self.axbottom, 

'bottom', 0, 1, 

targetfig.subplotpars.bottom, 

closedmax=False) 

self.sliderbottom.on_changed(self.funcbottom) 

 

self.axright = toolfig.add_subplot(713) 

self.axright.set_navigate(False) 

self.sliderright = Slider(self.axright, 'right', 0, 1, 

targetfig.subplotpars.right, 

closedmin=False) 

self.sliderright.on_changed(self.funcright) 

 

self.axtop = toolfig.add_subplot(714) 

self.axtop.set_navigate(False) 

self.slidertop = Slider(self.axtop, 'top', 0, 1, 

targetfig.subplotpars.top, 

closedmin=False) 

self.slidertop.on_changed(self.functop) 

 

self.axwspace = toolfig.add_subplot(715) 

self.axwspace.set_navigate(False) 

self.sliderwspace = Slider(self.axwspace, 'wspace', 

0, 1, targetfig.subplotpars.wspace, 

closedmax=False) 

self.sliderwspace.on_changed(self.funcwspace) 

 

self.axhspace = toolfig.add_subplot(716) 

self.axhspace.set_navigate(False) 

self.sliderhspace = Slider(self.axhspace, 'hspace', 

0, 1, targetfig.subplotpars.hspace, 

closedmax=False) 

self.sliderhspace.on_changed(self.funchspace) 

 

# constraints 

self.sliderleft.slidermax = self.sliderright 

self.sliderright.slidermin = self.sliderleft 

self.sliderbottom.slidermax = self.slidertop 

self.slidertop.slidermin = self.sliderbottom 

 

bax = toolfig.add_axes([0.8, 0.05, 0.15, 0.075]) 

self.buttonreset = Button(bax, 'Reset') 

 

sliders = (self.sliderleft, self.sliderbottom, self.sliderright, 

self.slidertop, self.sliderwspace, self.sliderhspace,) 

 

def func(event): 

thisdrawon = self.drawon 

 

self.drawon = False 

 

# store the drawon state of each slider 

bs = [] 

for slider in sliders: 

bs.append(slider.drawon) 

slider.drawon = False 

 

# reset the slider to the initial position 

for slider in sliders: 

slider.reset() 

 

# reset drawon 

for slider, b in zip(sliders, bs): 

slider.drawon = b 

 

# draw the canvas 

self.drawon = thisdrawon 

if self.drawon: 

toolfig.canvas.draw() 

self.targetfig.canvas.draw() 

 

# during reset there can be a temporary invalid state 

# depending on the order of the reset so we turn off 

# validation for the resetting 

validate = toolfig.subplotpars.validate 

toolfig.subplotpars.validate = False 

self.buttonreset.on_clicked(func) 

toolfig.subplotpars.validate = validate 

 

def funcleft(self, val): 

self.targetfig.subplots_adjust(left=val) 

if self.drawon: 

self.targetfig.canvas.draw() 

 

def funcright(self, val): 

self.targetfig.subplots_adjust(right=val) 

if self.drawon: 

self.targetfig.canvas.draw() 

 

def funcbottom(self, val): 

self.targetfig.subplots_adjust(bottom=val) 

if self.drawon: 

self.targetfig.canvas.draw() 

 

def functop(self, val): 

self.targetfig.subplots_adjust(top=val) 

if self.drawon: 

self.targetfig.canvas.draw() 

 

def funcwspace(self, val): 

self.targetfig.subplots_adjust(wspace=val) 

if self.drawon: 

self.targetfig.canvas.draw() 

 

def funchspace(self, val): 

self.targetfig.subplots_adjust(hspace=val) 

if self.drawon: 

self.targetfig.canvas.draw() 

 

 

class Cursor(AxesWidget): 

""" 

A horizontal and vertical line that spans the axes and moves with 

the pointer. You can turn off the hline or vline respectively with 

the following attributes: 

 

*horizOn* 

Controls the visibility of the horizontal line 

 

*vertOn* 

Controls the visibility of the horizontal line 

 

and the visibility of the cursor itself with the *visible* attribute. 

 

For the cursor to remain responsive you must keep a reference to 

it. 

""" 

def __init__(self, ax, horizOn=True, vertOn=True, useblit=False, 

**lineprops): 

""" 

Add a cursor to *ax*. If ``useblit=True``, use the backend-dependent 

blitting features for faster updates. *lineprops* is a dictionary of 

line properties. 

""" 

AxesWidget.__init__(self, ax) 

 

self.connect_event('motion_notify_event', self.onmove) 

self.connect_event('draw_event', self.clear) 

 

self.visible = True 

self.horizOn = horizOn 

self.vertOn = vertOn 

self.useblit = useblit and self.canvas.supports_blit 

 

if self.useblit: 

lineprops['animated'] = True 

self.lineh = ax.axhline(ax.get_ybound()[0], visible=False, **lineprops) 

self.linev = ax.axvline(ax.get_xbound()[0], visible=False, **lineprops) 

 

self.background = None 

self.needclear = False 

 

def clear(self, event): 

"""clear the cursor""" 

if self.ignore(event): 

return 

if self.useblit: 

self.background = self.canvas.copy_from_bbox(self.ax.bbox) 

self.linev.set_visible(False) 

self.lineh.set_visible(False) 

 

def onmove(self, event): 

"""on mouse motion draw the cursor if visible""" 

if self.ignore(event): 

return 

if not self.canvas.widgetlock.available(self): 

return 

if event.inaxes != self.ax: 

self.linev.set_visible(False) 

self.lineh.set_visible(False) 

 

if self.needclear: 

self.canvas.draw() 

self.needclear = False 

return 

self.needclear = True 

if not self.visible: 

return 

self.linev.set_xdata((event.xdata, event.xdata)) 

 

self.lineh.set_ydata((event.ydata, event.ydata)) 

self.linev.set_visible(self.visible and self.vertOn) 

self.lineh.set_visible(self.visible and self.horizOn) 

 

self._update() 

 

def _update(self): 

 

if self.useblit: 

if self.background is not None: 

self.canvas.restore_region(self.background) 

self.ax.draw_artist(self.linev) 

self.ax.draw_artist(self.lineh) 

self.canvas.blit(self.ax.bbox) 

else: 

 

self.canvas.draw_idle() 

 

return False 

 

 

class MultiCursor(Widget): 

""" 

Provide a vertical (default) and/or horizontal line cursor shared between 

multiple axes. 

 

For the cursor to remain responsive you must keep a reference to 

it. 

 

Example usage:: 

 

from matplotlib.widgets import MultiCursor 

import matplotlib.pyplot as plt 

import numpy as np 

 

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True) 

t = np.arange(0.0, 2.0, 0.01) 

ax1.plot(t, np.sin(2*np.pi*t)) 

ax2.plot(t, np.sin(4*np.pi*t)) 

 

multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1, 

horizOn=False, vertOn=True) 

plt.show() 

 

""" 

def __init__(self, canvas, axes, useblit=True, horizOn=False, vertOn=True, 

**lineprops): 

 

self.canvas = canvas 

self.axes = axes 

self.horizOn = horizOn 

self.vertOn = vertOn 

 

xmin, xmax = axes[-1].get_xlim() 

ymin, ymax = axes[-1].get_ylim() 

xmid = 0.5 * (xmin + xmax) 

ymid = 0.5 * (ymin + ymax) 

 

self.visible = True 

self.useblit = useblit and self.canvas.supports_blit 

self.background = None 

self.needclear = False 

 

if self.useblit: 

lineprops['animated'] = True 

 

if vertOn: 

self.vlines = [ax.axvline(xmid, visible=False, **lineprops) 

for ax in axes] 

else: 

self.vlines = [] 

 

if horizOn: 

self.hlines = [ax.axhline(ymid, visible=False, **lineprops) 

for ax in axes] 

else: 

self.hlines = [] 

 

self.connect() 

 

def connect(self): 

"""connect events""" 

self._cidmotion = self.canvas.mpl_connect('motion_notify_event', 

self.onmove) 

self._ciddraw = self.canvas.mpl_connect('draw_event', self.clear) 

 

def disconnect(self): 

"""disconnect events""" 

self.canvas.mpl_disconnect(self._cidmotion) 

self.canvas.mpl_disconnect(self._ciddraw) 

 

def clear(self, event): 

"""clear the cursor""" 

if self.ignore(event): 

return 

if self.useblit: 

self.background = ( 

self.canvas.copy_from_bbox(self.canvas.figure.bbox)) 

for line in self.vlines + self.hlines: 

line.set_visible(False) 

 

def onmove(self, event): 

if self.ignore(event): 

return 

if event.inaxes is None: 

return 

if not self.canvas.widgetlock.available(self): 

return 

self.needclear = True 

if not self.visible: 

return 

if self.vertOn: 

for line in self.vlines: 

line.set_xdata((event.xdata, event.xdata)) 

line.set_visible(self.visible) 

if self.horizOn: 

for line in self.hlines: 

line.set_ydata((event.ydata, event.ydata)) 

line.set_visible(self.visible) 

self._update() 

 

def _update(self): 

if self.useblit: 

if self.background is not None: 

self.canvas.restore_region(self.background) 

if self.vertOn: 

for ax, line in zip(self.axes, self.vlines): 

ax.draw_artist(line) 

if self.horizOn: 

for ax, line in zip(self.axes, self.hlines): 

ax.draw_artist(line) 

self.canvas.blit(self.canvas.figure.bbox) 

else: 

self.canvas.draw_idle() 

 

 

class _SelectorWidget(AxesWidget): 

 

def __init__(self, ax, onselect, useblit=False, button=None, 

state_modifier_keys=None): 

AxesWidget.__init__(self, ax) 

 

self.visible = True 

self.onselect = onselect 

self.useblit = useblit and self.canvas.supports_blit 

self.connect_default_events() 

 

self.state_modifier_keys = dict(move=' ', clear='escape', 

square='shift', center='control') 

self.state_modifier_keys.update(state_modifier_keys or {}) 

 

self.background = None 

self.artists = [] 

 

if isinstance(button, Integral): 

self.validButtons = [button] 

else: 

self.validButtons = button 

 

# will save the data (position at mouseclick) 

self.eventpress = None 

# will save the data (pos. at mouserelease) 

self.eventrelease = None 

self._prev_event = None 

self.state = set() 

 

def set_active(self, active): 

AxesWidget.set_active(self, active) 

if active: 

self.update_background(None) 

 

def update_background(self, event): 

"""force an update of the background""" 

# If you add a call to `ignore` here, you'll want to check edge case: 

# `release` can call a draw event even when `ignore` is True. 

if self.useblit: 

self.background = self.canvas.copy_from_bbox(self.ax.bbox) 

 

def connect_default_events(self): 

"""Connect the major canvas events to methods.""" 

self.connect_event('motion_notify_event', self.onmove) 

self.connect_event('button_press_event', self.press) 

self.connect_event('button_release_event', self.release) 

self.connect_event('draw_event', self.update_background) 

self.connect_event('key_press_event', self.on_key_press) 

self.connect_event('key_release_event', self.on_key_release) 

self.connect_event('scroll_event', self.on_scroll) 

 

def ignore(self, event): 

"""return *True* if *event* should be ignored""" 

if not self.active or not self.ax.get_visible(): 

return True 

 

# If canvas was locked 

if not self.canvas.widgetlock.available(self): 

return True 

 

if not hasattr(event, 'button'): 

event.button = None 

 

# Only do rectangle selection if event was triggered 

# with a desired button 

if self.validButtons is not None: 

if event.button not in self.validButtons: 

return True 

 

# If no button was pressed yet ignore the event if it was out 

# of the axes 

if self.eventpress is None: 

return event.inaxes != self.ax 

 

# If a button was pressed, check if the release-button is the 

# same. 

if event.button == self.eventpress.button: 

return False 

 

# If a button was pressed, check if the release-button is the 

# same. 

return (event.inaxes != self.ax or 

event.button != self.eventpress.button) 

 

def update(self): 

"""draw using newfangled blit or oldfangled draw depending on 

useblit 

 

""" 

if not self.ax.get_visible(): 

return False 

 

if self.useblit: 

if self.background is not None: 

self.canvas.restore_region(self.background) 

for artist in self.artists: 

self.ax.draw_artist(artist) 

 

self.canvas.blit(self.ax.bbox) 

 

else: 

self.canvas.draw_idle() 

return False 

 

def _get_data(self, event): 

"""Get the xdata and ydata for event, with limits""" 

if event.xdata is None: 

return None, None 

x0, x1 = self.ax.get_xbound() 

y0, y1 = self.ax.get_ybound() 

xdata = max(x0, event.xdata) 

xdata = min(x1, xdata) 

ydata = max(y0, event.ydata) 

ydata = min(y1, ydata) 

return xdata, ydata 

 

def _clean_event(self, event): 

"""Clean up an event 

 

Use prev event if there is no xdata 

Limit the xdata and ydata to the axes limits 

Set the prev event 

""" 

if event.xdata is None: 

event = self._prev_event 

else: 

event = copy.copy(event) 

event.xdata, event.ydata = self._get_data(event) 

 

self._prev_event = event 

return event 

 

def press(self, event): 

"""Button press handler and validator""" 

if not self.ignore(event): 

event = self._clean_event(event) 

self.eventpress = event 

self._prev_event = event 

key = event.key or '' 

key = key.replace('ctrl', 'control') 

# move state is locked in on a button press 

if key == self.state_modifier_keys['move']: 

self.state.add('move') 

self._press(event) 

return True 

return False 

 

def _press(self, event): 

"""Button press handler""" 

pass 

 

def release(self, event): 

"""Button release event handler and validator""" 

if not self.ignore(event) and self.eventpress: 

event = self._clean_event(event) 

self.eventrelease = event 

self._release(event) 

self.eventpress = None 

self.eventrelease = None 

self.state.discard('move') 

return True 

return False 

 

def _release(self, event): 

"""Button release event handler""" 

pass 

 

def onmove(self, event): 

"""Cursor move event handler and validator""" 

if not self.ignore(event) and self.eventpress: 

event = self._clean_event(event) 

self._onmove(event) 

return True 

return False 

 

def _onmove(self, event): 

"""Cursor move event handler""" 

pass 

 

def on_scroll(self, event): 

"""Mouse scroll event handler and validator""" 

if not self.ignore(event): 

self._on_scroll(event) 

 

def _on_scroll(self, event): 

"""Mouse scroll event handler""" 

pass 

 

def on_key_press(self, event): 

"""Key press event handler and validator for all selection widgets""" 

if self.active: 

key = event.key or '' 

key = key.replace('ctrl', 'control') 

if key == self.state_modifier_keys['clear']: 

for artist in self.artists: 

artist.set_visible(False) 

self.update() 

return 

for (state, modifier) in self.state_modifier_keys.items(): 

if modifier in key: 

self.state.add(state) 

self._on_key_press(event) 

 

def _on_key_press(self, event): 

"""Key press event handler - use for widget-specific key press actions. 

""" 

pass 

 

def on_key_release(self, event): 

"""Key release event handler and validator""" 

if self.active: 

key = event.key or '' 

for (state, modifier) in self.state_modifier_keys.items(): 

if modifier in key: 

self.state.discard(state) 

self._on_key_release(event) 

 

def _on_key_release(self, event): 

"""Key release event handler""" 

pass 

 

def set_visible(self, visible): 

""" Set the visibility of our artists """ 

self.visible = visible 

for artist in self.artists: 

artist.set_visible(visible) 

 

 

class SpanSelector(_SelectorWidget): 

""" 

Visually select a min/max range on a single axis and call a function with 

those values. 

 

To guarantee that the selector remains responsive, keep a reference to it. 

 

In order to turn off the SpanSelector, set `span_selector.active=False`. To 

turn it back on, set `span_selector.active=True`. 

 

Parameters 

---------- 

ax : :class:`matplotlib.axes.Axes` object 

 

onselect : func(min, max), min/max are floats 

 

direction : "horizontal" or "vertical" 

The axis along which to draw the span selector 

 

minspan : float, default is None 

If selection is less than *minspan*, do not call *onselect* 

 

useblit : bool, default is False 

If True, use the backend-dependent blitting features for faster 

canvas updates. 

 

rectprops : dict, default is None 

Dictionary of :class:`matplotlib.patches.Patch` properties 

 

onmove_callback : func(min, max), min/max are floats, default is None 

Called on mouse move while the span is being selected 

 

span_stays : bool, default is False 

If True, the span stays visible after the mouse is released 

 

button : int or list of ints 

Determines which mouse buttons activate the span selector 

1 = left mouse button\n 

2 = center mouse button (scroll wheel)\n 

3 = right mouse button\n 

 

Examples 

-------- 

>>> import matplotlib.pyplot as plt 

>>> import matplotlib.widgets as mwidgets 

>>> fig, ax = plt.subplots() 

>>> ax.plot([1, 2, 3], [10, 50, 100]) 

>>> def onselect(vmin, vmax): 

... print(vmin, vmax) 

>>> rectprops = dict(facecolor='blue', alpha=0.5) 

>>> span = mwidgets.SpanSelector(ax, onselect, 'horizontal', 

... rectprops=rectprops) 

>>> fig.show() 

 

See also: :doc:`/gallery/widgets/span_selector` 

 

""" 

 

def __init__(self, ax, onselect, direction, minspan=None, useblit=False, 

rectprops=None, onmove_callback=None, span_stays=False, 

button=None): 

 

_SelectorWidget.__init__(self, ax, onselect, useblit=useblit, 

button=button) 

 

if rectprops is None: 

rectprops = dict(facecolor='red', alpha=0.5) 

 

rectprops['animated'] = self.useblit 

 

if direction not in ['horizontal', 'vertical']: 

raise ValueError("direction must be 'horizontal' or 'vertical'") 

self.direction = direction 

 

self.rect = None 

self.pressv = None 

 

self.rectprops = rectprops 

self.onmove_callback = onmove_callback 

self.minspan = minspan 

self.span_stays = span_stays 

 

# Needed when dragging out of axes 

self.prev = (0, 0) 

 

# Reset canvas so that `new_axes` connects events. 

self.canvas = None 

self.new_axes(ax) 

 

def new_axes(self, ax): 

"""Set SpanSelector to operate on a new Axes""" 

self.ax = ax 

if self.canvas is not ax.figure.canvas: 

if self.canvas is not None: 

self.disconnect_events() 

 

self.canvas = ax.figure.canvas 

self.connect_default_events() 

 

if self.direction == 'horizontal': 

trans = blended_transform_factory(self.ax.transData, 

self.ax.transAxes) 

w, h = 0, 1 

else: 

trans = blended_transform_factory(self.ax.transAxes, 

self.ax.transData) 

w, h = 1, 0 

self.rect = Rectangle((0, 0), w, h, 

transform=trans, 

visible=False, 

**self.rectprops) 

if self.span_stays: 

self.stay_rect = Rectangle((0, 0), w, h, 

transform=trans, 

visible=False, 

**self.rectprops) 

self.stay_rect.set_animated(False) 

self.ax.add_patch(self.stay_rect) 

 

self.ax.add_patch(self.rect) 

self.artists = [self.rect] 

 

def ignore(self, event): 

"""return *True* if *event* should be ignored""" 

return _SelectorWidget.ignore(self, event) or not self.visible 

 

def _press(self, event): 

"""on button press event""" 

self.rect.set_visible(self.visible) 

if self.span_stays: 

self.stay_rect.set_visible(False) 

# really force a draw so that the stay rect is not in 

# the blit background 

if self.useblit: 

self.canvas.draw() 

xdata, ydata = self._get_data(event) 

if self.direction == 'horizontal': 

self.pressv = xdata 

else: 

self.pressv = ydata 

 

self._set_span_xy(event) 

return False 

 

def _release(self, event): 

"""on button release event""" 

if self.pressv is None: 

return 

self.buttonDown = False 

 

self.rect.set_visible(False) 

 

if self.span_stays: 

self.stay_rect.set_x(self.rect.get_x()) 

self.stay_rect.set_y(self.rect.get_y()) 

self.stay_rect.set_width(self.rect.get_width()) 

self.stay_rect.set_height(self.rect.get_height()) 

self.stay_rect.set_visible(True) 

 

self.canvas.draw_idle() 

vmin = self.pressv 

xdata, ydata = self._get_data(event) 

if self.direction == 'horizontal': 

vmax = xdata or self.prev[0] 

else: 

vmax = ydata or self.prev[1] 

 

if vmin > vmax: 

vmin, vmax = vmax, vmin 

span = vmax - vmin 

if self.minspan is not None and span < self.minspan: 

return 

self.onselect(vmin, vmax) 

self.pressv = None 

return False 

 

def _onmove(self, event): 

"""on motion notify event""" 

if self.pressv is None: 

return 

 

self._set_span_xy(event) 

 

if self.onmove_callback is not None: 

vmin = self.pressv 

xdata, ydata = self._get_data(event) 

if self.direction == 'horizontal': 

vmax = xdata or self.prev[0] 

else: 

vmax = ydata or self.prev[1] 

 

if vmin > vmax: 

vmin, vmax = vmax, vmin 

self.onmove_callback(vmin, vmax) 

 

self.update() 

return False 

 

def _set_span_xy(self, event): 

"""Setting the span coordinates""" 

x, y = self._get_data(event) 

if x is None: 

return 

 

self.prev = x, y 

if self.direction == 'horizontal': 

v = x 

else: 

v = y 

 

minv, maxv = v, self.pressv 

if minv > maxv: 

minv, maxv = maxv, minv 

if self.direction == 'horizontal': 

self.rect.set_x(minv) 

self.rect.set_width(maxv - minv) 

else: 

self.rect.set_y(minv) 

self.rect.set_height(maxv - minv) 

 

 

class ToolHandles(object): 

"""Control handles for canvas tools. 

 

Parameters 

---------- 

ax : :class:`matplotlib.axes.Axes` 

Matplotlib axes where tool handles are displayed. 

x, y : 1D arrays 

Coordinates of control handles. 

marker : str 

Shape of marker used to display handle. See `matplotlib.pyplot.plot`. 

marker_props : dict 

Additional marker properties. See :class:`matplotlib.lines.Line2D`. 

""" 

 

def __init__(self, ax, x, y, marker='o', marker_props=None, useblit=True): 

self.ax = ax 

 

props = dict(marker=marker, markersize=7, mfc='w', ls='none', 

alpha=0.5, visible=False, label='_nolegend_') 

props.update(marker_props if marker_props is not None else {}) 

self._markers = Line2D(x, y, animated=useblit, **props) 

self.ax.add_line(self._markers) 

self.artist = self._markers 

 

@property 

def x(self): 

return self._markers.get_xdata() 

 

@property 

def y(self): 

return self._markers.get_ydata() 

 

def set_data(self, pts, y=None): 

"""Set x and y positions of handles""" 

if y is not None: 

x = pts 

pts = np.array([x, y]) 

self._markers.set_data(pts) 

 

def set_visible(self, val): 

self._markers.set_visible(val) 

 

def set_animated(self, val): 

self._markers.set_animated(val) 

 

def closest(self, x, y): 

"""Return index and pixel distance to closest index.""" 

pts = np.transpose((self.x, self.y)) 

# Transform data coordinates to pixel coordinates. 

pts = self.ax.transData.transform(pts) 

diff = pts - ((x, y)) 

if diff.ndim == 2: 

dist = np.sqrt(np.sum(diff ** 2, axis=1)) 

return np.argmin(dist), np.min(dist) 

else: 

return 0, np.sqrt(np.sum(diff ** 2)) 

 

 

class RectangleSelector(_SelectorWidget): 

""" 

Select a rectangular region of an axes. 

 

For the cursor to remain responsive you must keep a reference to 

it. 

 

Example usage:: 

 

import numpy as np 

import matplotlib.pyplot as plt 

from matplotlib.widgets import RectangleSelector 

 

def onselect(eclick, erelease): 

"eclick and erelease are matplotlib events at press and release." 

print('startposition: (%f, %f)' % (eclick.xdata, eclick.ydata)) 

print('endposition : (%f, %f)' % (erelease.xdata, erelease.ydata)) 

print('used button : ', eclick.button) 

 

def toggle_selector(event): 

print('Key pressed.') 

if event.key in ['Q', 'q'] and toggle_selector.RS.active: 

print('RectangleSelector deactivated.') 

toggle_selector.RS.set_active(False) 

if event.key in ['A', 'a'] and not toggle_selector.RS.active: 

print('RectangleSelector activated.') 

toggle_selector.RS.set_active(True) 

 

x = np.arange(100.) / 99 

y = np.sin(x) 

fig, ax = plt.subplots() 

ax.plot(x, y) 

 

toggle_selector.RS = RectangleSelector(ax, onselect, drawtype='line') 

fig.canvas.connect('key_press_event', toggle_selector) 

plt.show() 

""" 

 

_shape_klass = Rectangle 

 

def __init__(self, ax, onselect, drawtype='box', 

minspanx=None, minspany=None, useblit=False, 

lineprops=None, rectprops=None, spancoords='data', 

button=None, maxdist=10, marker_props=None, 

interactive=False, state_modifier_keys=None): 

 

""" 

Create a selector in *ax*. When a selection is made, clear 

the span and call onselect with:: 

 

onselect(pos_1, pos_2) 

 

and clear the drawn box/line. The ``pos_1`` and ``pos_2`` are 

arrays of length 2 containing the x- and y-coordinate. 

 

If *minspanx* is not *None* then events smaller than *minspanx* 

in x direction are ignored (it's the same for y). 

 

The rectangle is drawn with *rectprops*; default:: 

 

rectprops = dict(facecolor='red', edgecolor = 'black', 

alpha=0.2, fill=True) 

 

The line is drawn with *lineprops*; default:: 

 

lineprops = dict(color='black', linestyle='-', 

linewidth = 2, alpha=0.5) 

 

Use *drawtype* if you want the mouse to draw a line, 

a box or nothing between click and actual position by setting 

 

``drawtype = 'line'``, ``drawtype='box'`` or ``drawtype = 'none'``. 

Drawing a line would result in a line from vertex A to vertex C in 

a rectangle ABCD. 

 

*spancoords* is one of 'data' or 'pixels'. If 'data', *minspanx* 

and *minspanx* will be interpreted in the same coordinates as 

the x and y axis. If 'pixels', they are in pixels. 

 

*button* is a list of integers indicating which mouse buttons should 

be used for rectangle selection. You can also specify a single 

integer if only a single button is desired. Default is *None*, 

which does not limit which button can be used. 

 

Note, typically: 

1 = left mouse button 

2 = center mouse button (scroll wheel) 

3 = right mouse button 

 

*interactive* will draw a set of handles and allow you interact 

with the widget after it is drawn. 

 

*state_modifier_keys* are keyboard modifiers that affect the behavior 

of the widget. 

 

The defaults are: 

dict(move=' ', clear='escape', square='shift', center='ctrl') 

 

Keyboard modifiers, which: 

'move': Move the existing shape. 

'clear': Clear the current shape. 

'square': Makes the shape square. 

'center': Make the initial point the center of the shape. 

'square' and 'center' can be combined. 

""" 

_SelectorWidget.__init__(self, ax, onselect, useblit=useblit, 

button=button, 

state_modifier_keys=state_modifier_keys) 

 

self.to_draw = None 

self.visible = True 

self.interactive = interactive 

 

if drawtype == 'none': 

drawtype = 'line' # draw a line but make it 

self.visible = False # invisible 

 

if drawtype == 'box': 

if rectprops is None: 

rectprops = dict(facecolor='red', edgecolor='black', 

alpha=0.2, fill=True) 

rectprops['animated'] = self.useblit 

self.rectprops = rectprops 

self.to_draw = self._shape_klass((0, 0), 0, 1, visible=False, 

**self.rectprops) 

self.ax.add_patch(self.to_draw) 

if drawtype == 'line': 

if lineprops is None: 

lineprops = dict(color='black', linestyle='-', 

linewidth=2, alpha=0.5) 

lineprops['animated'] = self.useblit 

self.lineprops = lineprops 

self.to_draw = Line2D([0, 0], [0, 0], visible=False, 

**self.lineprops) 

self.ax.add_line(self.to_draw) 

 

self.minspanx = minspanx 

self.minspany = minspany 

 

if spancoords not in ('data', 'pixels'): 

raise ValueError("'spancoords' must be 'data' or 'pixels'") 

 

self.spancoords = spancoords 

self.drawtype = drawtype 

 

self.maxdist = maxdist 

 

if rectprops is None: 

props = dict(mec='r') 

else: 

props = dict(mec=rectprops.get('edgecolor', 'r')) 

self._corner_order = ['NW', 'NE', 'SE', 'SW'] 

xc, yc = self.corners 

self._corner_handles = ToolHandles(self.ax, xc, yc, marker_props=props, 

useblit=self.useblit) 

 

self._edge_order = ['W', 'N', 'E', 'S'] 

xe, ye = self.edge_centers 

self._edge_handles = ToolHandles(self.ax, xe, ye, marker='s', 

marker_props=props, 

useblit=self.useblit) 

 

xc, yc = self.center 

self._center_handle = ToolHandles(self.ax, [xc], [yc], marker='s', 

marker_props=props, 

useblit=self.useblit) 

 

self.active_handle = None 

 

self.artists = [self.to_draw, self._center_handle.artist, 

self._corner_handles.artist, 

self._edge_handles.artist] 

 

if not self.interactive: 

self.artists = [self.to_draw] 

 

self._extents_on_press = None 

 

def _press(self, event): 

"""on button press event""" 

# make the drawed box/line visible get the click-coordinates, 

# button, ... 

if self.interactive and self.to_draw.get_visible(): 

self._set_active_handle(event) 

else: 

self.active_handle = None 

 

if self.active_handle is None or not self.interactive: 

# Clear previous rectangle before drawing new rectangle. 

self.update() 

 

if not self.interactive: 

x = event.xdata 

y = event.ydata 

self.extents = x, x, y, y 

 

self.set_visible(self.visible) 

 

def _release(self, event): 

"""on button release event""" 

if not self.interactive: 

self.to_draw.set_visible(False) 

 

# update the eventpress and eventrelease with the resulting extents 

x1, x2, y1, y2 = self.extents 

self.eventpress.xdata = x1 

self.eventpress.ydata = y1 

xy1 = self.ax.transData.transform_point([x1, y1]) 

self.eventpress.x, self.eventpress.y = xy1 

 

self.eventrelease.xdata = x2 

self.eventrelease.ydata = y2 

xy2 = self.ax.transData.transform_point([x2, y2]) 

self.eventrelease.x, self.eventrelease.y = xy2 

 

if self.spancoords == 'data': 

xmin, ymin = self.eventpress.xdata, self.eventpress.ydata 

xmax, ymax = self.eventrelease.xdata, self.eventrelease.ydata 

# calculate dimensions of box or line get values in the right 

# order 

elif self.spancoords == 'pixels': 

xmin, ymin = self.eventpress.x, self.eventpress.y 

xmax, ymax = self.eventrelease.x, self.eventrelease.y 

else: 

raise ValueError('spancoords must be "data" or "pixels"') 

 

if xmin > xmax: 

xmin, xmax = xmax, xmin 

if ymin > ymax: 

ymin, ymax = ymax, ymin 

 

spanx = xmax - xmin 

spany = ymax - ymin 

xproblems = self.minspanx is not None and spanx < self.minspanx 

yproblems = self.minspany is not None and spany < self.minspany 

 

# check if drawn distance (if it exists) is not too small in 

# either x or y-direction 

if self.drawtype != 'none' and (xproblems or yproblems): 

for artist in self.artists: 

artist.set_visible(False) 

self.update() 

return 

 

# call desired function 

self.onselect(self.eventpress, self.eventrelease) 

self.update() 

 

return False 

 

def _onmove(self, event): 

"""on motion notify event if box/line is wanted""" 

# resize an existing shape 

if self.active_handle and not self.active_handle == 'C': 

x1, x2, y1, y2 = self._extents_on_press 

if self.active_handle in ['E', 'W'] + self._corner_order: 

x2 = event.xdata 

if self.active_handle in ['N', 'S'] + self._corner_order: 

y2 = event.ydata 

 

# move existing shape 

elif (('move' in self.state or self.active_handle == 'C') 

and self._extents_on_press is not None): 

x1, x2, y1, y2 = self._extents_on_press 

dx = event.xdata - self.eventpress.xdata 

dy = event.ydata - self.eventpress.ydata 

x1 += dx 

x2 += dx 

y1 += dy 

y2 += dy 

 

# new shape 

else: 

center = [self.eventpress.xdata, self.eventpress.ydata] 

center_pix = [self.eventpress.x, self.eventpress.y] 

dx = (event.xdata - center[0]) / 2. 

dy = (event.ydata - center[1]) / 2. 

 

# square shape 

if 'square' in self.state: 

dx_pix = abs(event.x - center_pix[0]) 

dy_pix = abs(event.y - center_pix[1]) 

if not dx_pix: 

return 

maxd = max(abs(dx_pix), abs(dy_pix)) 

if abs(dx_pix) < maxd: 

dx *= maxd / (abs(dx_pix) + 1e-6) 

if abs(dy_pix) < maxd: 

dy *= maxd / (abs(dy_pix) + 1e-6) 

 

# from center 

if 'center' in self.state: 

dx *= 2 

dy *= 2 

 

# from corner 

else: 

center[0] += dx 

center[1] += dy 

 

x1, x2, y1, y2 = (center[0] - dx, center[0] + dx, 

center[1] - dy, center[1] + dy) 

 

self.extents = x1, x2, y1, y2 

 

@property 

def _rect_bbox(self): 

if self.drawtype == 'box': 

x0 = self.to_draw.get_x() 

y0 = self.to_draw.get_y() 

width = self.to_draw.get_width() 

height = self.to_draw.get_height() 

return x0, y0, width, height 

else: 

x, y = self.to_draw.get_data() 

x0, x1 = min(x), max(x) 

y0, y1 = min(y), max(y) 

return x0, y0, x1 - x0, y1 - y0 

 

@property 

def corners(self): 

"""Corners of rectangle from lower left, moving clockwise.""" 

x0, y0, width, height = self._rect_bbox 

xc = x0, x0 + width, x0 + width, x0 

yc = y0, y0, y0 + height, y0 + height 

return xc, yc 

 

@property 

def edge_centers(self): 

"""Midpoint of rectangle edges from left, moving clockwise.""" 

x0, y0, width, height = self._rect_bbox 

w = width / 2. 

h = height / 2. 

xe = x0, x0 + w, x0 + width, x0 + w 

ye = y0 + h, y0, y0 + h, y0 + height 

return xe, ye 

 

@property 

def center(self): 

"""Center of rectangle""" 

x0, y0, width, height = self._rect_bbox 

return x0 + width / 2., y0 + height / 2. 

 

@property 

def extents(self): 

"""Return (xmin, xmax, ymin, ymax).""" 

x0, y0, width, height = self._rect_bbox 

xmin, xmax = sorted([x0, x0 + width]) 

ymin, ymax = sorted([y0, y0 + height]) 

return xmin, xmax, ymin, ymax 

 

@extents.setter 

def extents(self, extents): 

# Update displayed shape 

self.draw_shape(extents) 

# Update displayed handles 

self._corner_handles.set_data(*self.corners) 

self._edge_handles.set_data(*self.edge_centers) 

self._center_handle.set_data(*self.center) 

self.set_visible(self.visible) 

self.update() 

 

def draw_shape(self, extents): 

x0, x1, y0, y1 = extents 

xmin, xmax = sorted([x0, x1]) 

ymin, ymax = sorted([y0, y1]) 

xlim = sorted(self.ax.get_xlim()) 

ylim = sorted(self.ax.get_ylim()) 

 

xmin = max(xlim[0], xmin) 

ymin = max(ylim[0], ymin) 

xmax = min(xmax, xlim[1]) 

ymax = min(ymax, ylim[1]) 

 

if self.drawtype == 'box': 

self.to_draw.set_x(xmin) 

self.to_draw.set_y(ymin) 

self.to_draw.set_width(xmax - xmin) 

self.to_draw.set_height(ymax - ymin) 

 

elif self.drawtype == 'line': 

self.to_draw.set_data([xmin, xmax], [ymin, ymax]) 

 

def _set_active_handle(self, event): 

"""Set active handle based on the location of the mouse event""" 

# Note: event.xdata/ydata in data coordinates, event.x/y in pixels 

c_idx, c_dist = self._corner_handles.closest(event.x, event.y) 

e_idx, e_dist = self._edge_handles.closest(event.x, event.y) 

m_idx, m_dist = self._center_handle.closest(event.x, event.y) 

 

if 'move' in self.state: 

self.active_handle = 'C' 

self._extents_on_press = self.extents 

 

# Set active handle as closest handle, if mouse click is close enough. 

elif m_dist < self.maxdist * 2: 

self.active_handle = 'C' 

elif c_dist > self.maxdist and e_dist > self.maxdist: 

self.active_handle = None 

return 

elif c_dist < e_dist: 

self.active_handle = self._corner_order[c_idx] 

else: 

self.active_handle = self._edge_order[e_idx] 

 

# Save coordinates of rectangle at the start of handle movement. 

x1, x2, y1, y2 = self.extents 

# Switch variables so that only x2 and/or y2 are updated on move. 

if self.active_handle in ['W', 'SW', 'NW']: 

x1, x2 = x2, event.xdata 

if self.active_handle in ['N', 'NW', 'NE']: 

y1, y2 = y2, event.ydata 

self._extents_on_press = x1, x2, y1, y2 

 

@property 

def geometry(self): 

""" 

Returns numpy.ndarray of shape (2,5) containing 

x (``RectangleSelector.geometry[1,:]``) and 

y (``RectangleSelector.geometry[0,:]``) 

coordinates of the four corners of the rectangle starting 

and ending in the top left corner. 

""" 

if hasattr(self.to_draw, 'get_verts'): 

xfm = self.ax.transData.inverted() 

y, x = xfm.transform(self.to_draw.get_verts()).T 

return np.array([x, y]) 

else: 

return np.array(self.to_draw.get_data()) 

 

 

class EllipseSelector(RectangleSelector): 

""" 

Select an elliptical region of an axes. 

 

For the cursor to remain responsive you must keep a reference to 

it. 

 

Example usage:: 

 

import numpy as np 

import matplotlib.pyplot as plt 

from matplotlib.widgets import EllipseSelector 

 

def onselect(eclick, erelease): 

"eclick and erelease are matplotlib events at press and release." 

print('startposition: (%f, %f)' % (eclick.xdata, eclick.ydata)) 

print('endposition : (%f, %f)' % (erelease.xdata, erelease.ydata)) 

print('used button : ', eclick.button) 

 

def toggle_selector(event): 

print(' Key pressed.') 

if event.key in ['Q', 'q'] and toggle_selector.ES.active: 

print('EllipseSelector deactivated.') 

toggle_selector.RS.set_active(False) 

if event.key in ['A', 'a'] and not toggle_selector.ES.active: 

print('EllipseSelector activated.') 

toggle_selector.ES.set_active(True) 

 

x = np.arange(100.) / 99 

y = np.sin(x) 

fig, ax = plt.subplots() 

ax.plot(x, y) 

 

toggle_selector.ES = EllipseSelector(ax, onselect, drawtype='line') 

fig.canvas.connect('key_press_event', toggle_selector) 

plt.show() 

""" 

_shape_klass = Ellipse 

 

def draw_shape(self, extents): 

x1, x2, y1, y2 = extents 

xmin, xmax = sorted([x1, x2]) 

ymin, ymax = sorted([y1, y2]) 

center = [x1 + (x2 - x1) / 2., y1 + (y2 - y1) / 2.] 

a = (xmax - xmin) / 2. 

b = (ymax - ymin) / 2. 

 

if self.drawtype == 'box': 

self.to_draw.center = center 

self.to_draw.width = 2 * a 

self.to_draw.height = 2 * b 

else: 

rad = np.deg2rad(np.arange(31) * 12) 

x = a * np.cos(rad) + center[0] 

y = b * np.sin(rad) + center[1] 

self.to_draw.set_data(x, y) 

 

@property 

def _rect_bbox(self): 

if self.drawtype == 'box': 

x, y = self.to_draw.center 

width = self.to_draw.width 

height = self.to_draw.height 

return x - width / 2., y - height / 2., width, height 

else: 

x, y = self.to_draw.get_data() 

x0, x1 = min(x), max(x) 

y0, y1 = min(y), max(y) 

return x0, y0, x1 - x0, y1 - y0 

 

 

class LassoSelector(_SelectorWidget): 

""" 

Selection curve of an arbitrary shape. 

 

For the selector to remain responsive you must keep a reference to it. 

 

The selected path can be used in conjunction with `~.Path.contains_point` 

to select data points from an image. 

 

In contrast to `Lasso`, `LassoSelector` is written with an interface 

similar to `RectangleSelector` and `SpanSelector`, and will continue to 

interact with the axes until disconnected. 

 

Example usage:: 

 

ax = subplot(111) 

ax.plot(x,y) 

 

def onselect(verts): 

print(verts) 

lasso = LassoSelector(ax, onselect) 

 

Parameters 

---------- 

ax : :class:`~matplotlib.axes.Axes` 

The parent axes for the widget. 

onselect : function 

Whenever the lasso is released, the *onselect* function is called and 

passed the vertices of the selected path. 

button : List[Int], optional 

A list of integers indicating which mouse buttons should be used for 

rectangle selection. You can also specify a single integer if only a 

single button is desired. Default is ``None``, which does not limit 

which button can be used. 

 

Note, typically: 

 

- 1 = left mouse button 

- 2 = center mouse button (scroll wheel) 

- 3 = right mouse button 

 

""" 

 

def __init__(self, ax, onselect=None, useblit=True, lineprops=None, 

button=None): 

_SelectorWidget.__init__(self, ax, onselect, useblit=useblit, 

button=button) 

 

self.verts = None 

 

if lineprops is None: 

lineprops = dict() 

if useblit: 

lineprops['animated'] = True 

self.line = Line2D([], [], **lineprops) 

self.line.set_visible(False) 

self.ax.add_line(self.line) 

self.artists = [self.line] 

 

def onpress(self, event): 

self.press(event) 

 

def _press(self, event): 

self.verts = [self._get_data(event)] 

self.line.set_visible(True) 

 

def onrelease(self, event): 

self.release(event) 

 

def _release(self, event): 

if self.verts is not None: 

self.verts.append(self._get_data(event)) 

self.onselect(self.verts) 

self.line.set_data([[], []]) 

self.line.set_visible(False) 

self.verts = None 

 

def _onmove(self, event): 

if self.verts is None: 

return 

self.verts.append(self._get_data(event)) 

 

self.line.set_data(list(zip(*self.verts))) 

 

self.update() 

 

 

class PolygonSelector(_SelectorWidget): 

"""Select a polygon region of an axes. 

 

Place vertices with each mouse click, and make the selection by completing 

the polygon (clicking on the first vertex). Hold the *ctrl* key and click 

and drag a vertex to reposition it (the *ctrl* key is not necessary if the 

polygon has already been completed). Hold the *shift* key and click and 

drag anywhere in the axes to move all vertices. Press the *esc* key to 

start a new polygon. 

 

For the selector to remain responsive you must keep a reference to 

it. 

 

Parameters 

---------- 

ax : :class:`~matplotlib.axes.Axes` 

The parent axes for the widget. 

onselect : function 

When a polygon is completed or modified after completion, 

the `onselect` function is called and passed a list of the vertices as 

``(xdata, ydata)`` tuples. 

useblit : bool, optional 

lineprops : dict, optional 

The line for the sides of the polygon is drawn with the properties 

given by `lineprops`. The default is ``dict(color='k', linestyle='-', 

linewidth=2, alpha=0.5)``. 

markerprops : dict, optional 

The markers for the vertices of the polygon are drawn with the 

properties given by `markerprops`. The default is ``dict(marker='o', 

markersize=7, mec='k', mfc='k', alpha=0.5)``. 

vertex_select_radius : float, optional 

A vertex is selected (to complete the polygon or to move a vertex) 

if the mouse click is within `vertex_select_radius` pixels of the 

vertex. The default radius is 15 pixels. 

 

Examples 

-------- 

:doc:`/gallery/widgets/polygon_selector_demo` 

""" 

 

def __init__(self, ax, onselect, useblit=False, 

lineprops=None, markerprops=None, vertex_select_radius=15): 

# The state modifiers 'move', 'square', and 'center' are expected by 

# _SelectorWidget but are not supported by PolygonSelector 

# Note: could not use the existing 'move' state modifier in-place of 

# 'move_all' because _SelectorWidget automatically discards 'move' 

# from the state on button release. 

state_modifier_keys = dict(clear='escape', move_vertex='control', 

move_all='shift', move='not-applicable', 

square='not-applicable', 

center='not-applicable') 

_SelectorWidget.__init__(self, ax, onselect, useblit=useblit, 

state_modifier_keys=state_modifier_keys) 

 

self._xs, self._ys = [0], [0] 

self._polygon_completed = False 

 

if lineprops is None: 

lineprops = dict(color='k', linestyle='-', linewidth=2, alpha=0.5) 

lineprops['animated'] = self.useblit 

self.line = Line2D(self._xs, self._ys, **lineprops) 

self.ax.add_line(self.line) 

 

if markerprops is None: 

markerprops = dict(mec='k', mfc=lineprops.get('color', 'k')) 

self._polygon_handles = ToolHandles(self.ax, self._xs, self._ys, 

useblit=self.useblit, 

marker_props=markerprops) 

 

self._active_handle_idx = -1 

self.vertex_select_radius = vertex_select_radius 

 

self.artists = [self.line, self._polygon_handles.artist] 

self.set_visible(True) 

 

def _press(self, event): 

"""Button press event handler""" 

# Check for selection of a tool handle. 

if ((self._polygon_completed or 'move_vertex' in self.state) 

and len(self._xs) > 0): 

h_idx, h_dist = self._polygon_handles.closest(event.x, event.y) 

if h_dist < self.vertex_select_radius: 

self._active_handle_idx = h_idx 

# Save the vertex positions at the time of the press event (needed to 

# support the 'move_all' state modifier). 

self._xs_at_press, self._ys_at_press = self._xs[:], self._ys[:] 

 

def _release(self, event): 

"""Button release event handler""" 

# Release active tool handle. 

if self._active_handle_idx >= 0: 

self._active_handle_idx = -1 

 

# Complete the polygon. 

elif (len(self._xs) > 3 

and self._xs[-1] == self._xs[0] 

and self._ys[-1] == self._ys[0]): 

self._polygon_completed = True 

 

# Place new vertex. 

elif (not self._polygon_completed 

and 'move_all' not in self.state 

and 'move_vertex' not in self.state): 

self._xs.insert(-1, event.xdata) 

self._ys.insert(-1, event.ydata) 

 

if self._polygon_completed: 

self.onselect(self.verts) 

 

def onmove(self, event): 

"""Cursor move event handler and validator""" 

# Method overrides _SelectorWidget.onmove because the polygon selector 

# needs to process the move callback even if there is no button press. 

# _SelectorWidget.onmove include logic to ignore move event if 

# eventpress is None. 

if not self.ignore(event): 

event = self._clean_event(event) 

self._onmove(event) 

return True 

return False 

 

def _onmove(self, event): 

"""Cursor move event handler""" 

# Move the active vertex (ToolHandle). 

if self._active_handle_idx >= 0: 

idx = self._active_handle_idx 

self._xs[idx], self._ys[idx] = event.xdata, event.ydata 

# Also update the end of the polygon line if the first vertex is 

# the active handle and the polygon is completed. 

if idx == 0 and self._polygon_completed: 

self._xs[-1], self._ys[-1] = event.xdata, event.ydata 

 

# Move all vertices. 

elif 'move_all' in self.state and self.eventpress: 

dx = event.xdata - self.eventpress.xdata 

dy = event.ydata - self.eventpress.ydata 

for k in range(len(self._xs)): 

self._xs[k] = self._xs_at_press[k] + dx 

self._ys[k] = self._ys_at_press[k] + dy 

 

# Do nothing if completed or waiting for a move. 

elif (self._polygon_completed 

or 'move_vertex' in self.state or 'move_all' in self.state): 

return 

 

# Position pending vertex. 

else: 

# Calculate distance to the start vertex. 

x0, y0 = self.line.get_transform().transform((self._xs[0], 

self._ys[0])) 

v0_dist = np.sqrt((x0 - event.x) ** 2 + (y0 - event.y) ** 2) 

# Lock on to the start vertex if near it and ready to complete. 

if len(self._xs) > 3 and v0_dist < self.vertex_select_radius: 

self._xs[-1], self._ys[-1] = self._xs[0], self._ys[0] 

else: 

self._xs[-1], self._ys[-1] = event.xdata, event.ydata 

 

self._draw_polygon() 

 

def _on_key_press(self, event): 

"""Key press event handler""" 

# Remove the pending vertex if entering the 'move_vertex' or 

# 'move_all' mode 

if (not self._polygon_completed 

and ('move_vertex' in self.state or 'move_all' in self.state)): 

self._xs, self._ys = self._xs[:-1], self._ys[:-1] 

self._draw_polygon() 

 

def _on_key_release(self, event): 

"""Key release event handler""" 

# Add back the pending vertex if leaving the 'move_vertex' or 

# 'move_all' mode (by checking the released key) 

if (not self._polygon_completed 

and 

(event.key == self.state_modifier_keys.get('move_vertex') 

or event.key == self.state_modifier_keys.get('move_all'))): 

self._xs.append(event.xdata) 

self._ys.append(event.ydata) 

self._draw_polygon() 

# Reset the polygon if the released key is the 'clear' key. 

elif event.key == self.state_modifier_keys.get('clear'): 

event = self._clean_event(event) 

self._xs, self._ys = [event.xdata], [event.ydata] 

self._polygon_completed = False 

self.set_visible(True) 

 

def _draw_polygon(self): 

"""Redraw the polygon based on the new vertex positions.""" 

self.line.set_data(self._xs, self._ys) 

# Only show one tool handle at the start and end vertex of the polygon 

# if the polygon is completed or the user is locked on to the start 

# vertex. 

if (self._polygon_completed 

or (len(self._xs) > 3 

and self._xs[-1] == self._xs[0] 

and self._ys[-1] == self._ys[0])): 

self._polygon_handles.set_data(self._xs[:-1], self._ys[:-1]) 

else: 

self._polygon_handles.set_data(self._xs, self._ys) 

self.update() 

 

@property 

def verts(self): 

"""Get the polygon vertices. 

 

Returns 

------- 

list 

A list of the vertices of the polygon as ``(xdata, ydata)`` tuples. 

""" 

return list(zip(self._xs[:-1], self._ys[:-1])) 

 

 

class Lasso(AxesWidget): 

"""Selection curve of an arbitrary shape. 

 

The selected path can be used in conjunction with 

:func:`~matplotlib.path.Path.contains_point` to select data points 

from an image. 

 

Unlike :class:`LassoSelector`, this must be initialized with a starting 

point `xy`, and the `Lasso` events are destroyed upon release. 

 

Parameters 

---------- 

ax : `~matplotlib.axes.Axes` 

The parent axes for the widget. 

xy : (float, float) 

Coordinates of the start of the lasso. 

callback : callable 

Whenever the lasso is released, the `callback` function is called and 

passed the vertices of the selected path. 

""" 

 

def __init__(self, ax, xy, callback=None, useblit=True): 

AxesWidget.__init__(self, ax) 

 

self.useblit = useblit and self.canvas.supports_blit 

if self.useblit: 

self.background = self.canvas.copy_from_bbox(self.ax.bbox) 

 

x, y = xy 

self.verts = [(x, y)] 

self.line = Line2D([x], [y], linestyle='-', color='black', lw=2) 

self.ax.add_line(self.line) 

self.callback = callback 

self.connect_event('button_release_event', self.onrelease) 

self.connect_event('motion_notify_event', self.onmove) 

 

def onrelease(self, event): 

if self.ignore(event): 

return 

if self.verts is not None: 

self.verts.append((event.xdata, event.ydata)) 

if len(self.verts) > 2: 

self.callback(self.verts) 

self.ax.lines.remove(self.line) 

self.verts = None 

self.disconnect_events() 

 

def onmove(self, event): 

if self.ignore(event): 

return 

if self.verts is None: 

return 

if event.inaxes != self.ax: 

return 

if event.button != 1: 

return 

self.verts.append((event.xdata, event.ydata)) 

 

self.line.set_data(list(zip(*self.verts))) 

 

if self.useblit: 

self.canvas.restore_region(self.background) 

self.ax.draw_artist(self.line) 

self.canvas.blit(self.ax.bbox) 

else: 

self.canvas.draw_idle()