5

Can someone give me an example of how to use the following tickFormatters. The docs are uninformative to me.

ticker.StrMethodFormatter() ticker.IndexFormatter()

for example I might think that

x = np.array([ 316566.962,  294789.545,  490032.382,  681004.044,  753757.024,
            385283.153,  651498.538,  937628.225,  199561.358,  601465.455])
y = np.array([ 208.075,  262.099,  550.066,  633.525,  612.804,  884.785,
            862.219,  349.805,  279.964,  500.612])
money_formatter = tkr.StrMethodFormatter('${:,}')

plt.scatter(x,y)
ax = plt.gca()
fmtr = ticker.StrMethodFormatter('${:,}')
ax.xaxis.set_major_formatter(fmtr)

would set my tick labels to be dollar signed and comma sep for thousands places ala

['$300,000', '$400,000', '$500,000', '$600,000', '$700,000', '$800,000', '$900,000']

but instead I get an index error.

IndexError: tuple index out of range

For IndexFormatter docs say:

Set the strings from a list of labels

don't really know what this means and when I try to use it my tics disappear.

RSHAP
  • 2,337
  • 3
  • 28
  • 39
  • 1
    Try to provide a complete example of what you have, what it produces, and explaining what you would like to produce instead. – Gabriel May 25 '17 at 18:22

1 Answers1

8

The StrMethodFormatter works indeed by supplying a string that can be formatted using the format method. So the approach of using '${:,}' goes in the right direction.

However from the documentation we learn

The field used for the value must be labeled x and the field used for the position must be labeled pos.

This means that you need to give an actual label x to the field. Additionally you may want to specify the number format as g not to have the decimal point.

fmtr = matplotlib.ticker.StrMethodFormatter('${x:,g}')

enter image description here

The IndexFormatter is of little use here. As you found out, you would need to provide a list of labels. Those labels are used for the index, starting at 0. So using this formatter would require to have the x axis start at zero and ranging over some whole numbers.

Example:

plt.scatter(range(len(y)),y)
fmtr = matplotlib.ticker.IndexFormatter(list("ABCDEFGHIJ"))
ax.xaxis.set_major_formatter(fmtr)

enter image description here

Here, the ticks are placed at (0,2,4,6,....) and the respective letters from the list (A, C, E, G, I, ...) are used as labels.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Ok I think I get it. so the string `"{x:,}"` is saying we want variable x (being passed to the format call by mpl) to be formatted with thousands separators. Now my only question is what is the g? is that like specifying f for float? if so what are the other options there? – RSHAP May 25 '17 at 19:12
  • `g` is a general numeric format, which rounds off the decimal point if needed. You don't need to use it here; leave it out if you like. You can read more on string formatting in the [official python docs](https://docs.python.org/3/library/string.html#format-specification-mini-language). – ImportanceOfBeingErnest May 25 '17 at 19:21
  • IndexFormatter is no longer available in Matplotlib. Use [FuncFormatter](https://matplotlib.org/stable/api/ticker_api.html#matplotlib.ticker.FuncFormatter) instead. – Jonathon Vandezande Jul 12 '22 at 15:25