[英]Matplotlib Plot time series with different periodicity
I have 2 dfs.我有 2 个 dfs。 One of them has data for a month.其中一个有一个月的数据。 Another one, averages for the past quarters.另一个,过去几个季度的平均值。 I wanna plot the averages in front of the monthly data.我想在每月数据前绘制平均值。 How can I do it?我该怎么做? Please note that I am trying to plot averages as dots and monthly as line chart.请注意,我试图将平均值绘制为点,将每月绘制为折线图。
So far my best result was achieved by ax1=ax.twiny()
, but still not ideal result as data point appear in throughout the chart, rather than just in front.到目前为止,我的最佳结果是通过ax1=ax.twiny()
,但仍然不是理想的结果,因为数据点出现在整个图表中,而不仅仅是在前面。
import pandas as pd
import numpy as np
import matplotlib.dates as mdates
from matplotlib.ticker import ScalarFormatter, FormatStrFormatter, FuncFormatter
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
date_base = pd.date_range(start='1/1/2018', end='1/30/2018')
df_base = pd.DataFrame(np.random.randn(30,4), columns=list("ABCD"), index=date_base)
date_ext = pd.date_range(start='1/1/2017', end='1/1/2018', freq="Q")
df_ext = pd.DataFrame(np.random.randn(4,4), columns=list("ABCD"), index=date_ext)
def drawChartsPlt(df_base, df_ext):
fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111)
number_of_plots = len(df_base.columns)
LINE_STYLES = ['-', '--', '-.', 'dotted']
colormap = plt.cm.nipy_spectral
ax.set_prop_cycle("color", [colormap(i) for i in np.linspace(0,1,number_of_plots)])
date_base = df_base.index
date_base = [i.strftime("%Y-%m-%d") for i in date_base]
q_ends = df_ext.index
q_ends = [i.strftime("%Y-%m-%d") for i in q_ends]
date_base.insert(0, "") #to shift xticks so they match chart
date_base += q_ends
for i in range(number_of_plots):
df_base.ix[:-3, df_base.columns[i]].plot(kind="line", linestyle=LINE_STYLES[i%2], subplots=False, ax=ax)
#ax.set_xticks(date_base)
#ax.set_xticklabels(date_base)
# ax.xaxis.set_major_locator(ticker.MultipleLocator(20))
ax.xaxis.set_major_locator(ticker.LinearLocator(len(date_base)))
ax.xaxis.set_major_formatter(plt.FixedFormatter(date_base))
fig.autofmt_xdate()
# ax1=ax.twinx()
ax1=ax.twiny()
ax1.set_prop_cycle("color", [colormap(i) for i in np.linspace(0,1,number_of_plots)])
for i in range(len(df_ext.columns)):
ax1.scatter(x=df_ext.index, y=df_ext[df_ext.columns[i]])
ax.set_title("Test")
#plt.minorticks_off())
ax.minorticks_off()
#ax1.minorticks_off()
#ax1.set_xticklabels(date_base)
#ax1.set_xticklabels(q_ends)
ax.legend(loc="center left", bbox_to_anchor=(1,0.5))
ax.xaxis.label.set_size(12)
plt.xlabel("TEST X Label")
plt.ylabel("TEST Y Label")
ax1.set_xlabel("Quarters")
plt.show()
drawChartsPlt(df_base, df_ext)
The way I ended up coding it is by saving quarterly index of df_ext to a temp variable, overwriting it with dates that are close to df_base.index
using pd.date_range(start=df_base.index[-1], periods=len(df_ext), freq='D')
, and the finally setting the dates that I need with ax.set_xticklabels(list(date_base)+list(date_ext))
.我最终编码的方式是将 df_ext 的季度索引保存到临时变量,使用pd.date_range(start=df_base.index[-1], periods=len(df_ext), freq='D')
用接近df_base.index
日期覆盖它pd.date_range(start=df_base.index[-1], periods=len(df_ext), freq='D')
,最后用ax.set_xticklabels(list(date_base)+list(date_ext))
设置我需要的日期。
It looks like it could be achieved using broken axes as indicated Break // in x axis of matplotlib and Python/Matplotlib - Is there a way to make a discontinuous axis?看起来可以使用断轴来实现,如所示Break // 在 matplotlib和Python/Matplotlib 的x 轴上- 有没有办法制作不连续的轴? , but I haven't tried that solution. ,但我还没有尝试过该解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.