1
我想更改以下圖形的色條中的刻度定位器和標籤。Matplotlib色條更改刻度標籤和定位器
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
????
顏色條顯示時間依賴性。因此,我希望將tick的值從數值(納秒)更改爲更合理的日期格式,例如月份和年份(例如,%b%Y或%Y-%m),其間隔可以是例如3或6個月。那可能嗎?
我試圖用cbar.formatter,cbar.locator和mdates玩不成功。