Python 我可以在同一子批次中从函数或数组中绘制pandas.DataFrame和法线吗?

Python 我可以在同一子批次中从函数或数组中绘制pandas.DataFrame和法线吗?,python,pandas,datetime,matplotlib,Python,Pandas,Datetime,Matplotlib,我有一个pandasDataFrame,它按我的意愿堆叠和重新组织,如下所示: print(df.unstack(level=0.cumsum()) 任务1020 1021 1022 1023 7141 参赛日期 2019-03-31楠楠32930.0楠楠 2019-04-30 28845.0 16695.0 27427.5 127476.0 NaN 2019-05-31 57465.0 85745.0 NaN 142561.0 3515.61 2019-06-30南90515.0南南 我用以

我有一个pandas
DataFrame
,它按我的意愿堆叠和重新组织,如下所示:

print(df.unstack(level=0.cumsum())
任务1020 1021 1022 1023 7141
参赛日期
2019-03-31楠楠32930.0楠楠
2019-04-30 28845.0 16695.0 27427.5 127476.0 NaN
2019-05-31 57465.0 85745.0 NaN 142561.0 3515.61
2019-06-30南90515.0南南
我用以下几行绘制此图:

fig, ax1 = plt.subplots(1)
grouped_by_task.unstack(level=0).cumsum().plot(ax=ax1, style = '.-')
现在,我想添加另一个图(为了简单起见,在两点之间添加一条直线)

这条线的这些端点由以下公式给出:

startmonth='2019-03'
月末='2022-03'
开始月份=datetime.strtime(开始月份,%Y-%m)
end_month=datetime.strtime(endmonth,%Y-%m)
预算=[0,1000000]
时间=[开始月份,结束月份]
我正在努力在同一个绘图上获得线条子绘图

我发现了一些关于如何在同一子批中添加来自多个
DataFrame
s的数据的讨论,但不是一个
DataFrame
以及不在
DataFrame
中的值

我尝试使用以下行进行打印:

fig, ax1 = plt.subplots(1)
grouped_by_task.unstack(level=0).cumsum().plot(ax=ax1, style = '.-')
按任务分组。取消堆栈(级别=0)。累计()绘图(ax=ax1,样式='.-')#(1)
ax1.绘图(时间、预算“-”)#(2)
plt.xlim(datetime.strtime(startmonth,%Y-%m)),
datetime.strTime(月末,“%Y-%m”)#(3)
仅使用第(1)行,我得到一个图,其中x轴和y轴都由
数据框中的数据确定

对于第(3)行,x轴也被正确确定(比
数据框中可用的数据长)

使用所有3条线,我将y轴调整为根据第(2)行中的数据调整的值,但没有该行

如果注释掉第(1)行,则可以正确绘制该行

唯一的区别是x轴TIC处的值写入方式不同


关于我做错了什么,有什么建议吗?

我尽可能多地使用了你的数据。你的例子不能完全重现。无论如何,改变绘制线条的顺序,设置限制并绘制数据框为我解决了问题。数据框必须排在最后。对不起,不知道为什么…
我使用的数据:

Task           1020     1021     1022      1023     7141
2019-03-31      NaN      NaN      NaN   32930.0      NaN
2019-04-30  28845.0  16695.0  27427.5  127476.0      NaN
2019-05-31  57465.0  85745.0      NaN  142561.0  3515.61
2019-06-30      NaN  90515.0      NaN       NaN      NaN
解析数据:

import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
df = pd.read_clipboard(sep='\s\s+')
df['Task'] = pd.to_datetime(df['Task'])
df = df.set_index('Task')
start_month = datetime.strptime('2019-03', "%Y-%m")
end_month = datetime.strptime('2022-03', "%Y-%m")
budget = [0, 1000000]
以及绘图部分。请注意,df位于最后:

fig, ax1 = plt.subplots()
ax1.plot([start_month, end_month], budget, '-', label='budget')
df.groupby(df.index).cumsum().plot(ax=ax1, style='.-', xlim=(start_month, end_month))
ax1.legend()

非常感谢!这很有效。如果有人知道原因,它可能是有用的信息添加。更改绘图顺序非常有效。很抱歉数据不可复制。下次我会做得更好。