gpt4 book ai didi

python - 图表上的烦人日期

转载 作者:行者123 更新时间:2023-12-04 11:46:38 25 4
gpt4 key购买 nike

我正在尝试向下添加一条直线,该直线将在该线上垂直打印日期。我在下面添加了我如何尝试完成此操作的图片。我还包含了我试图注释的代码。
我的代码:

import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
import numpy as np

start = datetime.date(2015,1,1)
end = datetime.date.today()
start1 = datetime.date(2019,1,1)

data = web.DataReader("^GSPC", 'yahoo',start, end)
data1 = web.DataReader("^GSPC", 'yahoo', start1, end)

data.index = pd.to_datetime(data.index, format ='%Y-%m-%d')
data1.index = pd.to_datetime(data1.index, format ='%Y-%m-%d')

full_dates = pd.date_range(start, end)
data = data.reindex(full_dates)
data1 = data1.reindex(full_dates)

data.set_index('month',append=True,inplace=True)
data1.set_index('month',append=True,inplace=True)
data.set_index('week',append=True,inplace=True)
data1.set_index('week',append=True,inplace=True)
data.set_index('day',append=True,inplace=True)
data1.set_index('day',append=True,inplace=True)

data['pct_day']= data['Adj Close'].pct_change()
data1['pct_day']= data1['Adj Close'].pct_change()

df = data.groupby(['month', 'day']).mean()
df2 = data1.groupby(['month', 'day']).mean()

df['cumsum_pct_day']=df['pct_day'].cumsum(axis = 0)
df2['cumsum_pct_day']=df2['pct_day'].cumsum(axis = 0)

ax = df.plot(y='cumsum_pct_day', grid = True, label='df')
df2.plot(y='cumsum_pct_day', grid= True, ax=ax, label='df2')

ylims = ax.get_ylim()
ax.vlines(end, ylims[0], data1.Close[0], linestyles='--')
ax.text(end, data1.Close[0], end, ha='right', va='top', rotation=90)
ax.set_ylim(ylims)


plt.show()
enter image description here
出于某种原因,我收到警告:
enter image description here
但这条线没有绘制。你能建议为什么它不这样做吗?

最佳答案

而不是 vlines绘制多条线,您可以使用方法 axvline添加一条垂直线:

from datetime import timedelta

def gen_df(size):
arr1 = pd.date_range(start='1/1/2018', periods=size)
arr2 = np.random.exponential(size=size).cumsum()
return pd.DataFrame({'col1': arr1, 'col2': arr2})

df1 = gen_df(60)
df2 = gen_df(50)

ax = df1.plot(x='col1', y='col2', label='df1')
df2.plot(x='col1', y='col2', ax=ax, label='df2')
ax.axvline(x=df2['col1'].max(), color='red')
ax.annotate(s=df2['col1'].max().date(), xy=(df2['col1'].max()-timedelta(days=2), 35), rotation=90)

enter image description here

让我们重现您的数据集:
def gen_df(size):
arr1 = pd.date_range(start='1/1/2018', periods=size)
arr2 = np.random.standard_exponential(size)
return pd.DataFrame({'Adj Close': arr2}, index=arr1)

df1 = gen_df(150)
df2 = gen_df(130)

print(df1.head())

输出:
            Adj Close
2018-01-01 0.061166
2018-01-02 0.669330
2018-01-03 0.123332
2018-01-04 0.029007
2018-01-05 1.024210
for df in [df1, df2]:
df['year'] = df.index.year
df['month'] = df.index.month
df['week'] = df.index.week
df['day'] = df.index.day
df.set_index('month', append=True, inplace=True)
df.set_index('week', append=True, inplace=True)
df.set_index('day', append=True, inplace=True)
df['pct_day']= df['Adj Close'].pct_change()

print(df1.head())

输出:
                           Adj Close  year    pct_day
month week day
2018-01-01 1 1 1 0.061166 2018 NaN
2018-01-02 1 1 2 0.669330 2018 9.942917
2018-01-03 1 1 3 0.123332 2018 -0.815739
2018-01-04 1 1 4 0.029007 2018 -0.764804
2018-01-05 1 1 5 1.024210 2018 34.308892
df1 = df1.groupby(['month', 'day']).mean()
df1['cumsum_pct_day'] = df1['pct_day'].cumsum(axis = 0)

df2 = df2.groupby(['month', 'day']).mean()
df2['cumsum_pct_day'] = df2['pct_day'].cumsum(axis = 0)

print(df1.head())

输出:
           Adj Close  year    pct_day  cumsum_pct_day
month day
1 1 0.061166 2018 NaN NaN
2 0.669330 2018 9.942917 9.942917
3 0.123332 2018 -0.815739 9.127178
4 0.029007 2018 -0.764804 8.362375
5 1.024210 2018 34.308892 42.671267

添加一行 axvline :
ax = df1.plot(y ='cumsum_pct_day', label='df1')
df2.plot(y ='cumsum_pct_day', ax=ax, label='df2')

df = df1 if len(df1) < len(df2) else df2 # get a smaller DataFrame
ax.axvline(x=len(df), color='red')

y_min, y_max = ax.get_ylim()
middle = (y_max - y_min) / 2
ax.annotate(s=df.index.max(), xy=(len(df) - 5, middle), rotation=90)

axvline

vlines 添加多行:
ax = df1.plot(y ='cumsum_pct_day', label='df1')
df2.plot(y ='cumsum_pct_day', ax=ax, label='df2')

y_min, y_max = ax.get_ylim()
ax.vlines(x=np.arange(len(df2), len(df1), step=.1), ymin=y_min, ymax=y_max, color='red')

middle = (y_max - y_min) / 2
ax.annotate(s=df.index.max(), xy=(len(df) - 5, middle), rotation=90)

enter image description here

关于python - 图表上的烦人日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59190666/

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