2
2
Test Figure.legend.
3
3
"""
4
4
5
+ import io
5
6
from pathlib import Path
6
7
7
8
import pytest
10
11
from pygmt .helpers import GMTTempFile
11
12
12
13
14
+ @pytest .fixture (scope = "module" , name = "legend_spec" )
15
+ def fixture_legend_spec ():
16
+ """
17
+ A string contains a legend specification.
18
+ """
19
+ return """
20
+ G -0.1i
21
+ H 24 Times-Roman My Map Legend
22
+ D 0.2i 1p
23
+ N 2
24
+ V 0 1p
25
+ S 0.1i c 0.15i p300/12 0.25p 0.3i This circle is hachured
26
+ S 0.1i e 0.15i yellow 0.25p 0.3i This ellipse is yellow
27
+ S 0.1i w 0.15i green 0.25p 0.3i This wedge is green
28
+ S 0.1i f0.1i+l+t 0.25i blue 0.25p 0.3i This is a fault
29
+ S 0.1i - 0.15i - 0.25p,- 0.3i A dashed contour
30
+ S 0.1i v0.1i+a40+e 0.25i magenta 0.25p 0.3i This is a vector
31
+ S 0.1i i 0.15i cyan 0.25p 0.3i This triangle is boring
32
+ V 0 1p
33
+ D 0.2i 1p
34
+ N 1
35
+ G 0.05i
36
+ G 0.05i
37
+ G 0.05i
38
+ L 9 4 R Smith et al., @%5%J. Geophys. Res., 99@%%, 2000
39
+ G 0.1i
40
+ P
41
+ T Let us just try some simple text that can go on a few lines.
42
+ T There is no easy way to predetermine how many lines will be required,
43
+ T so we may have to adjust the box height to get the right size box.
44
+ """
45
+
46
+
13
47
@pytest .mark .mpl_image_compare
14
48
def test_legend_position ():
15
49
"""
16
- Test that plots a position with each of the four legend coordinate systems.
50
+ Test positioning the legend with different coordinate systems.
17
51
"""
18
-
19
52
fig = Figure ()
20
53
fig .basemap (region = [- 2 , 2 , - 2 , 2 ], frame = True )
21
54
positions = ["jTR+jTR" , "g0/1" , "n0.2/0.2" , "x4i/2i/2i" ]
@@ -30,22 +63,18 @@ def test_legend_default_position():
30
63
"""
31
64
Test using the default legend position.
32
65
"""
33
-
34
66
fig = Figure ()
35
-
36
67
fig .basemap (region = [- 1 , 1 , - 1 , 1 ], frame = True )
37
-
38
68
fig .plot (x = [0 ], y = [0 ], style = "p10p" , label = "Default" )
39
69
fig .legend ()
40
-
41
70
return fig
42
71
43
72
44
73
@pytest .mark .benchmark
45
74
@pytest .mark .mpl_image_compare
46
75
def test_legend_entries ():
47
76
"""
48
- Test different marker types/shapes .
77
+ Test legend using the automatically generated legend entries .
49
78
"""
50
79
fig = Figure ()
51
80
fig .basemap (projection = "x1i" , region = [0 , 7 , 3 , 7 ], frame = True )
@@ -59,48 +88,31 @@ def test_legend_entries():
59
88
fig .plot (data = "@Table_5_11.txt" , pen = "1.5p,gray" , label = "My lines" )
60
89
fig .plot (data = "@Table_5_11.txt" , style = "t0.15i" , fill = "orange" , label = "Oranges" )
61
90
fig .legend (position = "JTR+jTR" )
62
-
63
91
return fig
64
92
65
93
66
94
@pytest .mark .mpl_image_compare
67
- def test_legend_specfile ():
95
+ def test_legend_specfile (legend_spec ):
68
96
"""
69
- Test specfile functionality .
97
+ Test passing a legend specification file .
70
98
"""
71
-
72
- specfile_contents = """
73
- G -0.1i
74
- H 24 Times-Roman My Map Legend
75
- D 0.2i 1p
76
- N 2
77
- V 0 1p
78
- S 0.1i c 0.15i p300/12 0.25p 0.3i This circle is hachured
79
- S 0.1i e 0.15i yellow 0.25p 0.3i This ellipse is yellow
80
- S 0.1i w 0.15i green 0.25p 0.3i This wedge is green
81
- S 0.1i f0.1i+l+t 0.25i blue 0.25p 0.3i This is a fault
82
- S 0.1i - 0.15i - 0.25p,- 0.3i A dashed contour
83
- S 0.1i v0.1i+a40+e 0.25i magenta 0.25p 0.3i This is a vector
84
- S 0.1i i 0.15i cyan 0.25p 0.3i This triangle is boring
85
- V 0 1p
86
- D 0.2i 1p
87
- N 1
88
- G 0.05i
89
- G 0.05i
90
- G 0.05i
91
- L 9 4 R Smith et al., @%5%J. Geophys. Res., 99@%%, 2000
92
- G 0.1i
93
- P
94
- T Let us just try some simple text that can go on a few lines.
95
- T There is no easy way to predetermine how many lines will be required,
96
- T so we may have to adjust the box height to get the right size box.
97
- """
98
-
99
99
with GMTTempFile () as specfile :
100
- Path (specfile .name ).write_text (specfile_contents , encoding = "utf-8" )
100
+ Path (specfile .name ).write_text (legend_spec , encoding = "utf-8" )
101
101
fig = Figure ()
102
102
fig .basemap (projection = "x6i" , region = [0 , 1 , 0 , 1 ], frame = True )
103
103
fig .legend (specfile .name , position = "JTM+jCM+w5i" )
104
+ return fig
105
+
106
+
107
+ @pytest .mark .mpl_image_compare (filename = "test_legend_specfile.png" )
108
+ def test_legend_stringio (legend_spec ):
109
+ """
110
+ Test passing an legend specification via an io.StringIO object.
111
+ """
112
+ spec = io .StringIO (legend_spec )
113
+ fig = Figure ()
114
+ fig .basemap (projection = "x6i" , region = [0 , 1 , 0 , 1 ], frame = True )
115
+ fig .legend (spec , position = "JTM+jCM+w5i" )
104
116
return fig
105
117
106
118
@@ -111,3 +123,6 @@ def test_legend_fails():
111
123
fig = Figure ()
112
124
with pytest .raises (GMTInvalidInput ):
113
125
fig .legend (spec = ["@Table_5_11.txt" ])
126
+
127
+ with pytest .raises (GMTInvalidInput ):
128
+ fig .legend (spec = [1 , 2 ])
0 commit comments