Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引(x轴)为datetime64[ns]不工作_Python_Python 3.x_Pandas_Matplotlib - Fatal编程技术网

Python 索引(x轴)为datetime64[ns]不工作

Python 索引(x轴)为datetime64[ns]不工作,python,python-3.x,pandas,matplotlib,Python,Python 3.x,Pandas,Matplotlib,我有一个数据框,看起来像: Open time Open High Low Close 4_EMA 20_EMA Position 0 2021-03-30 12:00:00 1848.86 1849.04 1826.05 1830.61 1830.610000 1830.610000 NaN 1 2021-03-30 16:00:00 1830.61 1858.29 1827.78

我有一个数据框,看起来像:

           Open time     Open     High      Low    Close        4_EMA       20_EMA  Position
0 2021-03-30 12:00:00  1848.86  1849.04  1826.05  1830.61  1830.610000  1830.610000       NaN
1 2021-03-30 16:00:00  1830.61  1858.29  1827.78  1858.27  1841.674000  1833.244286       1.0
2 2021-03-30 20:00:00  1858.27  1859.13  1833.50  1840.46  1841.188400  1833.931497       0.0
3 2021-03-31 00:00:00  1840.41  1862.55  1810.99  1823.17  1833.981040  1832.906592       0.0
4 2021-03-31 04:00:00  1823.18  1855.55  1801.00  1813.89  1825.944624  1831.095488      -1.0
...

我的绘图代码是:

import pandas as pd
import matplotlib.pyplot as plt

df4 = pd.read_csv('data/df_4h.csv', parse_dates=['Open time']) 

#df4.set_index('Open time', inplace=True) # this line causes the problem

fig, (ax2) = plt.subplots(1, 1, figsize=(15, 10))

df4['Close'].plot(ax=ax2, color = 'k', lw = 1, label = 'Close Price')
df4['4_EMA'].plot(ax=ax2, color = 'b', lw = 1, label = '4_EMA')
df4['20_EMA'].plot(ax=ax2, color = 'g', lw = 1, label = '20_EMA')
        
# plot 'buy' signals
ax2.plot(df4[df4['Position'] == 1].index, 
        df4['4_EMA'][df4['Position'] == 1], 
        '^', markersize = 15, color = 'g', alpha = 0.7, label = 'buy')

# plot 'sell' signals
ax2.plot(df4[df4['Position'] == -1].index, 
        df4['4_EMA'][df4['Position'] == -1], 
        'v', markersize = 15, color = 'r', alpha = 0.7, label = 'sell')


plt.show()
像这样很有效:

问题是,当我尝试
将_index
设置为
打开时间
列时,箭头不再显示,我无法理解为什么,我需要在x轴刻度上插入此列,因此如果我:

...
df4.set_index('Open time', inplace=True)
...
生成的绘图不带箭头(买入和卖出,
df4['4_-EMA'][df4['Position']==1]
df4['4_-EMA'][df4['Position']==1]
):


如何保持箭头并使日期时间在x轴上?

问题是您正在将绘图与
pandas
(使用
df.plot
)和
matplotlib
(使用
ax2.plot
)相结合。
pandas
选项使用转换后的索引绘制

您可以将两者都更改为使用
pandas
,例如:

df4 = pd.read_csv('https://pastebin.pl/view/raw/1046cfca',
                  parse_dates=['Open time']) 

df4.set_index('Open time', inplace=True)

fig, (ax2) = plt.subplots(1, 1, figsize=(15, 10))

df4['Close'].plot(ax=ax2, color = 'k', lw = 1, label = 'Close Price')
df4['4_EMA'].plot(ax=ax2, color = 'b', lw = 1, label = '4_EMA')
df4['20_EMA'].plot(ax=ax2, color = 'g', lw = 1, label = '20_EMA')
        
ax2.set_title("4H time frame")

# ----------------------------------------------------------------------
# the following is changed to use `df.plot`:
# ----------------------------------------------------------------------

# plot 'buy' signals
df4.loc[df4['Position']==1, '4_EMA'].plot(
    ls='None', marker='^', markersize = 15,
    color = 'g', alpha = 0.7, label = 'buy', ax=ax2)

# plot 'sell' signals
df4.loc[df4['Position']==-1, '4_EMA'].plot(
    ls='None', marker='v', markersize = 15,
    color = 'r', alpha = 0.7, label = 'sell', ax=ax2)

plt.show()
输出: