在 matplotlib 日期中的特定日期插入垂直线的问题
problem with inserting vertical lines in specific days in matplotlib date
我想在我的绘图中插入一些垂直虚线。我使用以下代码,遇到错误“ValueError: view limit minimum -34758.04999999988 is less than 1 and is an invalid Matplotlib date value. 如果将非日期时间值传递给具有日期时间单位的轴,通常会发生这种情况” .这是我的数据示例。
Date M
2013-03-13 0.727195
2013-03-14 0.727195
2013-03-15 0.727195
2013-03-16 0.727195
2013-03-17 0.727195
... ...
2018-11-12 0.115674
2018-11-13 -0.427214
2018-11-14 -0.389715
2018-11-15 0.427149
2018-11-16 -0.416864
[2075 rows x 1 columns]
这是我的代码
import pandas as pd
from datetime import datetime, timedelta
from matplotlib import pyplot as plt
from matplotlib import dates as mpl_dates
data=pd.read_excel('ff.xlsx')
data['Date']=pd.to_datetime(data['Date'], format="%Y-%m-%d")
date = data['Date']
amount = data['M']
data.set_index('Date', inplace=True, drop=True)
plt.plot(date,amount, color='blue')
ax = plt.axes()
ax.yaxis.grid()
plt.ylabel('dvv percentage')
xposition = [2015-11-11, 2014-11-11]
for xc in xposition:
plt.axvline(x=xc, color='k', linestyle='--')
plt.show()
这应该有效:
from matplotlib import pyplot as plt
import pandas as pd
# example df:
df = pd.DataFrame({'Date':['2013-03-13','2015-03-14','2019-03-15'],
'M':[0.727195, -0.727195, 0.669195]})
# ensure datetime:
df['Date'] = pd.to_datetime(df['Date'])
ax = plt.plot(df.Date, df.M, color='blue')
ax = plt.axes()
ax.yaxis.grid()
plt.ylabel('dvv percentage')
# vertical line position as datetime dtype:
xposition = pd.to_datetime(['2015-11-11', '2014-11-11'])
for xc in xposition:
ax.axvline(x=xc, color='k', linestyle='--')
plt.show()
我想在我的绘图中插入一些垂直虚线。我使用以下代码,遇到错误“ValueError: view limit minimum -34758.04999999988 is less than 1 and is an invalid Matplotlib date value. 如果将非日期时间值传递给具有日期时间单位的轴,通常会发生这种情况” .这是我的数据示例。
Date M
2013-03-13 0.727195
2013-03-14 0.727195
2013-03-15 0.727195
2013-03-16 0.727195
2013-03-17 0.727195
... ...
2018-11-12 0.115674
2018-11-13 -0.427214
2018-11-14 -0.389715
2018-11-15 0.427149
2018-11-16 -0.416864
[2075 rows x 1 columns]
这是我的代码
import pandas as pd
from datetime import datetime, timedelta
from matplotlib import pyplot as plt
from matplotlib import dates as mpl_dates
data=pd.read_excel('ff.xlsx')
data['Date']=pd.to_datetime(data['Date'], format="%Y-%m-%d")
date = data['Date']
amount = data['M']
data.set_index('Date', inplace=True, drop=True)
plt.plot(date,amount, color='blue')
ax = plt.axes()
ax.yaxis.grid()
plt.ylabel('dvv percentage')
xposition = [2015-11-11, 2014-11-11]
for xc in xposition:
plt.axvline(x=xc, color='k', linestyle='--')
plt.show()
这应该有效:
from matplotlib import pyplot as plt
import pandas as pd
# example df:
df = pd.DataFrame({'Date':['2013-03-13','2015-03-14','2019-03-15'],
'M':[0.727195, -0.727195, 0.669195]})
# ensure datetime:
df['Date'] = pd.to_datetime(df['Date'])
ax = plt.plot(df.Date, df.M, color='blue')
ax = plt.axes()
ax.yaxis.grid()
plt.ylabel('dvv percentage')
# vertical line position as datetime dtype:
xposition = pd.to_datetime(['2015-11-11', '2014-11-11'])
for xc in xposition:
ax.axvline(x=xc, color='k', linestyle='--')
plt.show()