[Matplotlib-users] clabel return values
Is the list of return values from the clabel() function supposed to represent the position and orientation of the contour labels? I have a script below where I try to re-draw the contour labels using the Text objects in the list returned from clabel(), and they do not line up in my output. I'm using Matplotlib version 0.98.5.1, revision 6622. If this is intentional, is there some way of retrieving the actual position/orientation of the contour labels? I'm trying to make a drop-shadow effect for those labels, and I need to know where they are exactly for my code to work. Sample script: #!/usr/bin/env python import matplotlib import numpy as np import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pyplot as plt import math matplotlib.rcParams['xtick.direction'] = 'out' matplotlib.rcParams['ytick.direction'] = 'out' delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.0, delta) X, Y = np.meshgrid(x, y) Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) # difference of Gaussians Z = 10.0 * (Z2 - Z1) f=plt.figure(facecolor='g') ax = plt.gca() ax.set_axis_bgcolor('g') CS = plt.contour(X, Y, Z) xt = plt.xticks() yt = plt.yticks() fontsize = 10 fontdict2 = {'fontweight':'light', 'color': 'r', 'fontsize':fontsize} labels = plt.clabel(CS, inline=1, fontsize=fontsize,colors='k') for label in labels: ltext = label.get_text() lrot = label.get_rotation() lx,ly = label.get_position() plt.text(lx,ly,ltext,fontdict2,rotation=lrot) plt.savefig('output.png') -- ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] clabel return values
I guess you're missing vertical and horizontal alignment. Also, your font properties were not set correctly. The 4th argument of the text function is a color. fontdict2 = {'fontweight':'light', 'color': 'r', 'fontsize':fontsize} fp = FontProperties(fontdict2) labels = plt.clabel(CS, inline=1, fontsize=fontsize,colors='k') for label in labels: ltext = label.get_text() lx,ly = label.get_position() lrot=label.get_rotation() va, ha = label.get_va(), label.get_ha() t = plt.text(lx,ly,ltext, fontproperties=fp, rotation=lrot,va=va, ha=ha) You may simply use update_from() method. Anyhow, I'm not sure if you can put dorpshadow effect this way. Changing font weight usually changes the overall size of each glyph. Regards, -JJ On Tue, Jan 6, 2009 at 10:20 AM, Michael Hearne mhea...@usgs.gov wrote: Is the list of return values from the clabel() function supposed to represent the position and orientation of the contour labels? I have a script below where I try to re-draw the contour labels using the Text objects in the list returned from clabel(), and they do not line up in my output. I'm using Matplotlib version 0.98.5.1, revision 6622. If this is intentional, is there some way of retrieving the actual position/orientation of the contour labels? I'm trying to make a drop-shadow effect for those labels, and I need to know where they are exactly for my code to work. Sample script: #!/usr/bin/env python import matplotlib import numpy as np import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pyplot as plt import math matplotlib.rcParams['xtick.direction'] = 'out' matplotlib.rcParams['ytick.direction'] = 'out' delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.0, delta) X, Y = np.meshgrid(x, y) Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) # difference of Gaussians Z = 10.0 * (Z2 - Z1) f=plt.figure(facecolor='g') ax = plt.gca() ax.set_axis_bgcolor('g') CS = plt.contour(X, Y, Z) xt = plt.xticks() yt = plt.yticks() fontsize = 10 fontdict2 = {'fontweight':'light', 'color': 'r', 'fontsize':fontsize} labels = plt.clabel(CS, inline=1, fontsize=fontsize,colors='k') for label in labels: ltext = label.get_text() lrot = label.get_rotation() lx,ly = label.get_position() plt.text(lx,ly,ltext,fontdict2,rotation=lrot) plt.savefig('output.png') -- ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users -- Check out the new SourceForge.net Marketplace. It is the best place to buy or sell services for just about anything Open Source. http://p.sf.net/sfu/Xq1LFB ___ Matplotlib-users mailing list Matplotlib-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/matplotlib-users
Re: [Matplotlib-users] clabel return values
Jae-Joon - Thank you for your suggestion. I wasn't aware I needed the alignments. However, when I try your sample code in my script, I get a sequence of rendering errors if I use show() or savefig(): Traceback (most recent call last): File /Library/Python/2.5/site-packages/matplotlib-0.98.5.1-py2.5-macosx-10.5-i386.egg/matplotlib/figure.py, line 772, in draw for a in self.axes: a.draw(renderer) File /Library/Python/2.5/site-packages/matplotlib-0.98.5.1-py2.5-macosx-10.5-i386.egg/matplotlib/axes.py, line 1601, in draw a.draw(renderer) File /Library/Python/2.5/site-packages/matplotlib-0.98.5.1-py2.5-macosx-10.5-i386.egg/matplotlib/text.py, line 450, in draw bbox, info = self._get_layout(renderer) File /Library/Python/2.5/site-packages/matplotlib-0.98.5.1-py2.5-macosx-10.5-i386.egg/matplotlib/text.py, line 246, in _get_layout 'lp', self._fontproperties, ismath=False) File /Library/Python/2.5/site-packages/matplotlib-0.98.5.1-py2.5-macosx-10.5-i386.egg/matplotlib/backends/backend_macosx.py, line 123, in get_text_width_height_descent return self.gc.get_text_width_height_descent(unicode(s), family, size, weight, style) RuntimeError: ATSUSetAttributes failed My new script is below: #!/usr/bin/env python import matplotlib import numpy as np import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pyplot as plt import math matplotlib.rcParams['xtick.direction'] = 'out' matplotlib.rcParams['ytick.direction'] = 'out' delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.0, delta) X, Y = np.meshgrid(x, y) Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) # difference of Gaussians Z = 10.0 * (Z2 - Z1) f=plt.figure(facecolor='g') ax = plt.gca() ax.set_axis_bgcolor('g') CS = plt.contour(X, Y, Z) xt = plt.xticks() yt = plt.yticks() fontsize = 10 fontdict2 = {'fontweight':'light', 'color': 'r', 'fontsize':fontsize} fp = matplotlib.font_manager.FontProperties(fontdict2) labels = plt.clabel(CS, inline=1, fontsize=fontsize,colors='k') for label in labels: ltext = label.get_text() lx,ly = label.get_position() lrot=label.get_rotation() va, ha = label.get_va(), label.get_ha() t = plt.text(lx,ly,ltext, fontproperties=fp, rotation=lrot,va=va, ha=ha) plt.savefig('output.png') Jae-Joon Lee wrote: I guess you're missing vertical and horizontal alignment. Also, your font properties were not set correctly. The 4th argument of the text function is a color. fontdict2 = {'fontweight':'light', 'color': 'r', 'fontsize':fontsize} fp = FontProperties(fontdict2) labels = plt.clabel(CS, inline=1, fontsize=fontsize,colors='k') for label in labels: ltext = label.get_text() lx,ly = label.get_position() lrot=label.get_rotation() va, ha = label.get_va(), label.get_ha() t = plt.text(lx,ly,ltext, fontproperties=fp, rotation=lrot,va=va, ha=ha) You may simply use update_from() method. Anyhow, I'm not sure if you can put dorpshadow effect this way. Changing font weight usually changes the overall size of each glyph. Regards, -JJ On Tue, Jan 6, 2009 at 10:20 AM, Michael Hearne mhea...@usgs.gov wrote: Is the list of return values from the clabel() function supposed to represent the position and orientation of the contour labels? I have a script below where I try to re-draw the contour labels using the Text objects in the list returned from clabel(), and they do not line up in my output. I'm using Matplotlib version 0.98.5.1, revision 6622. If this is intentional, is there some way of retrieving the actual position/orientation of the contour labels? I'm trying to make a drop-shadow effect for those labels, and I need to know where they are exactly for my code to work. Sample script: #!/usr/bin/env python import matplotlib import numpy as np import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pyplot as plt import math matplotlib.rcParams['xtick.direction'] = 'out' matplotlib.rcParams['ytick.direction'] = 'out' delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.0, delta) X, Y = np.meshgrid(x, y) Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) # difference of Gaussians Z = 10.0 * (Z2 - Z1) f=plt.figure(facecolor='g') ax = plt.gca() ax.set_axis_bgcolor('g') CS = plt.contour(X, Y, Z) xt = plt.xticks() yt = plt.yticks() fontsize = 10 fontdict2 = {'fontweight':'light', 'color': 'r', 'fontsize':fontsize} labels = plt.clabel(CS, inline=1, fontsize=fontsize,colors='k') for label in labels: ltext = label.get_text() lrot = label.get_rotation() lx,ly = label.get_position() plt.text(lx,ly,ltext,fontdict2,rotation=lrot) plt.savefig('output.png')
Re: [Matplotlib-users] clabel return values
I can't reproduce the error and I guess it is back end specific. Anyhow, the fontproperties object were created incorrectly and this may be cause of the problem. I just thought your original dictionary was in a correct form. So, test your code w/o fontproperties and see if your text is placed in a correct position. Then you need to create the fontproperties in a correct way. Please take a look at FontProperties document and revise your code accordingly. It should be something like FontProperties(weight=light, size=fontsize) There is no fontweight keyword. And the color needs to be specified as a parameters of the text function. I don't have much clue on how font selection work in the mpl, but you may need some tweaking on that. Regards, -JJ On Tue, Jan 6, 2009 at 12:46 PM, Michael Hearne mhea...@usgs.gov wrote: Jae-Joon - Thank you for your suggestion. I wasn't aware I needed the alignments. However, when I try your sample code in my script, I get a sequence of rendering errors if I use show() or savefig(): Traceback (most recent call last): File /Library/Python/2.5/site-packages/matplotlib-0.98.5.1-py2.5-macosx-10.5-i386.egg/matplotlib/figure.py, line 772, in draw for a in self.axes: a.draw(renderer) File /Library/Python/2.5/site-packages/matplotlib-0.98.5.1-py2.5-macosx-10.5-i386.egg/matplotlib/axes.py, line 1601, in draw a.draw(renderer) File /Library/Python/2.5/site-packages/matplotlib-0.98.5.1-py2.5-macosx-10.5-i386.egg/matplotlib/text.py, line 450, in draw bbox, info = self._get_layout(renderer) File /Library/Python/2.5/site-packages/matplotlib-0.98.5.1-py2.5-macosx-10.5-i386.egg/matplotlib/text.py, line 246, in _get_layout 'lp', self._fontproperties, ismath=False) File /Library/Python/2.5/site-packages/matplotlib-0.98.5.1-py2.5-macosx-10.5-i386.egg/matplotlib/backends/backend_macosx.py, line 123, in get_text_width_height_descent return self.gc.get_text_width_height_descent(unicode(s), family, size, weight, style) RuntimeError: ATSUSetAttributes failed My new script is below: #!/usr/bin/env python import matplotlib import numpy as np import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pyplot as plt import math matplotlib.rcParams['xtick.direction'] = 'out' matplotlib.rcParams['ytick.direction'] = 'out' delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.0, delta) X, Y = np.meshgrid(x, y) Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) # difference of Gaussians Z = 10.0 * (Z2 - Z1) f=plt.figure(facecolor='g') ax = plt.gca() ax.set_axis_bgcolor('g') CS = plt.contour(X, Y, Z) xt = plt.xticks() yt = plt.yticks() fontsize = 10 fontdict2 = {'fontweight':'light', 'color': 'r', 'fontsize':fontsize} fp = matplotlib.font_manager.FontProperties(fontdict2) labels = plt.clabel(CS, inline=1, fontsize=fontsize,colors='k') for label in labels: ltext = label.get_text() lx,ly = label.get_position() lrot=label.get_rotation() va, ha = label.get_va(), label.get_ha() t = plt.text(lx,ly,ltext, fontproperties=fp, rotation=lrot,va=va, ha=ha) plt.savefig('output.png') Jae-Joon Lee wrote: I guess you're missing vertical and horizontal alignment. Also, your font properties were not set correctly. The 4th argument of the text function is a color. fontdict2 = {'fontweight':'light', 'color': 'r', 'fontsize':fontsize} fp = FontProperties(fontdict2) labels = plt.clabel(CS, inline=1, fontsize=fontsize,colors='k') for label in labels: ltext = label.get_text() lx,ly = label.get_position() lrot=label.get_rotation() va, ha = label.get_va(), label.get_ha() t = plt.text(lx,ly,ltext, fontproperties=fp, rotation=lrot,va=va, ha=ha) You may simply use update_from() method. Anyhow, I'm not sure if you can put dorpshadow effect this way. Changing font weight usually changes the overall size of each glyph. Regards, -JJ On Tue, Jan 6, 2009 at 10:20 AM, Michael Hearne mhea...@usgs.gov wrote: Is the list of return values from the clabel() function supposed to represent the position and orientation of the contour labels? I have a script below where I try to re-draw the contour labels using the Text objects in the list returned from clabel(), and they do not line up in my output. I'm using Matplotlib version 0.98.5.1, revision 6622. If this is intentional, is there some way of retrieving the actual position/orientation of the contour labels? I'm trying to make a drop-shadow effect for those labels, and I need to know where they are exactly for my code to work. Sample script: #!/usr/bin/env python import matplotlib import numpy as np import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pyplot as plt import math matplotlib.rcParams['xtick.direction'] = 'out'