trying to figure out how to add path effects to my axis tick labels in a plot.. I know how to do it for the axis label:
cb.set_label('Water Surface Elevation [m]', fontsize=18, path_effects=[pe.withStroke(linewidth=1, foreground="b")])
assuming cb
is a fig.colorbar
object and pe
is matplotlib.patheffects
but cannot figure out for the tick labels? anybody know how? thanks
CodePudding user response:
You can pass the argument path_effects=[pe.withStroke(linewidth=1, foreground="b")]
to cb.ax.set_yticklabels
. For example:
import numpy as np
from numpy.random import randn
import matplotlib.pyplot as plt
from matplotlib import patheffects as pe
from matplotlib import cm
# Fixing random state for reproducibility
np.random.seed(42)
# Make plot with vertical (default) colorbar
fig, ax = plt.subplots()
data = np.clip(randn(250, 250), -1, 1)
cax = ax.imshow(data, cmap=cm.coolwarm)
ax.set_title('Gaussian noise with vertical colorbar')
# Add colorbar, make sure to specify tick locations to match desired ticklabels
cb = fig.colorbar(cax, ticks=[-1, 0, 1])
cb.ax.set_yticklabels(['< -1', '0', '> 1', ], fontsize=18, path_effects=[pe.withStroke(linewidth=1, foreground="b")]) # vertically oriented colorbar
plt.show()