简体   繁体   English

如何在时间序列轴上绘制一条垂直线?

[英]How to plot a vertical line on a time series axis?

I would like to 'link' two pieces of code.我想“链接”两段代码。 One,一,

x= df[df['Value']==True].sort_values(by='Date').head(1).Date

Out[111]:
8    2020-03-04

extracts the date where the first value appears;提取第一个值出现的日期; the other one另一个

df[df['Buy']==1].groupby('Date').size().plot(ax=ax, label='Buy')

should plot some information through time.应该通过时间绘制一些信息。

I would like to add a vertical line at the first date where the value is true, ie 2020-03-04 .我想在值为 true 的第一个日期添加一条垂直线,即2020-03-04 In order to do it, I would need to extract this information from the first code (not using copy and paste) to the other piece of code which generates the plot.为了做到这一点,我需要将此信息从第一个代码(不使用复制和粘贴)提取到生成绘图的另一段代码。 Can you give me some guide on how to do it?你能给我一些关于如何做的指南吗? Thanks a lot非常感谢

Update:更新:

I tried as follows:我试过如下:

x= df[df['Value']==True].sort_values(by='Date').head(1).Date.tolist()

Out[111]:
8    ['2020-03-04']

df[df['Buy']==1].groupby('Date').size().plot(ax=ax, label='Buy')

ax.axvline(x, color="red", linestyle="--")

but I got a TypeError: unhashable type: 'numpy.ndarray'但我得到了一个 TypeError: unhashable type: 'numpy.ndarray'

Some data:一些数据:

Date           Buy      Value
0   2020-02-23  0   False
1   2020-02-23  0   False
2   2020-02-25  0   False
3   2020-02-27  1   False
4   2020-03-03  1   False
5   2020-03-03  1   False
6   2020-03-03  0   False
7   2020-03-04  1   False
8   2020-03-04  0   True
9   2020-03-04  0   True
10  2020-03-04  1   False
11  2020-03-05  0   True
12  2020-03-05  1   False
13  2020-03-05  1   False
14  2020-03-05  1   False
15  2020-03-06  0   False
16  2020-03-06  1   False
17  2020-03-06  1   False
18  2020-03-07  1   False
19  2020-03-07  1   False
20  2020-03-07  1   False
21  2020-03-08  1   False
22  2020-03-08  1   False
23  2020-03-09  1   False
24  2020-03-09  1   False
25  2020-03-09  1   False
26  2020-03-10  1   False
27  2020-03-10  1   False
28  2020-03-10  1   False
29  2020-03-10  0   True
30  2020-03-11  1   False
31  2020-03-11  1   False
32  2020-03-13  0   True
33  2020-03-13  0   False
34  2020-03-15  0   True
35  2020-03-16  0   False
36  2020-03-19  0   False
37  2020-03-22  0   True
  • Make certain the Date column is in a datetime format.确保Date列采用datetime格式。
import pandas as pd
import random  # for test data
import matplotlib.pyplot as plt

# setup sample data
random.seed(365)
rows = 40
data = {'Date': [random.choice(pd.bdate_range('2020-02-23', freq='d', periods=rows).strftime('%Y-%m-%d').tolist()) for _ in range(rows)],
        'Buy': [random.choice([0, 1]) for _ in range(rows)],
        'Value': [random.choice([False, True]) for _ in range(rows)]}

df = pd.DataFrame(data)

# set the Date column to a datetime
df.Date = pd.to_datetime(df.Date)

# extract values
x = df[df['Value']==True].sort_values(by='Date').head(1).Date

# groupby and plot
ax = df[df['Buy']==1].groupby('Date').size().plot(figsize=(7, 5), label='Buy')

# plot the vertical line; axvline works as long as x is one value
ax.axvline(x, color="red", linestyle="--", label='my value') 

# show the legend
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')

在此处输入图片说明

package versions包版本

import matplotlib as mpl

print(mpl.__version__)
print(pd.__version__)

[out]:
3.3.1
1.1.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM