I would like to change the ticks locators and labels in the colorbar of the following plot.
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import dates as mdates
import numpy as np
# fontdict to control style of text and labels
font = {'family': 'serif',
'color': (0.33, 0.33, 0.33),
'weight': 'normal',
'size': 18,
}
num = 1000
x = np.linspace(-4,4,num) + (0.5 - np.random.rand(num))
y = np.linspace(-2,2,num) + (0.5 - np.random.rand(num))
t = pd.date_range('1/1/2014', periods=num)
# make plot with vertical (default) colorbar
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(6, 6))
ax.set_title('Scatter plot', fontdict=font)
# plot data
s = ax.scatter(x = x, y = y,
s=50, c=t, marker='o',
cmap=plt.cm.rainbow)
# plot settings
ax.grid(True)
ax.set_aspect('equal')
ax.set_ylabel('Northing [cm]', fontdict=font)
ax.set_xlabel('Easting [cm]', fontdict=font)
# add colorbar
cbar = fig.colorbar(mappable=s, ax=ax)
cbar.set_label('Date')
# change colobar ticks labels and locators
????
The colorbar illustrates the time dependency. Thus, I would like to change the ticks from their numerical values (nanoseconds?) to more sensible date format like months and year (e.g., %b%Y or %Y-%m) where the interval could be for example 3 or 6 months. Is that possible?
I tried to play unsuccessfully with cbar.formatter, cbar.locator and mdates.
You can keep the same locators as proposed by the colorbar function but change the ticklabels in order to print the formatted date as follows:
# change colobar ticks labels and locators
cbar.set_ticks([s.colorbar.vmin + t*(s.colorbar.vmax-s.colorbar.vmin) for t in cbar.ax.get_yticks()])
cbar.set_ticklabels([mdates.datetime.datetime.fromtimestamp((s.colorbar.vmin + t*(s.colorbar.vmax-s.colorbar.vmin))/1000000000).strftime('%c') for t in cbar.ax.get_yticks()])
plt.show()
If you really want to control tick locations, you can compute the desired values (here for approximately 3 months intervals ~91.25 days):
i,ticks = 0,[s.colorbar.vmin]
while ticks[-1] < s.colorbar.vmax:
ticks.append(s.colorbar.vmin+i*24*3600*91.25*1e9)
i = i+1
ticks[-1] = s.colorbar.vmax
cbar.set_ticks(ticks)
cbar.set_ticklabels([mdates.datetime.datetime.fromtimestamp(t/1e9).strftime('%c') for t in ticks])