在具有多个图形的同一图形上绘制多组数据框 [英] Plotting multiple sets of dataframes on the same graph with multiple figures

查看:101
本文介绍了在具有多个图形的同一图形上绘制多组数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在同一张图上绘制两个数据框.我想这样做五次,以便产生五个不同的图形,以便可以将它们另存为单独的文件,以便以后使用.但是,我无法在同一张图上获得这两套数据.我知道当网格中有多个子图时如何在同一图形上绘制两组数据,以及当不在同一图形上使用数据框时如何绘制两组数据.但是,这似乎使我感到困惑.

I am attempting to plot two dataframes on the same graph. I want to do this five times in order to produce five different figures so that they can be saved as separate files in order to be used later. However, I have not been able to get these two sets of data on the same graph. I know how to plot two sets of data on the same graph when I have multiple subplots in grid as well as how to plot two sets of data when not using dataframes on the same graph. However, this seeems to perplex me.

tableau20=xcolorpallet()
f0,axes00=plt.subplots()
axes01=axes00.twinx()
df00=df00.drop('elev', 1)
ax00min=float(df00.min())
ax00max=1.05*float(df00.max())
ax00step=int((ax00max-ax00min)/10)
ax10min=float(df10.min())
ax10max=1.05*float(df10.max())
ax10step=int((ax10max-ax10min)/10)
df00.plot(linewidth=2, figsize=(9,6),color=tableau20[6])
axes00.set_ylabel(v00,fontsize=14, rotation=90)
axes00.set_ylim(ax00min,ax00max)
axes00.set_yticks(np.arange(ax00min,ax00max,ax00step))
df10.plot(fig=f0,ax=axes01,secondary_y=v10,linewidth=2,color=tableau20[14])
f0.set_title(v10,fontsize=18)
axes01.set_ylabel(v00,fontsize=14, rotation=90)
axes01.set_ylim(ax00min,ax00max)
axes01.set_yticks(np.arange(ax00min,ax00max,ax00step))
plt.show()

当前,它正在生成两个带有我的数据的独立图形,这很好,但不是我想要的.有什么建议吗?

Currently, it is producing two separate graphs with my data on it, which is nice, but not what I am looking for. Any suggestions?

以下是一些示例数据,可让我大致了解一下我要绘制的图形:

Here is some sample data to get an idea of what I am trying to graph:

df00:

time                Temp
2014-08-16 12:02:40 68.0
2014-08-16 12:17:40 69.0
2014-08-16 12:32:40 68.0
2014-08-16 12:47:40 68.0
2014-08-16 13:02:40 68.0
2014-08-16 13:17:40 68.0
2014-08-16 13:32:40 68.0
2014-08-16 13:47:40 68.0
2014-08-16 14:02:40 68.0
2014-08-16 14:17:40 68.0
2014-08-16 14:32:39 66.0
2014-08-16 14:32:40 67.0
2014-08-16 14:47:39 66.0
2014-08-16 14:47:40 66.0
2014-08-16 15:02:40 66.0
2014-08-16 15:17:39 64.0
2014-08-16 15:17:40 65.0
...

df10:

date_time       Temperature
2014-08-16 12:00:00 17.3997
2014-08-16 13:00:00 16.9094
2014-08-16 14:00:00 16.4693
2014-08-16 15:00:00 15.9627
2014-08-16 16:00:00 15.5795
2014-08-16 17:00:00 15.5492
2014-08-16 18:00:00 15.2729
2014-08-16 19:00:00 15.2119
2014-08-16 20:00:00 15.3572
2014-08-16 21:00:00 15.497
2014-08-16 22:00:00 15.349
2014-08-16 23:00:00 15.3398
2014-08-17 00:00:00 15.5546
2014-08-17 01:00:00 14.9101
2014-08-17 02:00:00 15.279
2014-08-17 03:00:00 15.2961
2014-08-17 04:00:00 15.003
2014-08-17 05:00:00 15.4753
...

我很抱歉没有最初包含它.我以为这是一个图形问题,但似乎很大程度上已经变成了数据框之间的兼容性问题.

My apologies for not originally including it. I thought this was a graphing issue, but it seems has largely turned into a compatibility issue between dataframes.

推荐答案

要在同一张图表上进行绘图,您将使用与.plot()相同的ax对象:

To plot on the same chart, you would be using the same ax object with .plot():

使用示例数据-第一个df1:

Using the sample data - first df1:

DatetimeIndex: 17 entries, 2014-08-16 12:02:40 to 2014-08-16 15:17:40
Data columns (total 1 columns):
Temp    17 non-null int64
dtypes: int64(1)
memory usage: 272.0 bytes
None
                     Temp
time                     
2014-08-16 12:02:40    68
2014-08-16 12:17:40    69
2014-08-16 12:32:40    68
2014-08-16 12:47:40    68
2014-08-16 13:02:40    68
2014-08-16 13:17:40    68
2014-08-16 13:32:40    68
2014-08-16 13:47:40    68
2014-08-16 14:02:40    68
2014-08-16 14:17:40    68
2014-08-16 14:32:39    66
2014-08-16 14:32:40    67
2014-08-16 14:47:39    66
2014-08-16 14:47:40    66
2014-08-16 15:02:40    66
2014-08-16 15:17:39    64
2014-08-16 15:17:40    65

df2:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 18 entries, 2014-08-16 12:00:00 to 2014-08-17 05:00:00
Data columns (total 1 columns):
Temperature    18 non-null float64
dtypes: float64(1)
memory usage: 288.0 bytes
None
                     Temperature
date_time                       
2014-08-16 12:00:00      17.3997
2014-08-16 13:00:00      16.9094
2014-08-16 14:00:00      16.4693
2014-08-16 15:00:00      15.9627
2014-08-16 16:00:00      15.5795
2014-08-16 17:00:00      15.5492
2014-08-16 18:00:00      15.2729
2014-08-16 19:00:00      15.2119
2014-08-16 20:00:00      15.3572
2014-08-16 21:00:00      15.4970
2014-08-16 22:00:00      15.3490
2014-08-16 23:00:00      15.3398
2014-08-17 00:00:00      15.5546
2014-08-17 01:00:00      14.9101
2014-08-17 02:00:00      15.2790
2014-08-17 03:00:00      15.2961
2014-08-17 04:00:00      15.0030
2014-08-17 05:00:00      15.4753

此:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')

ax = df1.plot()
df2.plot(ax=ax)
plt.show()

DataFrame处缩短一行,导致数据提前12小时结束,但在其他情况下符合预期:

results in a shorter line for the DataFrame where the data end some 12 hours earlier, but otherwise as expected:

这篇关于在具有多个图形的同一图形上绘制多组数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆