我有一个代码,用于为数据列表的每个元素分配图例(17种不同的颜色),包括错误栏的相同颜色。
在此过程中,我发现了一个非常有用的答案,可以为循环中的绘图着色(例如,在帖子中:Setting different color for each series in scatter plot on matplotlib),为此,我得到如下图像:
问题是:在将时间序列作为x轴时,如何为每个iSub的dx偏移每个序列,以使每个iSub的散布图不重叠? strong>
from datetime import datetime, timedelta
import matplotlib.cm as cm
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np
def datetime_range(start, end, delta):
current = start
while current < end:
yield current
current += delta
nSub = 17
nTimes = 34
datalist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q']
DATA = np.random.uniform(low=0.5, high=50.0, size=(nSub,nTimes))
DZ = np.random.rand(nSub,nTimes)
timelist = [dt.strftime('%Y-%m-%d') for dt in
datetime_range(datetime(2012, 1, 1), datetime(2014, 8, 1),
timedelta(weeks=4))]
datelist = [datetime.strptime(timelist[i], '%Y-%m-%d') for i in range(len(timelist))]
fig, ax = plt.subplots (nrows=1, ncols=1)
colors = cm.rainbow(np.linspace(0, 1, len(datalist)))
for isub in range(len(datalist)):
ax.scatter(datelist, DATA[isub], label=datalist[isub], color = colors[isub])
ax.errorbar(datelist, DATA[isub], yerr = DZ[isub], color = colors[isub], fmt='o', markersize=8, capsize=20)
ax.set_xlim([datetime(2011,12,1).toordinal(), datetime(2015,1,1).toordinal()] )
ax.xaxis.set_tick_params(labelsize=20)
ax.yaxis.set_tick_params(labelsize=20)
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
fig.set_size_inches(40,20)