我有一个包含许多具有相同结构的聚合数据帧的列表。
我想在同一个图表上绘制每个数据帧的两列。
我使用了这个代码片段,但它为每个数据帧提供了一个单独的图:
# iterate through a list
for df in frames:
df.plot(x='Time', y='G1', figsize=(16, 10))
plt.hold(True)
plt.show()
最佳答案
如果您对每个集合都建立了索引,则可以将所有集合连接起来并立即绘制它们,而无需迭代。
# If not indexed:
# frames = [df.assign(sample=i) for i, df in enumerate(frames)]
df = pd.concat(frames).pivot(index='Time', columns='sample', values='G1')
df.plot(figsize=(16, 10));
这有助于确保您的数据对齐,并且 plt.hold
在 matplotlib 2.0 中已弃用。
关于python - 在同一图 pandas 上绘制多个数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45168069/