## matplotlib-users

 [Matplotlib-users] Labeling Contours From: Alexa Villaume - 2012-02-15 20:15:40 Attachments: Message as HTML ```Hi Everybody, I'm trying to label the contours of my contour plot following this example - http://matplotlib.sourceforge.net/examples/pylab_examples/contour_label_demo.html My actual code looks like this - import matplotlib matplotlib.use('PDF') from pylab import * import numpy as np # Define the surface of the plot metals=np.arange(-3.0, 1.1, 0.1) U=np.arange(-6.0, 0.25, 0.25) # Create the arrays that the data will be stored in o3=np.zeros([25,41]) o2=np.zeros([25,41]) c3=np.zeros([25,41]) mg2=np.zeros([25,41]) c3=np.zeros([25,41]) si2=np.zeros([25,41]) s3=np.zeros([25,41]) CS=plt.contourf(metals, U, o3, levels=[o3col-nsig*o3sig, o3col+nsig*o3sig], alpha=0.50, colors='#f88534') CS=plt.contourf(metals, U, o2, levels=[o2col-nsig*o2sig, o2col+nsig*o2sig], alpha=0.50, colors='#f2f34f') CS=plt.contourf(metals, U, c3, levels=[c3col-nsig*c3sig, c3col+nsig*c3sig], alpha=0.50, colors='#93d3f3') CS=plt.contourf(metals, U, mg2, levels=[mg2col-nsig*mg2sig, mg2col+nsig*mg2sig], alpha=0.50, colors='#ff536d') CS=plt.contourf(metals, U, s3, levels=[s3col-nsig*s3sig, s3col+nsig*s3sig], alpha=0.50, colors='#83c460') CS=plt.contourf(metals, U, si2, levels=[si2col-nsig*si2sig, si2col+nsig*si2sig], alpha=0.50, colors='black') # Trying to label the contours fmt = {} strs = [ 'O III', 'O II', 'C III', 'Mg II', 'S III', 'Si II'] for l,s in zip(levels, strs): fmt[l] = s plt.clabel(CS,levels[::2],inline=True,fmt=fmt,fontize=14) But I get an error that says that "levels" is not defined. What should I do? Thanks! Alexa ```
 Re: [Matplotlib-users] Labeling Contours From: Eric Firing - 2012-02-15 20:34:27 ```On 02/15/2012 10:15 AM, Alexa Villaume wrote: > Hi Everybody, > > > I'm trying to label the contours of my contour plot following this > example - > > http://matplotlib.sourceforge.net/examples/pylab_examples/contour_label_demo.html > > > My actual code looks like this - > > > import matplotlib > > matplotlib.use('PDF') > > frompylab import* > > import numpy as np > > > # Define the surface of the plot > > metals=np.arange(-3.0, 1.1, 0.1) > > U=np.arange(-6.0, 0.25, 0.25) > > > # Create the arrays that the data will be stored in > > o3=np.zeros([25,41]) > > o2=np.zeros([25,41]) > > c3=np.zeros([25,41]) > > mg2=np.zeros([25,41]) > > c3=np.zeros([25,41]) > > si2=np.zeros([25,41]) > > s3=np.zeros([25,41]) > > > CS=plt.contourf(metals, U, o3, levels=[o3col-nsig*o3sig, > o3col+nsig*o3sig], alpha=0.50, colors='#f88534') > > CS=plt.contourf(metals, U, o2, levels=[o2col-nsig*o2sig, > o2col+nsig*o2sig], alpha=0.50, colors='#f2f34f') > > CS=plt.contourf(metals, U, c3, levels=[c3col-nsig*c3sig, > c3col+nsig*c3sig], alpha=0.50, colors='#93d3f3') > > CS=plt.contourf(metals, U, mg2, levels=[mg2col-nsig*mg2sig, > mg2col+nsig*mg2sig], alpha=0.50, colors='#ff536d') > > CS=plt.contourf(metals, U, s3, levels=[s3col-nsig*s3sig, > s3col+nsig*s3sig], alpha=0.50, colors='#83c460') > > CS=plt.contourf(metals, U, si2, levels=[si2col-nsig*si2sig, > si2col+nsig*si2sig], alpha=0.50, colors='black') > > > > # Trying to label the contours > > > fmt = {} > > strs = [ 'O III', 'O II', 'C III', 'Mg II', 'S III', 'Si II'] > > for l,s in zip(levels, strs): > > fmt[l] = s > > plt.clabel(CS,levels[::2],inline=True,fmt=fmt,fontize=14) > > > But I get an error that says that "levels" is not defined. What should I do? Define levels! In your call to clabel, you are referencing a global "levels" which you did not define; what you did define is the levels attribute of each CS object. So probably what you want is something like: plt.clabel(CS, CS.levels[::2],inline=True,fmt=fmt,fontize=14) but you need one such call for each CS you create, if you want all of them labeled. Eric > > > Thanks! > > Alexa > > > > ------------------------------------------------------------------------------ > Virtualization& Cloud Management Using Capacity Planning > Cloud computing makes use of virtualization - but cloud computing > also focuses on allowing computing to be delivered as a service. > http://www.accelacomm.com/jaw/sfnl/114/51521223/ > > > > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users ```
 Re: [Matplotlib-users] Labeling Contours From: Alexa Villaume - 2012-02-16 01:10:50 Attachments: Message as HTML ```Hi Eric, Thanks for your help, I'm still really new to python and matplotlib. I've got my labels defined but now I'm having another problem with the formatting. I'm doing - CS1.level=[14.07, 14.27] plt.clabel(CS1,CS1.level[::2],inline=True,fmt='OIII',fontize=14) Where I get a type error that says that not all string arguments are getting converted during formatting. This is the full error, Traceback (most recent call last): File "ContourAttempt.py", line 81, in plt.clabel(CS1,CS1.level[::2],inline=True,fmt='OIII',fontize=14) # Something wrong with formmating File "/Users/alexavillaume/src/matplotlib/lib/matplotlib/pyplot.py", line 2176, in clabel ret = ax.clabel(CS, *args, **kwargs) File "/Users/alexavillaume/src/matplotlib/lib/matplotlib/axes.py", line 7326, in clabel return CS.clabel(*args, **kwargs) File "/Users/alexavillaume/src/matplotlib/lib/matplotlib/contour.py", line 217, in clabel self.labels(inline,inline_spacing) File "/Users/alexavillaume/src/matplotlib/lib/matplotlib/contour.py", line 624, in labels lw = self.get_label_width(lev, self.labelFmt, fsize) File "/Users/alexavillaume/src/matplotlib/lib/matplotlib/contour.py", line 284, in get_label_width lev = self.get_text(lev, fmt) File "/Users/alexavillaume/src/matplotlib/lib/matplotlib/contour.py", line 344, in get_text return fmt%lev TypeError: not all arguments converted during string formatting On Wed, Feb 15, 2012 at 10:34 AM, Eric Firing wrote: > On 02/15/2012 10:15 AM, Alexa Villaume wrote: > > Hi Everybody, > > > > > > I'm trying to label the contours of my contour plot following this > > example - > > > > > http://matplotlib.sourceforge.net/examples/pylab_examples/contour_label_demo.html > > > > > > My actual code looks like this - > > > > > > import matplotlib > > > > matplotlib.use('PDF') > > > > frompylab import* > > > > import numpy as np > > > > > > # Define the surface of the plot > > > > metals=np.arange(-3.0, 1.1, 0.1) > > > > U=np.arange(-6.0, 0.25, 0.25) > > > > > > # Create the arrays that the data will be stored in > > > > o3=np.zeros([25,41]) > > > > o2=np.zeros([25,41]) > > > > c3=np.zeros([25,41]) > > > > mg2=np.zeros([25,41]) > > > > c3=np.zeros([25,41]) > > > > si2=np.zeros([25,41]) > > > > s3=np.zeros([25,41]) > > > > > > CS=plt.contourf(metals, U, o3, levels=[o3col-nsig*o3sig, > > o3col+nsig*o3sig], alpha=0.50, colors='#f88534') > > > > CS=plt.contourf(metals, U, o2, levels=[o2col-nsig*o2sig, > > o2col+nsig*o2sig], alpha=0.50, colors='#f2f34f') > > > > CS=plt.contourf(metals, U, c3, levels=[c3col-nsig*c3sig, > > c3col+nsig*c3sig], alpha=0.50, colors='#93d3f3') > > > > CS=plt.contourf(metals, U, mg2, levels=[mg2col-nsig*mg2sig, > > mg2col+nsig*mg2sig], alpha=0.50, colors='#ff536d') > > > > CS=plt.contourf(metals, U, s3, levels=[s3col-nsig*s3sig, > > s3col+nsig*s3sig], alpha=0.50, colors='#83c460') > > > > CS=plt.contourf(metals, U, si2, levels=[si2col-nsig*si2sig, > > si2col+nsig*si2sig], alpha=0.50, colors='black') > > > > > > > > # Trying to label the contours > > > > > > fmt = {} > > > > strs = [ 'O III', 'O II', 'C III', 'Mg II', 'S III', 'Si II'] > > > > for l,s in zip(levels, strs): > > > > fmt[l] = s > > > > plt.clabel(CS,levels[::2],inline=True,fmt=fmt,fontize=14) > > > > > > But I get an error that says that "levels" is not defined. What should I > do? > > Define levels! > > In your call to clabel, you are referencing a global "levels" which you > did not define; what you did define is the levels attribute of each CS > object. So probably what you want is something like: > > plt.clabel(CS, CS.levels[::2],inline=True,fmt=fmt,fontize=14) > > but you need one such call for each CS you create, if you want all of > them labeled. > > > Eric > > > > > > > Thanks! > > > > Alexa > > > > > > > > > ------------------------------------------------------------------------------ > > Virtualization& Cloud Management Using Capacity Planning > > Cloud computing makes use of virtualization - but cloud computing > > also focuses on allowing computing to be delivered as a service. > > http://www.accelacomm.com/jaw/sfnl/114/51521223/ > > > > > > > > _______________________________________________ > > Matplotlib-users mailing list > > Matplotlib-users@... > > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > > > ------------------------------------------------------------------------------ > Virtualization & Cloud Management Using Capacity Planning > Cloud computing makes use of virtualization - but cloud computing > also focuses on allowing computing to be delivered as a service. > http://www.accelacomm.com/jaw/sfnl/114/51521223/ > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > ```
 Re: [Matplotlib-users] Labeling Contours From: Eric Firing - 2012-02-17 08:21:28 ```On 02/15/2012 03:10 PM, Alexa Villaume wrote: > Hi Eric, > > Thanks for your help, I'm still really new to python and matplotlib. > I've got my labels defined but now I'm having another problem with the > formatting. > > I'm doing - > > CS1.level=[14.07, 14.27] > > plt.clabel(CS1,CS1.level[::2],inline=True,fmt='OIII',fontize=14) Alexa, First, it looks like you might have slightly misunderstood my suggestion. When levels are specified as a kwarg for contour or contourf, they are saved as the "levels" attribute of the ContourSet object that is returned. So, one does not assign directly to that attribute. Second, clabel is intended for use with contour, not contourf, and can produce odd results with the ContourSet returned by a call to contourf. (This might be considered a bug; I only stumbled over it while trying to understand your problem. It had never occurred to me to try to use clabel with contourf. I think that either it should be made to work as one might reasonably expect, or clabel should raise an exception if given a ContourSet from contourf.) You can always use both contour and contourf as in this example: http://matplotlib.sourceforge.net/examples/pylab_examples/contourf_demo.html If you don't want the lines you can make them invisible and leave only the labels. > > > Where I get a type error that says that not all string arguments are > getting converted during formatting. This is the full error, > > Traceback (most recent call last): > > File "ContourAttempt.py", line 81, in > > plt.clabel(CS1,CS1.level[::2],inline=True,fmt='OIII',fontize=14) # > Something wrong with formmating > Third, in your line above, you are trying to set fmt to a string that is not a number formatting string; that is why you are getting the exception. The code is trying to use the fmt string to format a numeric level, but there is no % expression in it, so it can't. It looks like your earlier code, with the dictionary as fmt, was more along the right lines. I have never tried this option myself, though. If you run into another problem (or need more advice with the present problem) please try to provide a complete, self-contained example that illustrates it. That way someone else can run it, see exactly the problem, and show you exactly how to solve it. Or in the process of condensing the problem down to an example in a few lines, sometimes one sees the solution. Welcome to python and mpl! Eric > File "/Users/alexavillaume/src/matplotlib/lib/matplotlib/pyplot.py", > line 2176, in clabel > > ret = ax.clabel(CS, *args, **kwargs) > > File "/Users/alexavillaume/src/matplotlib/lib/matplotlib/axes.py", > line 7326, in clabel > > return CS.clabel(*args, **kwargs) > > File "/Users/alexavillaume/src/matplotlib/lib/matplotlib/contour.py", > line 217, in clabel > > self.labels(inline,inline_spacing) > > File "/Users/alexavillaume/src/matplotlib/lib/matplotlib/contour.py", > line 624, in labels > > lw = self.get_label_width(lev, self.labelFmt, fsize) > > File "/Users/alexavillaume/src/matplotlib/lib/matplotlib/contour.py", > line 284, in get_label_width > > lev = self.get_text(lev, fmt) > > File "/Users/alexavillaume/src/matplotlib/lib/matplotlib/contour.py", > line 344, in get_text > > return fmt%lev > > TypeError: not all arguments converted during string formatting > > > > > On Wed, Feb 15, 2012 at 10:34 AM, Eric Firing > wrote: > > On 02/15/2012 10:15 AM, Alexa Villaume wrote: > > Hi Everybody, > > > > > > I'm trying to label the contours of my contour plot following this > > example - > > > > > http://matplotlib.sourceforge.net/examples/pylab_examples/contour_label_demo.html > > > > > > My actual code looks like this - > > > > > > import matplotlib > > > > matplotlib.use('PDF') > > > > frompylab import* > > > > import numpy as np > > > > > > # Define the surface of the plot > > > > metals=np.arange(-3.0, 1.1, 0.1) > > > > U=np.arange(-6.0, 0.25, 0.25) > > > > > > # Create the arrays that the data will be stored in > > > > o3=np.zeros([25,41]) > > > > o2=np.zeros([25,41]) > > > > c3=np.zeros([25,41]) > > > > mg2=np.zeros([25,41]) > > > > c3=np.zeros([25,41]) > > > > si2=np.zeros([25,41]) > > > > s3=np.zeros([25,41]) > > > > > > CS=plt.contourf(metals, U, o3, levels=[o3col-nsig*o3sig, > > o3col+nsig*o3sig], alpha=0.50, colors='#f88534') > > > > CS=plt.contourf(metals, U, o2, levels=[o2col-nsig*o2sig, > > o2col+nsig*o2sig], alpha=0.50, colors='#f2f34f') > > > > CS=plt.contourf(metals, U, c3, levels=[c3col-nsig*c3sig, > > c3col+nsig*c3sig], alpha=0.50, colors='#93d3f3') > > > > CS=plt.contourf(metals, U, mg2, levels=[mg2col-nsig*mg2sig, > > mg2col+nsig*mg2sig], alpha=0.50, colors='#ff536d') > > > > CS=plt.contourf(metals, U, s3, levels=[s3col-nsig*s3sig, > > s3col+nsig*s3sig], alpha=0.50, colors='#83c460') > > > > CS=plt.contourf(metals, U, si2, levels=[si2col-nsig*si2sig, > > si2col+nsig*si2sig], alpha=0.50, colors='black') > > > > > > > > # Trying to label the contours > > > > > > fmt = {} > > > > strs = [ 'O III', 'O II', 'C III', 'Mg II', 'S III', 'Si II'] > > > > for l,s in zip(levels, strs): > > > > fmt[l] = s > > > > plt.clabel(CS,levels[::2],inline=True,fmt=fmt,fontize=14) > > > > > > But I get an error that says that "levels" is not defined. What > should I do? > > Define levels! > > In your call to clabel, you are referencing a global "levels" which you > did not define; what you did define is the levels attribute of each CS > object. So probably what you want is something like: > > plt.clabel(CS, CS.levels[::2],inline=True,fmt=fmt,fontize=14) > > but you need one such call for each CS you create, if you want all of > them labeled. > > > Eric > > > > > > > Thanks! > > > > Alexa ```