gpt4 book ai didi

python - pandas用plot画线图,时间不连续,如何让线连续?

转载 作者:行者123 更新时间:2023-12-04 03:37:50 30 4
gpt4 key购买 nike

我有这样的数据框:

import pandas as pd

dataA = [["2005-1-20", "9:35", 5], ["2005-1-20", "9:40", 8], ["2005-1-20", "9:45", 7],
["2005-1-20","9:50", 4], ["2005-1-20", "10:25", 2], ["2005-1-20", "10:30", 2],
["2005-1-20", "10:35", 15], ["2005-1-20", "10:40", 6], ["2005-1-20", "10:45", 1],
["2005-1-20", "10:50", 11], ["2005-1-20", "10:55", 12], ["2005-1-20", "11:00", 7],
["2005-1-21", "9:35", 2], ["2005-1-21", "9:40", 3], ["2005-1-21", "9:45", 4],
["2005-1-21","9:50", 4], ["2005-1-21", "10:25", 5], ["2005-1-21", "10:30", 9],
["2005-1-21", "10:35", 4], ["2005-1-21", "10:40", 3], ["2005-1-21", "10:45", 5],
["2005-1-21", "10:50", 4], ["2005-1-21", "10:55", 4], ["2005-1-21", "11:00", 8],
["2005-1-22", "9:35", 12], ["2005-1-22", "9:40", 13], ["2005-1-22", "9:45", 14],
["2005-1-22","9:50", 14], ["2005-1-22", "10:25", 15], ["2005-1-22", "10:30",19],
["2005-1-22", "10:35", 14], ["2005-1-22", "10:40", 13], ["2005-1-22", "10:45", 15],
["2005-1-22", "10:50", 14], ["2005-1-22", "10:55", 14], ["2005-1-22", "11:00", 18]]

df = pd.DataFrame(data = dataA, columns=["date", "min", "val"])

df['min']=pd.to_datetime(df['min'],format= '%H:%M' ).dt.time

我用plot画时间序列折线图的时候,因为没有9:50-10:20的数据,折线不连续:

df = df.pivot('min','date','val')
df.plot()

像这样的图片: pic

如何忽略9:50-10:20时间段画一条连续的线?

最佳答案

I want to connect the 9:50 point directly to the 10:20 point, and the distance between the two points is only 5 minutes, instead of automatically filling in the 30-minute interval as it is currently

如果将 min 列转换为字符串,它将显示为分类变量,点之间的距离相等:

df['min'] = pd.to_datetime(df['min'], format='%H:%M').dt.time.astype(str)

df = df.pivot('min','date','val')
df.plot()

输出:

chart

关于python - pandas用plot画线图,时间不连续,如何让线连续?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66561467/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com