熊猫-绘制两个数据框,其中一个作为标记

时间:2018-12-07 18:39:29

标签: python pandas matplotlib plot

我有两个dataFrame。两者都有日期时间作为索引。

                     PM 2.5  PM 10  entry_id
created_at
2018-06-13 16:11:43    4.67   5.17        20
2018-06-13 16:12:43    5.04   5.59        21
2018-06-13 16:13:43    5.40   6.10        22
2018-06-13 16:14:43    5.03   5.55        23
2018-06-13 16:15:43    4.93   5.44        24

                          action              end_at
done_at
2018-11-10 10:15:00  Babymassage 2018-11-10 12:00:00
2018-11-24 10:15:00  Babymassage 2018-11-24 12:00:00
2018-12-01 10:15:00  Babymassage 2018-12-01 12:00:00
2018-12-01 14:30:00  staubsaugen                 NaT
2018-12-01 15:30:00  Ausser Haus 2018-12-03 12:00:00

我想在x上绘制时间,在PM 2.5和pm 10上绘制时间。并且第二个数据帧中的操作列应该是带有索引索引中x的文本标签。 大熊猫有可能吗?我尝试了此方法,但没有成功

#Import Modules
from sql_connection import sql_connector
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
from datetime import date

#Get data from SQL server
df = pd.read_sql("feinstaub_wohnzimmer",con=sql_connector("mysql","localhost","3306","root","xxx","feinstaub"),index_col="created_at")
df1 = pd.read_sql("wohnzimmer_aktionen",con=sql_connector("mysql","localhost","3306","root","xxx","feinstaub"),index_col="done_at")

#Sort index
df = df.sort_index(axis=0)
df1 = df1.sort_index(axis=0)

#Group by Day 
dfs = dict(tuple(df.groupby(df.index.strftime('%Y-%m-%d'))))
dfd = dict(tuple(df1.groupby(df1.index.strftime('%Y-%m-%d'))))

#Plot for Today
today = str(date.today())
ax = dfd[today].plot(marker="o", label="action")
dfs[today].plot( y=["PM 2.5","PM 10"], kind='line', ax=ax)
plt.show()

应该看起来像这样 enter image description here 还是您认为当我在end_at列中使用附加信息时,也可以在动作持续时间内绘制一个符号吗?

0 个答案:

没有答案