Home > Net >  What are the input options for Matplotlib's colorbar "format" parameter?
What are the input options for Matplotlib's colorbar "format" parameter?

Time:07-06

Matplotlib's colorbar has format as one of it's parameters. According to the documentation, this parameters can be either a string or a Formatter, but no further details are given. What are the standard options that can be passed as a string and will be understood by the colorbar or where are they listed?

Although I'm sure this information is somewhere in the documentation, I couldn't find it there or on another SO answer. I think this question can be useful to make it easier to find for other users in the future.

CodePudding user response:

The format strings are addressed in the matplotlib formatter documentation in the matplotlib.ticker.FormatStrFormatter(fmt) section. String formats are used the same way as in other languages like C, in which ".2f" will produce a number with two decimals showed as a string in your colormap. E.g

fig, ax = plt.subplots(1)
ax.plot(x,y)
norm = colors.Normalize(vmin=-2, vmax=2)
fig.colorbar(mappable = colormap.ScalarMappable(norm, colormap.cool), format = '%.2f')

will show 2 and

fig, ax = plt.subplots(1)
ax.plot(x,y)
norm = colors.Normalize(vmin=-2, vmax=2)
fig.colorbar(mappable = colormap.ScalarMappable(norm, colormap.cool), format = '%.4f m/s')

will show 3

  • Related