@@ -30,46 +30,44 @@ def rainbow_text(x, y, strings, colors, orientation='horizontal',
30
30
ax = None , ** kwargs ):
31
31
"""
32
32
Take a list of *strings* and *colors* and place them next to each
33
- other, with text strings[i] being shown in colors[i] .
33
+ other, with the nth text being shown in the nth color .
34
34
35
35
Parameters
36
36
----------
37
37
x, y : float
38
- Text position in data coordinates.
38
+ The position of the first string. As for `~.Axes.text`, the coordinate
39
+ system can be set using the *transform* parameter.
39
40
strings : list of str
40
41
The strings to draw.
41
42
colors : list of color
42
43
The colors to use.
43
44
orientation : {'horizontal', 'vertical'}
44
- ax : Axes, optional
45
- The Axes to draw into. If None, the current axes will be used.
46
- **kwargs :
47
- All other keyword arguments are passed to plt.text() and plt.annotate(), so you
48
- can set the font size, family, etc.
45
+ The orientation of the text.
46
+ ax : Axes, default: the current Axes
47
+ The Axes to draw into.
48
+ **kwargs : `.Text` properties
49
49
"""
50
50
if ax is None :
51
51
ax = plt .gca ()
52
52
53
- assert orientation in [' horizontal' , ' vertical' ]
54
- if orientation == ' horizontal' :
55
- txt = ax . text ( x , y , strings [ 0 ], color = colors [ 0 ], ** kwargs )
56
- for s , c in zip ( strings [ 1 :], colors [ 1 :]) :
57
- txt = ax . annotate ( ' ' + s , xy = ( 1 , 0 ), xycoords = txt ,
58
- va = "bottom" , color = c , ** kwargs )
53
+ assert orientation in [" horizontal" , " vertical" ]
54
+ if orientation == " horizontal" :
55
+ xy = ( 1 , 0 )
56
+ elif orientation == "vertical" :
57
+ xy = ( 0 , 1 )
58
+ kwargs . update ( rotation = 90 )
59
59
60
- elif orientation == 'vertical' :
61
- kwargs .update (rotation = 90 , verticalalignment = 'bottom' )
62
- txt = ax .text (x , y , strings [0 ], color = colors [0 ], ** kwargs )
63
- for s , c in zip (strings [1 :], colors [1 :]):
64
- txt = ax .annotate (' ' + s , xy = (0 , 1 ), xycoords = txt ,
65
- va = "bottom" , color = c , ** kwargs )
60
+ txt = ax .text (x , y , strings [0 ], color = colors [0 ], ** kwargs )
61
+ for s , c in zip (strings [1 :], colors [1 :]):
62
+ txt = ax .annotate (" " + s , xy = xy , xycoords = txt ,
63
+ va = "bottom" , color = c , ** kwargs )
66
64
67
65
68
66
words = "all unicorns poop rainbows ! ! !" .split ()
69
67
colors = ['red' , 'orange' , 'gold' , 'lawngreen' , 'lightseagreen' , 'royalblue' ,
70
68
'blueviolet' ]
71
- plt .figure ( figsize = ( 8 , 8 ) )
72
- rainbow_text (0.1 , 0.05 , words , colors , size = 18 )
73
- rainbow_text (0.05 , 0.1 , words , colors , orientation = 'vertical' , size = 18 )
69
+ fig , ax = plt .subplots ( )
70
+ rainbow_text (0.02 , 0.02 , words , colors , size = 18 , transform = fig . transFigure )
71
+ rainbow_text (0.05 , 0.1 , words , colors , orientation = 'vertical' )
74
72
75
73
plt .show ()
0 commit comments