File: patheffect_demo.py

package info (click to toggle)
matplotlib 1.1.1~rc2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 66,076 kB
  • sloc: python: 90,600; cpp: 69,891; objc: 5,231; ansic: 1,723; makefile: 171; sh: 7
file content (37 lines) | stat: -rw-r--r-- 1,340 bytes parent folder | download | duplicates (3)
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
import matplotlib.pyplot as plt
import matplotlib.patheffects as PathEffects
import numpy as np

if 1:
    plt.figure(1, figsize=(8,3))
    ax1 = plt.subplot(131)
    ax1.imshow([[1,2],[2,3]])
    txt = ax1.annotate("test", (1., 1.), (0., 0),
                       arrowprops=dict(arrowstyle="->",
                                       connectionstyle="angle3", lw=2),
                       size=20, ha="center")

    txt.set_path_effects([PathEffects.withStroke(linewidth=3,
                                                 foreground="w")])
    txt.arrow_patch.set_path_effects([PathEffects.Stroke(linewidth=5,
                                                         foreground="w"),
                                      PathEffects.Normal()])

    ax2 = plt.subplot(132)
    arr = np.arange(25).reshape((5,5))
    ax2.imshow(arr)
    cntr = ax2.contour(arr, colors="k")
    clbls = ax2.clabel(cntr, fmt="%2.0f", use_clabeltext=True)
    plt.setp(clbls,
             path_effects=[PathEffects.withStroke(linewidth=3,
                                                  foreground="w")])


    # shadow as a path effect
    ax3 = plt.subplot(133)
    p1, = ax3.plot([0, 1], [0, 1])
    leg = ax3.legend([p1], ["Line 1"], fancybox=True, loc=2)
    leg.legendPatch.set_path_effects([PathEffects.withSimplePatchShadow()])

    plt.show()