1010from pygmt .helpers import GMTTempFile
1111
1212
13+ @pytest .fixture (scope = "module" , name = "legend_spec" )
14+ def fixture_legend_spec ():
15+ """
16+ A string contains a legend specification.
17+ """
18+ return """
19+ G -0.1i
20+ H 24 Times-Roman My Map Legend
21+ D 0.2i 1p
22+ N 2
23+ V 0 1p
24+ S 0.1i c 0.15i p300/12 0.25p 0.3i This circle is hachured
25+ S 0.1i e 0.15i yellow 0.25p 0.3i This ellipse is yellow
26+ S 0.1i w 0.15i green 0.25p 0.3i This wedge is green
27+ S 0.1i f0.1i+l+t 0.25i blue 0.25p 0.3i This is a fault
28+ S 0.1i - 0.15i - 0.25p,- 0.3i A dashed contour
29+ S 0.1i v0.1i+a40+e 0.25i magenta 0.25p 0.3i This is a vector
30+ S 0.1i i 0.15i cyan 0.25p 0.3i This triangle is boring
31+ V 0 1p
32+ D 0.2i 1p
33+ N 1
34+ G 0.05i
35+ G 0.05i
36+ G 0.05i
37+ L 9 4 R Smith et al., @%5%J. Geophys. Res., 99@%%, 2000
38+ G 0.1i
39+ P
40+ T Let us just try some simple text that can go on a few lines.
41+ T There is no easy way to predetermine how many lines will be required,
42+ T so we may have to adjust the box height to get the right size box.
43+ """
44+
45+
1346@pytest .mark .mpl_image_compare
1447def test_legend_position ():
1548 """
16- Test that plots a position with each of the four legend coordinate systems.
49+ Test positioning the legend with different coordinate systems.
1750 """
18-
1951 fig = Figure ()
2052 fig .basemap (region = [- 2 , 2 , - 2 , 2 ], frame = True )
2153 positions = ["jTR+jTR" , "g0/1" , "n0.2/0.2" , "x4i/2i/2i" ]
@@ -30,22 +62,18 @@ def test_legend_default_position():
3062 """
3163 Test using the default legend position.
3264 """
33-
3465 fig = Figure ()
35-
3666 fig .basemap (region = [- 1 , 1 , - 1 , 1 ], frame = True )
37-
3867 fig .plot (x = [0 ], y = [0 ], style = "p10p" , label = "Default" )
3968 fig .legend ()
40-
4169 return fig
4270
4371
4472@pytest .mark .benchmark
4573@pytest .mark .mpl_image_compare
4674def test_legend_entries ():
4775 """
48- Test different marker types/shapes .
76+ Test legend using the automatically generated legend entries .
4977 """
5078 fig = Figure ()
5179 fig .basemap (projection = "x1i" , region = [0 , 7 , 3 , 7 ], frame = True )
@@ -59,45 +87,16 @@ def test_legend_entries():
5987 fig .plot (data = "@Table_5_11.txt" , pen = "1.5p,gray" , label = "My lines" )
6088 fig .plot (data = "@Table_5_11.txt" , style = "t0.15i" , fill = "orange" , label = "Oranges" )
6189 fig .legend (position = "JTR+jTR" )
62-
6390 return fig
6491
6592
6693@pytest .mark .mpl_image_compare
67- def test_legend_specfile ():
94+ def test_legend_specfile (legend_spec ):
6895 """
69- Test specfile functionality .
96+ Test passing a legend specification file .
7097 """
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-
9998 with GMTTempFile () as specfile :
100- Path (specfile .name ).write_text (specfile_contents , encoding = "utf-8" )
99+ Path (specfile .name ).write_text (legend_spec , encoding = "utf-8" )
101100 fig = Figure ()
102101 fig .basemap (projection = "x6i" , region = [0 , 1 , 0 , 1 ], frame = True )
103102 fig .legend (specfile .name , position = "JTM+jCM+w5i" )
@@ -111,3 +110,6 @@ def test_legend_fails():
111110 fig = Figure ()
112111 with pytest .raises (GMTInvalidInput ):
113112 fig .legend (spec = ["@Table_5_11.txt" ])
113+
114+ with pytest .raises (GMTInvalidInput ):
115+ fig .legend (spec = [1 , 2 ])
0 commit comments