在 matplotlib 上绘制两个数据系列时的随机直线

Random straight lines when plotting two data series on matplotlib

我是 python 的新手,我正在尝试使用 matplotlib 绘制两个数据系列的折线图。

示例代码和数据如下:

import matplotlib.pyplot as plt

# choronologically sorted dates with frequencies
date_1 = ['2018-06-06', '2018-06-07', '2018-06-08', '2018-06-09', '2018-06-10', '2018-06-11', '2018-06-12', '2018-06-13', '2018-06-14', '2018-06-15', '2018-06-16', '2018-06-17', '2018-06-18', '2018-06-19', '2018-06-20', '2018-06-21', '2018-06-22', '2018-06-23', '2018-06-25', '2018-06-27', '2018-06-28', '2018-06-30', '2018-07-01', '2018-07-02', '2018-07-03', '2018-07-04', '2018-07-05', '2018-07-06', '2018-07-07', '2018-07-08', '2018-07-09', '2018-07-10', '2018-07-11', '2018-07-12', '2018-07-13', '2018-07-14', '2018-07-15', '2018-07-16', '2018-07-17', '2018-07-18', '2018-07-19', '2018-07-20', '2018-07-21', '2018-07-22', '2018-07-23', '2018-07-24', '2018-07-25', '2018-07-26', '2018-07-27', '2018-07-28', '2018-07-29', '2018-07-30', '2018-07-31', '2018-08-01', '2018-08-02', '2018-08-03', '2018-08-04', '2018-08-05', '2018-08-06', '2018-08-07', '2018-08-08', '2018-08-09', '2018-08-10', '2018-08-11', '2018-08-12', '2018-08-13', '2018-08-14', '2018-08-15', '2018-08-16', '2018-08-17', '2018-08-18', '2018-08-19', '2018-08-20', '2018-08-21', '2018-08-22', '2018-08-23', '2018-08-24', '2018-08-25', '2018-08-26', '2018-08-27', '2018-08-28', '2018-08-29', '2018-08-30', '2018-08-31', '2018-09-01', '2018-09-02', '2018-09-03', '2018-09-04', '2018-09-05', '2018-09-06', '2018-09-07', '2018-09-08', '2018-09-09', '2018-09-10', '2018-09-11', '2018-09-12', '2018-09-13', '2018-09-14', '2018-09-15', '2018-09-16', '2018-09-17', '2018-09-18', '2018-09-19', '2018-09-20', '2018-09-21', '2018-09-22', '2018-09-23', '2018-09-24', '2018-09-25', '2018-09-26', '2018-09-27', '2018-09-28', '2018-09-29', '2018-09-30', '2018-10-01', '2018-10-02', '2018-10-03', '2018-10-04', '2018-10-05', '2018-10-06', '2018-10-07', '2018-10-08', '2018-10-09', '2018-10-10', '2018-10-11', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-10-16', '2018-10-17', '2018-10-18', '2018-10-19', '2018-10-20', '2018-10-21', '2018-10-22', '2018-10-23', '2018-10-24', '2018-10-25', '2018-10-26', '2018-10-27', '2018-10-28', '2018-10-29', '2018-10-30', '2018-10-31', '2018-11-01', '2018-11-02', '2018-11-03', '2018-11-04', '2018-11-05', '2018-11-06', '2018-11-07', '2018-11-08', '2018-11-09', '2018-11-10', '2018-11-11', '2018-11-12', '2018-11-13', '2018-11-14', '2018-11-15', '2018-11-16', '2018-11-17', '2018-11-18', '2018-11-19', '2018-11-20', '2018-11-21', '2018-11-22', '2018-11-23', '2018-11-24', '2018-11-25', '2018-11-26', '2018-11-27', '2018-11-28']
freq_1 = [87, 123, 147, 170, 118, 23, 9, 87, 118, 297, 139, 650, 95, 336, 338, 276, 56, 474, 2, 1, 1, 576, 292, 211, 25, 20, 11, 91, 84, 43, 280, 33, 198, 88, 25, 36, 46, 14, 44, 112, 76, 15, 42, 124, 54, 132, 271, 230, 728, 35, 251, 163, 41, 89, 50, 257, 48, 26, 73, 204, 19, 39, 58, 222, 48, 129, 286, 191, 121, 84, 20, 13, 157, 66, 180, 10, 36, 298, 162, 274, 21, 298, 243, 21, 79, 199, 12, 48, 212, 141, 62, 308, 339, 296, 479, 174, 90, 389, 120, 256, 327, 131, 451, 447, 279, 37, 51, 105, 578, 298, 184, 347, 59, 276, 379, 117, 158, 162, 64, 36, 102, 127, 131, 61, 46, 124, 202, 650, 183, 119, 44, 18, 28, 26, 126, 103, 34, 66, 109, 91, 155, 109, 172, 76, 91, 67, 840, 56, 126, 55, 144, 39, 63, 62, 242, 41, 64, 83, 78, 53, 35, 30, 38, 59, 238, 110, 116, 79, 91, 38, 633, 176, 56]

date_2 = ['2018-06-06', '2018-06-07', '2018-06-08', '2018-06-09', '2018-06-10', '2018-06-11', '2018-06-12', '2018-06-13', '2018-06-14', '2018-06-15', '2018-06-16', '2018-06-17', '2018-06-18', '2018-06-19', '2018-06-20', '2018-06-21', '2018-06-22', '2018-06-23', '2018-06-25', '2018-06-28', '2018-06-29', '2018-06-30', '2018-07-01', '2018-07-02', '2018-07-03', '2018-07-04', '2018-07-05', '2018-07-06', '2018-07-07', '2018-07-08', '2018-07-09', '2018-07-10', '2018-07-11', '2018-07-12', '2018-07-13', '2018-07-14', '2018-07-15', '2018-07-16', '2018-07-17', '2018-07-18', '2018-07-19', '2018-07-20', '2018-07-21', '2018-07-22', '2018-07-23', '2018-07-24', '2018-07-25', '2018-07-26', '2018-07-27', '2018-07-28', '2018-07-29', '2018-07-30', '2018-07-31', '2018-08-01', '2018-08-02', '2018-08-03', '2018-08-04', '2018-08-05', '2018-08-06', '2018-08-07', '2018-08-08', '2018-08-09', '2018-08-10', '2018-08-11', '2018-08-12', '2018-08-13', '2018-08-14', '2018-08-15', '2018-08-16', '2018-08-17', '2018-08-18', '2018-08-19', '2018-08-20', '2018-08-21', '2018-08-22', '2018-08-23', '2018-08-24', '2018-08-25', '2018-08-26', '2018-08-27', '2018-08-28', '2018-08-29', '2018-08-30', '2018-08-31', '2018-09-01', '2018-09-02', '2018-09-03', '2018-09-04', '2018-09-05', '2018-09-06', '2018-09-07', '2018-09-08', '2018-09-09', '2018-09-10', '2018-09-11', '2018-09-12', '2018-09-13', '2018-09-14', '2018-09-15', '2018-09-16', '2018-09-17', '2018-09-18', '2018-09-19', '2018-09-20', '2018-09-21', '2018-09-22', '2018-09-23', '2018-09-24', '2018-09-25', '2018-09-26', '2018-09-27', '2018-09-28', '2018-09-29', '2018-09-30', '2018-10-01', '2018-10-02', '2018-10-03', '2018-10-04', '2018-10-05', '2018-10-06', '2018-10-07', '2018-10-08', '2018-10-09', '2018-10-10', '2018-10-11', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-10-16', '2018-10-17', '2018-10-18', '2018-10-19', '2018-10-20', '2018-10-21', '2018-10-22', '2018-10-23', '2018-10-24', '2018-10-25', '2018-10-26', '2018-10-27', '2018-10-28', '2018-10-29', '2018-10-30', '2018-10-31', '2018-11-01', '2018-11-02', '2018-11-03', '2018-11-04', '2018-11-05', '2018-11-06', '2018-11-07', '2018-11-08', '2018-11-09', '2018-11-10', '2018-11-11', '2018-11-12', '2018-11-13', '2018-11-14', '2018-11-15', '2018-11-16', '2018-11-17', '2018-11-18', '2018-11-19', '2018-11-20', '2018-11-21', '2018-11-22', '2018-11-23', '2018-11-24', '2018-11-25', '2018-11-26', '2018-11-27', '2018-11-28']
freq_2 = [85, 109, 134, 165, 125, 20, 10, 78, 107, 198, 105, 347, 66, 268, 357, 252, 45, 427, 13, 10, 1, 545, 286, 223, 18, 22, 15, 109, 83, 55, 276, 29, 189, 93, 25, 30, 50, 14, 36, 91, 77, 18, 40, 84, 50, 104, 274, 269, 657, 31, 200, 145, 34, 90, 50, 259, 42, 25, 67, 177, 25, 40, 82, 238, 53, 108, 276, 160, 126, 71, 13, 13, 152, 57, 161, 13, 51, 232, 154, 205, 22, 248, 202, 16, 78, 179, 12, 44, 205, 126, 56, 323, 270, 260, 426, 134, 87, 391, 133, 241, 399, 112, 368, 361, 199, 51, 55, 115, 624, 301, 194, 370, 63, 243, 302, 96, 134, 154, 74, 42, 97, 111, 132, 69, 54, 127, 236, 563, 181, 139, 49, 20, 45, 33, 130, 99, 34, 62, 96, 85, 122, 113, 165, 69, 87, 66, 826, 53, 102, 52, 105, 31, 47, 58, 218, 49, 70, 70, 74, 51, 33, 43, 40, 66, 184, 75, 118, 72, 78, 40, 455, 153, 33]

fig, ax = plt.subplots()
ax.plot(date_1, freq_1, c='blue')
ax.plot(date_2, freq_2, c='red')

plt.title("Date and frequency")
plt.ylabel("Frequency")
ax.get_xaxis().set_visible(False)
fig.autofmt_xdate()

plt.show()

注释掉第一个或第二个图不会产生任何问题,如图所示:

但是当尝试同时绘制两者时,我的绘图中出现了奇怪的直线:

首先绘制第二个图给出

这里发生了什么,最适合初学者的更正方法是什么?

您很可能有异常值或不合适的数据点。 我建议首先像这样绘制数据,以确保它确实是一个 out-of-context 点:

ax.plot(date_1, freq_1, '.', c='blue')
ax.plot(date_2, freq_2, '.',c='red')

然后也许尝试根据 x 对点进行排序。或者删除异常值。

从图中可以看出,很可能您的数据中间某处有一个点 (0, 0)。

没有数据,这很难回答 - 看起来你的 non-sorted 数据在你的地块数据中回归到 0:

import matplotlib.pyplot as plt

# choronologically sorted dates with frequencies
date_1, freq_1 = [1,2,3], [4,5,6]
date_2, freq_2 = [1,2,1,3,], [6,7,0,8]

fig, ax = plt.subplots()
ax.plot(date_1, freq_1, c='blue')
ax.plot(date_2, freq_2, c='red')
plt.legend()

plt.title("Date and frequency")
plt.ylabel("Frequency")
ax.get_xaxis().set_visible(False)
fig.autofmt_xdate()

plt.show()

在你的数据中有一个 NaN 会让你的图表中断:

提供数据和见解后:您有 2 个日期仅在您的一个列表中:

date_1 = ['2018-06-06', '2018-06-07', '2018-06-08', '2018-06-09', '2018-06-10', '2018-06-11', '2018-06-12', '2018-06-13', '2018-06-14', '2018-06-15', '2018-06-16', '2018-06-17', '2018-06-18', '2018-06-19', '2018-06-20', '2018-06-21', '2018-06-22', '2018-06-23', '2018-06-25', '2018-06-27', '2018-06-28', '2018-06-30', '2018-07-01', '2018-07-02', '2018-07-03', '2018-07-04', '2018-07-05', '2018-07-06', '2018-07-07', '2018-07-08', '2018-07-09', '2018-07-10', '2018-07-11', '2018-07-12', '2018-07-13', '2018-07-14', '2018-07-15', '2018-07-16', '2018-07-17', '2018-07-18', '2018-07-19', '2018-07-20', '2018-07-21', '2018-07-22', '2018-07-23', '2018-07-24', '2018-07-25', '2018-07-26', '2018-07-27', '2018-07-28', '2018-07-29', '2018-07-30', '2018-07-31', '2018-08-01', '2018-08-02', '2018-08-03', '2018-08-04', '2018-08-05', '2018-08-06', '2018-08-07', '2018-08-08', '2018-08-09', '2018-08-10', '2018-08-11', '2018-08-12', '2018-08-13', '2018-08-14', '2018-08-15', '2018-08-16', '2018-08-17', '2018-08-18', '2018-08-19', '2018-08-20', '2018-08-21', '2018-08-22', '2018-08-23', '2018-08-24', '2018-08-25', '2018-08-26', '2018-08-27', '2018-08-28', '2018-08-29', '2018-08-30', '2018-08-31', '2018-09-01', '2018-09-02', '2018-09-03', '2018-09-04', '2018-09-05', '2018-09-06', '2018-09-07', '2018-09-08', '2018-09-09', '2018-09-10', '2018-09-11', '2018-09-12', '2018-09-13', '2018-09-14', '2018-09-15', '2018-09-16', '2018-09-17', '2018-09-18', '2018-09-19', '2018-09-20', '2018-09-21', '2018-09-22', '2018-09-23', '2018-09-24', '2018-09-25', '2018-09-26', '2018-09-27', '2018-09-28', '2018-09-29', '2018-09-30', '2018-10-01', '2018-10-02', '2018-10-03', '2018-10-04', '2018-10-05', '2018-10-06', '2018-10-07', '2018-10-08', '2018-10-09', '2018-10-10', '2018-10-11', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-10-16', '2018-10-17', '2018-10-18', '2018-10-19', '2018-10-20', '2018-10-21', '2018-10-22', '2018-10-23', '2018-10-24', '2018-10-25', '2018-10-26', '2018-10-27', '2018-10-28', '2018-10-29', '2018-10-30', '2018-10-31', '2018-11-01', '2018-11-02', '2018-11-03', '2018-11-04', '2018-11-05', '2018-11-06', '2018-11-07', '2018-11-08', '2018-11-09', '2018-11-10', '2018-11-11', '2018-11-12', '2018-11-13', '2018-11-14', '2018-11-15', '2018-11-16', '2018-11-17', '2018-11-18', '2018-11-19', '2018-11-20', '2018-11-21', '2018-11-22', '2018-11-23', '2018-11-24', '2018-11-25', '2018-11-26', '2018-11-27', '2018-11-28']
freq_1 = [87, 123, 147, 170, 118, 23, 9, 87, 118, 297, 139, 650, 95, 336, 338, 276, 56, 474, 2, 1, 1, 576, 292, 211, 25, 20, 11, 91, 84, 43, 280, 33, 198, 88, 25, 36, 46, 14, 44, 112, 76, 15, 42, 124, 54, 132, 271, 230, 728, 35, 251, 163, 41, 89, 50, 257, 48, 26, 73, 204, 19, 39, 58, 222, 48, 129, 286, 191, 121, 84, 20, 13, 157, 66, 180, 10, 36, 298, 162, 274, 21, 298, 243, 21, 79, 199, 12, 48, 212, 141, 62, 308, 339, 296, 479, 174, 90, 389, 120, 256, 327, 131, 451, 447, 279, 37, 51, 105, 578, 298, 184, 347, 59, 276, 379, 117, 158, 162, 64, 36, 102, 127, 131, 61, 46, 124, 202, 650, 183, 119, 44, 18, 28, 26, 126, 103, 34, 66, 109, 91, 155, 109, 172, 76, 91, 67, 840, 56, 126, 55, 144, 39, 63, 62, 242, 41, 64, 83, 78, 53, 35, 30, 38, 59, 238, 110, 116, 79, 91, 38, 633, 176, 56]

date_2 = ['2018-06-06', '2018-06-07', '2018-06-08', '2018-06-09', '2018-06-10', '2018-06-11', '2018-06-12', '2018-06-13', '2018-06-14', '2018-06-15', '2018-06-16', '2018-06-17', '2018-06-18', '2018-06-19', '2018-06-20', '2018-06-21', '2018-06-22', '2018-06-23', '2018-06-25', '2018-06-28', '2018-06-29', '2018-06-30', '2018-07-01', '2018-07-02', '2018-07-03', '2018-07-04', '2018-07-05', '2018-07-06', '2018-07-07', '2018-07-08', '2018-07-09', '2018-07-10', '2018-07-11', '2018-07-12', '2018-07-13', '2018-07-14', '2018-07-15', '2018-07-16', '2018-07-17', '2018-07-18', '2018-07-19', '2018-07-20', '2018-07-21', '2018-07-22', '2018-07-23', '2018-07-24', '2018-07-25', '2018-07-26', '2018-07-27', '2018-07-28', '2018-07-29', '2018-07-30', '2018-07-31', '2018-08-01', '2018-08-02', '2018-08-03', '2018-08-04', '2018-08-05', '2018-08-06', '2018-08-07', '2018-08-08', '2018-08-09', '2018-08-10', '2018-08-11', '2018-08-12', '2018-08-13', '2018-08-14', '2018-08-15', '2018-08-16', '2018-08-17', '2018-08-18', '2018-08-19', '2018-08-20', '2018-08-21', '2018-08-22', '2018-08-23', '2018-08-24', '2018-08-25', '2018-08-26', '2018-08-27', '2018-08-28', '2018-08-29', '2018-08-30', '2018-08-31', '2018-09-01', '2018-09-02', '2018-09-03', '2018-09-04', '2018-09-05', '2018-09-06', '2018-09-07', '2018-09-08', '2018-09-09', '2018-09-10', '2018-09-11', '2018-09-12', '2018-09-13', '2018-09-14', '2018-09-15', '2018-09-16', '2018-09-17', '2018-09-18', '2018-09-19', '2018-09-20', '2018-09-21', '2018-09-22', '2018-09-23', '2018-09-24', '2018-09-25', '2018-09-26', '2018-09-27', '2018-09-28', '2018-09-29', '2018-09-30', '2018-10-01', '2018-10-02', '2018-10-03', '2018-10-04', '2018-10-05', '2018-10-06', '2018-10-07', '2018-10-08', '2018-10-09', '2018-10-10', '2018-10-11', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-10-16', '2018-10-17', '2018-10-18', '2018-10-19', '2018-10-20', '2018-10-21', '2018-10-22', '2018-10-23', '2018-10-24', '2018-10-25', '2018-10-26', '2018-10-27', '2018-10-28', '2018-10-29', '2018-10-30', '2018-10-31', '2018-11-01', '2018-11-02', '2018-11-03', '2018-11-04', '2018-11-05', '2018-11-06', '2018-11-07', '2018-11-08', '2018-11-09', '2018-11-10', '2018-11-11', '2018-11-12', '2018-11-13', '2018-11-14', '2018-11-15', '2018-11-16', '2018-11-17', '2018-11-18', '2018-11-19', '2018-11-20', '2018-11-21', '2018-11-22', '2018-11-23', '2018-11-24', '2018-11-25', '2018-11-26', '2018-11-27', '2018-11-28']
freq_2 = [85, 109, 134, 165, 125, 20, 10, 78, 107, 198, 105, 347, 66, 268, 357, 252, 45, 427, 13, 10, 1, 545, 286, 223, 18, 22, 15, 109, 83, 55, 276, 29, 189, 93, 25, 30, 50, 14, 36, 91, 77, 18, 40, 84, 50, 104, 274, 269, 657, 31, 200, 145, 34, 90, 50, 259, 42, 25, 67, 177, 25, 40, 82, 238, 53, 108, 276, 160, 126, 71, 13, 13, 152, 57, 161, 13, 51, 232, 154, 205, 22, 248, 202, 16, 78, 179, 12, 44, 205, 126, 56, 323, 270, 260, 426, 134, 87, 391, 133, 241, 399, 112, 368, 361, 199, 51, 55, 115, 624, 301, 194, 370, 63, 243, 302, 96, 134, 154, 74, 42, 97, 111, 132, 69, 54, 127, 236, 563, 181, 139, 49, 20, 45, 33, 130, 99, 34, 62, 96, 85, 122, 113, 165, 69, 87, 66, 826, 53, 102, 52, 105, 31, 47, 58, 218, 49, 70, 70, 74, 51, 33, 43, 40, 66, 184, 75, 118, 72, 78, 40, 455, 153, 33]

only_in_1 = set(date_1).difference(date_2) # {'2018-06-27'} not in date_2
only_in_2 = set(date_2).difference(date_1) # {'2018-06-29'} not in date_1

您可以通过从源数据中删除单一日期及其各自的数据点来解决此问题:

date_1, freq_1 = map(list, zip(*(tups for tups in zip(date_1, freq_1) 
                                 if tups[0] not in only_in_1)))

date_2, freq_2 = map(list, zip(*(tups for tups in zip(date_2, freq_2) 
                                 if tups[0] not in only_in_2)))

fig, ax = plt.subplots()
ax.plot(date_1, freq_1, c='blue')
ax.plot(date_2, freq_2, c='red', )

plt.title("Date and frequency")
plt.ylabel("Frequency")
ax.get_xaxis().set_visible(False)
fig.autofmt_xdate()

plt.show()

然后给你:

而不是


或者,您可以解耦 x-axes 并保持数据原样:

import matplotlib.pyplot as plt

# choronologically sorted dates with frequencies
date_1 = ['2018-06-06', '2018-06-07', '2018-06-08', '2018-06-09', '2018-06-10', '2018-06-11', '2018-06-12', '2018-06-13', '2018-06-14', '2018-06-15', '2018-06-16', '2018-06-17', '2018-06-18', '2018-06-19', '2018-06-20', '2018-06-21', '2018-06-22', '2018-06-23', '2018-06-25', '2018-06-27', '2018-06-28', '2018-06-30', '2018-07-01', '2018-07-02', '2018-07-03', '2018-07-04', '2018-07-05', '2018-07-06', '2018-07-07', '2018-07-08', '2018-07-09', '2018-07-10', '2018-07-11', '2018-07-12', '2018-07-13', '2018-07-14', '2018-07-15', '2018-07-16', '2018-07-17', '2018-07-18', '2018-07-19', '2018-07-20', '2018-07-21', '2018-07-22', '2018-07-23', '2018-07-24', '2018-07-25', '2018-07-26', '2018-07-27', '2018-07-28', '2018-07-29', '2018-07-30', '2018-07-31', '2018-08-01', '2018-08-02', '2018-08-03', '2018-08-04', '2018-08-05', '2018-08-06', '2018-08-07', '2018-08-08', '2018-08-09', '2018-08-10', '2018-08-11', '2018-08-12', '2018-08-13', '2018-08-14', '2018-08-15', '2018-08-16', '2018-08-17', '2018-08-18', '2018-08-19', '2018-08-20', '2018-08-21', '2018-08-22', '2018-08-23', '2018-08-24', '2018-08-25', '2018-08-26', '2018-08-27', '2018-08-28', '2018-08-29', '2018-08-30', '2018-08-31', '2018-09-01', '2018-09-02', '2018-09-03', '2018-09-04', '2018-09-05', '2018-09-06', '2018-09-07', '2018-09-08', '2018-09-09', '2018-09-10', '2018-09-11', '2018-09-12', '2018-09-13', '2018-09-14', '2018-09-15', '2018-09-16', '2018-09-17', '2018-09-18', '2018-09-19', '2018-09-20', '2018-09-21', '2018-09-22', '2018-09-23', '2018-09-24', '2018-09-25', '2018-09-26', '2018-09-27', '2018-09-28', '2018-09-29', '2018-09-30', '2018-10-01', '2018-10-02', '2018-10-03', '2018-10-04', '2018-10-05', '2018-10-06', '2018-10-07', '2018-10-08', '2018-10-09', '2018-10-10', '2018-10-11', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-10-16', '2018-10-17', '2018-10-18', '2018-10-19', '2018-10-20', '2018-10-21', '2018-10-22', '2018-10-23', '2018-10-24', '2018-10-25', '2018-10-26', '2018-10-27', '2018-10-28', '2018-10-29', '2018-10-30', '2018-10-31', '2018-11-01', '2018-11-02', '2018-11-03', '2018-11-04', '2018-11-05', '2018-11-06', '2018-11-07', '2018-11-08', '2018-11-09', '2018-11-10', '2018-11-11', '2018-11-12', '2018-11-13', '2018-11-14', '2018-11-15', '2018-11-16', '2018-11-17', '2018-11-18', '2018-11-19', '2018-11-20', '2018-11-21', '2018-11-22', '2018-11-23', '2018-11-24', '2018-11-25', '2018-11-26', '2018-11-27', '2018-11-28']
freq_1 = [87, 123, 147, 170, 118, 23, 9, 87, 118, 297, 139, 650, 95, 336, 338, 276, 56, 474, 2, 1, 1, 576, 292, 211, 25, 20, 11, 91, 84, 43, 280, 33, 198, 88, 25, 36, 46, 14, 44, 112, 76, 15, 42, 124, 54, 132, 271, 230, 728, 35, 251, 163, 41, 89, 50, 257, 48, 26, 73, 204, 19, 39, 58, 222, 48, 129, 286, 191, 121, 84, 20, 13, 157, 66, 180, 10, 36, 298, 162, 274, 21, 298, 243, 21, 79, 199, 12, 48, 212, 141, 62, 308, 339, 296, 479, 174, 90, 389, 120, 256, 327, 131, 451, 447, 279, 37, 51, 105, 578, 298, 184, 347, 59, 276, 379, 117, 158, 162, 64, 36, 102, 127, 131, 61, 46, 124, 202, 650, 183, 119, 44, 18, 28, 26, 126, 103, 34, 66, 109, 91, 155, 109, 172, 76, 91, 67, 840, 56, 126, 55, 144, 39, 63, 62, 242, 41, 64, 83, 78, 53, 35, 30, 38, 59, 238, 110, 116, 79, 91, 38, 633, 176, 56]

date_2 = ['2018-06-06', '2018-06-07', '2018-06-08', '2018-06-09', '2018-06-10', '2018-06-11', '2018-06-12', '2018-06-13', '2018-06-14', '2018-06-15', '2018-06-16', '2018-06-17', '2018-06-18', '2018-06-19', '2018-06-20', '2018-06-21', '2018-06-22', '2018-06-23', '2018-06-25', '2018-06-28', '2018-06-29', '2018-06-30', '2018-07-01', '2018-07-02', '2018-07-03', '2018-07-04', '2018-07-05', '2018-07-06', '2018-07-07', '2018-07-08', '2018-07-09', '2018-07-10', '2018-07-11', '2018-07-12', '2018-07-13', '2018-07-14', '2018-07-15', '2018-07-16', '2018-07-17', '2018-07-18', '2018-07-19', '2018-07-20', '2018-07-21', '2018-07-22', '2018-07-23', '2018-07-24', '2018-07-25', '2018-07-26', '2018-07-27', '2018-07-28', '2018-07-29', '2018-07-30', '2018-07-31', '2018-08-01', '2018-08-02', '2018-08-03', '2018-08-04', '2018-08-05', '2018-08-06', '2018-08-07', '2018-08-08', '2018-08-09', '2018-08-10', '2018-08-11', '2018-08-12', '2018-08-13', '2018-08-14', '2018-08-15', '2018-08-16', '2018-08-17', '2018-08-18', '2018-08-19', '2018-08-20', '2018-08-21', '2018-08-22', '2018-08-23', '2018-08-24', '2018-08-25', '2018-08-26', '2018-08-27', '2018-08-28', '2018-08-29', '2018-08-30', '2018-08-31', '2018-09-01', '2018-09-02', '2018-09-03', '2018-09-04', '2018-09-05', '2018-09-06', '2018-09-07', '2018-09-08', '2018-09-09', '2018-09-10', '2018-09-11', '2018-09-12', '2018-09-13', '2018-09-14', '2018-09-15', '2018-09-16', '2018-09-17', '2018-09-18', '2018-09-19', '2018-09-20', '2018-09-21', '2018-09-22', '2018-09-23', '2018-09-24', '2018-09-25', '2018-09-26', '2018-09-27', '2018-09-28', '2018-09-29', '2018-09-30', '2018-10-01', '2018-10-02', '2018-10-03', '2018-10-04', '2018-10-05', '2018-10-06', '2018-10-07', '2018-10-08', '2018-10-09', '2018-10-10', '2018-10-11', '2018-10-12', '2018-10-13', '2018-10-14', '2018-10-15', '2018-10-16', '2018-10-17', '2018-10-18', '2018-10-19', '2018-10-20', '2018-10-21', '2018-10-22', '2018-10-23', '2018-10-24', '2018-10-25', '2018-10-26', '2018-10-27', '2018-10-28', '2018-10-29', '2018-10-30', '2018-10-31', '2018-11-01', '2018-11-02', '2018-11-03', '2018-11-04', '2018-11-05', '2018-11-06', '2018-11-07', '2018-11-08', '2018-11-09', '2018-11-10', '2018-11-11', '2018-11-12', '2018-11-13', '2018-11-14', '2018-11-15', '2018-11-16', '2018-11-17', '2018-11-18', '2018-11-19', '2018-11-20', '2018-11-21', '2018-11-22', '2018-11-23', '2018-11-24', '2018-11-25', '2018-11-26', '2018-11-27', '2018-11-28']
freq_2 = [85, 109, 134, 165, 125, 20, 10, 78, 107, 198, 105, 347, 66, 268, 357, 252, 45, 427, 13, 10, 1, 545, 286, 223, 18, 22, 15, 109, 83, 55, 276, 29, 189, 93, 25, 30, 50, 14, 36, 91, 77, 18, 40, 84, 50, 104, 274, 269, 657, 31, 200, 145, 34, 90, 50, 259, 42, 25, 67, 177, 25, 40, 82, 238, 53, 108, 276, 160, 126, 71, 13, 13, 152, 57, 161, 13, 51, 232, 154, 205, 22, 248, 202, 16, 78, 179, 12, 44, 205, 126, 56, 323, 270, 260, 426, 134, 87, 391, 133, 241, 399, 112, 368, 361, 199, 51, 55, 115, 624, 301, 194, 370, 63, 243, 302, 96, 134, 154, 74, 42, 97, 111, 132, 69, 54, 127, 236, 563, 181, 139, 49, 20, 45, 33, 130, 99, 34, 62, 96, 85, 122, 113, 165, 69, 87, 66, 826, 53, 102, 52, 105, 31, 47, 58, 218, 49, 70, 70, 74, 51, 33, 43, 40, 66, 184, 75, 118, 72, 78, 40, 455, 153, 33]


fig, ax = plt.subplots()

axes = [ax, ax.twiny()]

axes[0].plot(date_1, freq_1, c='blue')  # this decouples them - not sure
axes[1].plot(date_2, freq_2, c='red',)  # it makes sense in your case

plt.title("Date and frequency")
plt.ylabel("Frequency")
for a in axes:
    a.get_xaxis().set_visible(False)
    
fig.autofmt_xdate()

plt.show()
  • 参见 f.e。 matplotlib: overlay plots with different scales? 获取更多信息。

无论什么对您来说更有意义 - 您都必须做出决定。使用不同的 x-axis 可能会略微偏离图纸。