HOW TO

Stack labels from a single field using a Python expression

Last Published: April 25, 2020

Summary

A feature can be labeled to contain descriptive text on the feature, by displaying the values in a certain field from the attribute table. In some cases, it is desirable to stack labels from a single field to reduce its horizontal length and to provide better a view, similar to the concept of word wrap. This is possible in ArcMap using the Label Manager in the Labeling toolbar with the Python expression.

Procedure

The following steps describe how to stack labels from a single field:

  1. Enable the Labeling toolbar. Click Customize > Toolbars > Labeling.
  2. In the Labeling toolbar, click the Label Manager The image of Label Manager icon. icon.
  3. Select the desired feature in the Label Classes segment.
  4. Click Expression.
The image of the Label Manager window
  1. In the Label Expression window, check the Advanced box.
  2. In the Parser drop-down list, select the Python parser.
  3. In the Expression text box, type the following Python expression.
def FindLabel ([Field_Name]):
  x = [Field_Name]
  x1 = x.split(" ")
  if len(x1)>1:
    return x1[0] + '\n' + x1[1]
  else:
    return x1[0] 
Note:
The array parameter x1[n] can be added and modified according to the number of words present in the value. For example:

return x1[0] + '\n' + x1[1] + '\n' + x1[2]
The image of the Label Expression window
  1. Click OK.
  2. Click Apply on the Label Manager window, and click OK. The following images are sample of the before and after result.
  The image of the sample result

 

Article ID:000021501

Software:
  • ArcMap

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options