我想在一条线上绘制 Pandas 时间序列数据,并将其与点数据组合在一个图中。 (我有全年的每日值和某些特定日期的值,这些值应绘制为点。)
是否可以只将点数据绘制为没有线条的标记?我用 style = 'x' 试过了,但是图例在样式应该出现的地方是空的。
当我使用 marker = 'o' 时,点之间总是有一条线。
df_points
point_values
2015-10-31 34.000000
2015-11-30 86.914286
2015-12-14 109.777778
2015-12-29 113.340206
2016-01-13 239.503817
2016-01-19 296.768657
2016-01-27 296.849558
2016-02-02 377.489726
2016-02-10 436.665698
2016-02-24 506.214689
2016-03-02 523.585106
2016-03-15 574.376344
2016-04-01 602.537143
2016-04-15 662.771812
2016-04-29 738.079096
2016-05-15 784.083333
2016-05-31 698.914286
2016-06-14 539.915254
2016-06-29 318.356164
df_lines
line_values
2015-10-31 34.010000
2015-11-30 97.800000
2015-12-14 127.700000
2015-12-29 147.000000
2016-01-13 252.150000
2016-01-19 329.900000
2016-01-27 341.700000
2016-02-02 389.700000
2016-02-10 478.550000
2016-02-24 543.300000
2016-03-02 562.700000
2016-03-15 620.100000
2016-04-01 636.500000
2016-04-15 628.482486
2016-04-29 708.000000
2016-05-15 770.000000
2016-05-31 808.000000
2016-06-14 632.900000
2016-06-29 415.528680
fig, (ax1) = plt.subplots(nrows=1, ncols=1, figsize=(12, 10))
df_points['point_values'].plot(ax=ax1, label='point values', legend=True, grid=True, sharex=True, style = 'o')
df_lines['line_values'].plot(ax=ax1, label='daily values', legend=True, grid=True, sharex=True)
请您参考如下方法:
您可以在线图上绘制散点图来实现这一点。引用下面的代码。
如果 dates 是两个数据帧中的 index,
df_lines.index = [pd.to_datetime(d) for d in df_lines.index]
df_points.index = [pd.to_datetime(d) for d in df_points.index]
fig=plt.figure(figsize=(12, 10))
ax1 = fig.add_subplot(111)
ax1.scatter(x=df_points.index, y=df_points['point_values'] ,label='point values')
df_lines.plot(ax=ax1, label='line values', color='darkorange')
ax1.legend()
plt.grid()
plt.show()
输出:
