Matplotlib, usetex: Colorbar's font does not match plot's font
<p>I have a matplotlib plot with a colorbar and can't seem to figure out how to match the fonts of the plot and the colorbar.</p>

<p>I try to use usetex for text handling, but it seems like only the ticks of the plot are affected, not the ticks of the colorbar.
Also, I have searched for solution quite a bit, so in the following code sample, there are a few different attempts included, but the result still is a bold font. Minimum failing code sample:</p>

<pre><code>import matplotlib as mpl
import matplotlib.pyplot as plt
import sys
import colorsys
import numpy as np


mpl.rc('text', usetex=True)
plt.rcParams["font.family"] = "Times New Roman"
plt.rcParams["font.weight"] = 100
plt.rcParams["axes.labelweight"] = 100
plt.rcParams["figure.titleweight"] = 100


def draw():
    colors = [colorsys.hsv_to_rgb(0.33, step /15, 1) for step in [2, 5, 8, 11, 14]]
    mymap = mpl.colors.LinearSegmentedColormap.from_list('value',colors, N=5)

    Z = [[0,0],[0,0]]
    levels = range(2,15+4,3)
    CS3 = plt.contourf(Z, levels, cmap=mymap)
    plt.clf()

    plt.figure(figsize=(20, 15))
    plt.gca().set_aspect('equal')

    cbar = plt.colorbar(CS3, ax=plt.gca(), shrink=0.5, fraction=0.5, aspect=30, pad=0.05, orientation="horizontal")

    cbar.set_ticks([1.5 + x for x in [2,5,8,11,14]])

    # attempts to make the fonts look the same
    cbar.ax.set_xticklabels([1,2,3,4,5], weight="light", fontsize=16)
    cbar.set_label("value", weight="ultralight", fontsize=32)
    plt.setp(cbar.ax.xaxis.get_ticklabels(), weight='ultralight', fontsize=16)

    for l in cbar.ax.xaxis.get_ticklabels():
        l.set_weight("light")

    plt.show()

draw()
</code></pre>

<p>Unfortunately, the fonts don't look the same at all. Picture:</p>

<p><a href="http://i.stack.imgur.com/3jNq4.png" rel="nofollow"><img src="http://i.stack.imgur.com/3jNq4.png" alt="plot font and colorbar font are different"></a></p>

<p>I'm sure it's just a stupid misunderstanding of usetex on my part. Why does the usetex not seem to handle the colormap's font?</p>

<p>Thanks!</p>

<p>The issue here is that MPL wraps ticklabels in <code>$</code> when <code>usetex=True</code>. This causes them to have different sizes than text that is not wrapped in <code>$</code>. When you force your ticks to be labeled with <code>[1,2,3,4,5]</code> these values are not wrapped in <code>$</code>, and are therefore rendered differently. I don't know the details of <em>why</em> this is, but I've run into the problem before myself.</p>

<p>I'd recommend wrapping your colorbar ticks in <code>$</code>, for example:</p>

<pre><code>cbar.ax.set_xticklabels(['${}$'.format(tkval) for tkval in [1, 2, 3, 4, 5]])
</code></pre>

<p>From there you should be able to figure out how to adjust font sizes/weights so that they match and you get what you want.</p>

python matplotlib fonts styles tex