0
我試圖繪製出現在不同時間的兩組數據。我的目標是在當時有兩組xaxis標籤對應該數據點,一個在另一個之上。到目前爲止,我有這樣的:添加一行到xtick標籤並更改顏色
import numpy as np
import matplotlib.pyplot as plt
data1 = [4, 5, 3, 6]
data2 = [1, 6, 7, 2]
xlabel = ['2120', '2125', '2129', '2133']
xlabel2 = ['\n 2115', '\n 2118', '\n 2121', '\n 2124']
xticks = np.arange(0, len(data1) ,1)
fig = plt.figure(figsize=[8.0,5.0])
ax = fig.add_subplot(111)
ax.plot(xticks, data1, color='b', label='Data 1')
ax.plot(xticks, data2, color='r', label='Data 2')
xmajor_ticks=np.arange(0,4,1)
ax.set_xticks(xmajor_ticks)
ax.set_xticklabels(xlabel, color='b')
ax.set_xticklabels(xlabel2, color='r')
ax.set_xlim([-0.5,3.5])
ax.set_xlabel('Time')
ax.legend(loc='upper left')
ax.grid()
這是覆蓋第一個ax.set_xticklabels,只繪製data2紅色時間。我怎樣才能將data1次數據保持爲藍色,並在xaxis下的data2次數爲紅色?