17

Up until recently I have been using Mathematica for my plots. Although it was a real pain and everything had to be done manually, the results where very close to what I wanted. One example is the following:

Mathematica example plot

I really like the grey rounded rectangle in the background of the colorbar. While everything had to be adjusted manually in Mathematica, matplotlib is a lot more automatic and already produced nice results.

Mathematica example plot

But there are still two problems I have:

  1. I don't know how to do a rounded rectangle in the background. I looked at the fancybbox patch but didn't get it to work with the colorbar as I want it to. What is the best way to get something like the Mathematica box? For the legend in plots there seems to be a fancy bbox option... but not for colorbars
  2. When I use the "lesser sign" (<) the label of colorbar moves too far to the right. How can I adjust this (maybe even "in-between" the numbers as in the Mathematica plot)?

I am looking forward to any suggestions pointing in the right direction :).

oli
  • 659
  • 1
  • 6
  • 18

1 Answers1

32

To your second question: you can use a negative labelpad value to move the label back towards the ticklabels, like this:

import numpy as np
import matplotlib.pyplot as plt

data = np.linspace(0, 10, num=256).reshape(16,16) 

cf = plt.contourf(data, levels=(0, 2.5, 5, 7.5, 10))
cb = plt.colorbar(cf)

cb.set_ticklabels([r'$<10^{0}$', 1, 2, r'$10^{14}$', r'$10^{14}+12345678$'])
cb.set_label(r'$n_e$ in $m^{-3}$', labelpad=-40, y=0.45)

plt.show()

Using the parameter y, you can additionally move the label up or down for better symmetry.

The argument of labelpad is given in points (1/72 inch). y accepts values in [0, 1], 0.0 is the lower border and 1.0 the upper.

The result:

Script Output

sodd
  • 12,482
  • 3
  • 54
  • 62
mwil.me
  • 1,134
  • 1
  • 19
  • 33
  • 1
    `y` is not restricted to the interval `[0.0, 1.0]`. It can take any float/integer. If `y > 1.0` the label will be placed _above_ the top of the axis, and if `y < 0.0` it will be placed _below_ the bottom of the axis. The axis coordinates are _normalized_, meaning `0.0` is the "start" and `1.0` the "end" of the axis' extent. – sodd Aug 24 '13 at 20:33
  • Just to be clear: the `[0, 1]` is _interval notation_, not list notation. Thus this means from `0` to `1`, inclusive. This applies both to the answer and the comment above. – sodd Aug 25 '13 at 09:31
  • Could you tell me where do you find these parameters like `y` and `labelpad` because I did not find them either in [documentation](https://matplotlib.org/3.1.1/api/colorbar_api.html#matplotlib.colorbar.ColorbarBase.set_label) or source code? – Fei Yao Oct 30 '19 at 17:17
  • I accidentally found that `cb.set_label` is actually a [text](https://matplotlib.org/3.1.1/api/text_api.html#matplotlib.text.Text) and hence `y` is text properties. Still wondering how to determine more details of `**kw` found in this [page](https://matplotlib.org/3.1.1/api/colorbar_api.html#matplotlib.colorbar.ColorbarBase.set_label). – Fei Yao Oct 30 '19 at 17:29