Clean up and move text rotation example #8066

Merged
merged 4 commits into from Feb 25, 2017
@@ -1,44 +0,0 @@
-"""
-The way matplotlib does text layout is counter-intuitive to some, so
-this example is designed to make it a little clearer. The text is
-aligned by it's bounding box (the rectangular box that surrounds the
-ink rectangle). The order of operations is basically rotation then
-alignment, rather than alignment then rotation. Basically, the text
-is centered at your x,y location, rotated around this point, and then
-aligned according to the bounding box of the rotated text.
-
-So if you specify left, bottom alignment, the bottom left of the
-bounding box of the rotated text will be at the x,y coordinate of the
-text.
-
-But a picture is worth a thousand words!
-"""
-
-import matplotlib.pyplot as plt
-import numpy as np
-
-
-def addtext(props):
- plt.text(0.5, 0.5, 'text 0', props, rotation=0)
- plt.text(1.5, 0.5, 'text 45', props, rotation=45)
- plt.text(2.5, 0.5, 'text 135', props, rotation=135)
- plt.text(3.5, 0.5, 'text 225', props, rotation=225)
- plt.text(4.5, 0.5, 'text -45', props, rotation=-45)
- plt.yticks([0, .5, 1])
- plt.grid(True)
-
-# the text bounding box
-bbox = {'fc': '0.8', 'pad': 0}
-
-plt.subplot(211)
-addtext({'ha': 'center', 'va': 'center', 'bbox': bbox})
-plt.xlim(0, 5)
-plt.xticks(np.arange(0, 5.1, 0.5), [])
-plt.ylabel('center / center')
-
-plt.subplot(212)
-addtext({'ha': 'left', 'va': 'bottom', 'bbox': bbox})
-plt.xlim(0, 5)
-plt.xticks(np.arange(0, 5.1, 0.5))
-plt.ylabel('left / bottom')
-plt.show()
@@ -92,6 +92,7 @@
files['text_labels_and_annotations'] = [
'text_demo_fontdict.py',
+ 'text_rotation.py',
'unicode_demo.py',
]
@@ -236,7 +237,6 @@
'subplots_adjust.py',
'symlog_demo.py',
'table_demo.py',
- 'text_rotation.py',
'text_rotation_relative_to_line.py',
'transoffset.py',
'xcorr_demo.py',
@@ -0,0 +1,51 @@
+"""
+===================================
+Default text rotation demonstration
+===================================
+
+The way Matplotlib does text layout by default is counter-intuitive to some, so
+this example is designed to make it a little clearer.
+
+The text is aligned by its bounding box (the rectangular box that surrounds the
+ink rectangle). The order of operations is rotation then alignment.
+Basically, the text is centered at your x,y location, rotated around this
+point, and then aligned according to the bounding box of the rotated text.
+
+So if you specify left, bottom alignment, the bottom left of the
+bounding box of the rotated text will be at the x,y coordinate of the
+text.
+
+But a picture is worth a thousand words!
+"""
+
+import matplotlib.pyplot as plt
+import numpy as np
+
+
+def addtext(ax, props):
@QuLogic

QuLogic Feb 24, 2017

Member

This function does more than just add text.

+ ax.text(0.5, 0.5, 'text 0', props, rotation=0)
@QuLogic

QuLogic Feb 24, 2017

Member

How about putting this in the loop too? for i, angle in enumerate([0, 45, 135, 225, -45]):

+ ax.text(1.5, 0.5, 'text 45', props, rotation=45)
+ ax.text(2.5, 0.5, 'text 135', props, rotation=135)
+ ax.text(3.5, 0.5, 'text 225', props, rotation=225)
+ ax.text(4.5, 0.5, 'text -45', props, rotation=-45)
+ for x in range(0, 5):
+ ax.scatter(x + 0.5, 0.5, color='r', alpha=0.5)
+ ax.set_yticks([0, .5, 1])
+ ax.set_xlim(0, 5)
+ ax.grid(True)
+
+
+# the text bounding box
+bbox = {'fc': '0.8', 'pad': 0}
+
+fig, axs = plt.subplots(2, 1)
+
+addtext(axs[0], {'ha': 'center', 'va': 'center', 'bbox': bbox})
+axs[0].set_xticks(np.arange(0, 5.1, 0.5), [])
@QuLogic

QuLogic Feb 24, 2017

Member

This could be done in the (suitably-renamed) addtext.

+axs[0].set_ylabel('center / center')
+
+addtext(axs[1], {'ha': 'left', 'va': 'bottom', 'bbox': bbox})
+axs[1].set_xticks(np.arange(0, 5.1, 0.5))
+axs[1].set_ylabel('left / bottom')
+
+plt.show()