SF.net SVN: matplotlib:[6363] trunk/matplotlib/examples/pylab_examples/ text_rotation_relative_to_line.py

Thanks, David. That's a much-needed feature.

However, wouldn't it be simpler, API-wise, to add a new kwarg "rotation_data" (or some better name) which would be an angle in data space? (Or alternatively a boolean flag "rotation_in_data_coords"). The other advantage of that approach is that since the Text object knows what the "purpose" of the angle is, it could update the angle when the limits or figure size are changed.

It looks like the heavy lifting of the calculation is already there...


dmkaplan@...189... wrote:


Revision: 6363
Author: dmkaplan
Date: 2008-11-05 14:43:29 +0000 (Wed, 05 Nov 2008)

Log Message:
Adding a small script that demonstrates the utility of transform_angles method added in last
commit (from dmkaplan).

Added Paths:

Added: trunk/matplotlib/examples/pylab_examples/text_rotation_relative_to_line.py

--- trunk/matplotlib/examples/pylab_examples/text_rotation_relative_to_line.py (rev 0)
+++ trunk/matplotlib/examples/pylab_examples/text_rotation_relative_to_line.py 2008-11-05 14:43:29 UTC (rev 6363)
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+Text objects in matplotlib are normally rotated with respect to the
+screen coordinate system (i.e., 45 degrees rotation plots text along a
+line that is inbetween horizontal and vertical no matter how the axes
+are changed). However, at times one wants to rotate text with respect
+to something on the plot. In this case, the correct angle won't be
+the angle of that object in the plot coordinate system, but the angle
+that that object APPEARS in the screen coordinate system. This angle
+is found by transforming the angle from the plot to the screen
+coordinate system, as shown in the example below.
+from pylab import *
+# Plot diagonal line (45 degrees)
+h = plot( r_[:10], r_[:10] )
+# set limits so that it no longer looks on screen to be 45 degrees
+# Locations to plot text
+l1 = array((1,1))
+l2 = array((5,5))
+# Rotate angle
+angle = 45
+trans_angle = gca().transData.transform_angles(array((45,)),
+ l2.reshape((1,2)))[0]
+# Plot text
+th1 = text(l1[0],l1[1],'text not rotated correctly',fontsize=16,
+ rotation=angle)
+th2 = text(l2[0],l2[1],'text not rotated correctly',fontsize=16,
+ rotation=trans_angle)

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.

This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
Matplotlib-checkins mailing list
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA