<p>因此,以下代码从头开始创建一个与您的数据帧相似的数据帧,并生成您要求的绘图:</p>
<pre><code>import pandas as pd
import datetime
import numpy as np
from matplotlib import pyplot as plt
# The following two lines are not mandatory for the code to work
import matplotlib.style as style
style.use('dark_background')
def create_datetime_range(numdays=10):
"""Creates the timestamp range"""
base = datetime.datetime.today()
datelist = pd.date_range(base, periods=numdays).to_pydatetime()
return datelist
def convert_to_date(datetime_list):
"""Converts a timestamp array into a date array"""
return [x.date() for x in datetime_list]
a = pd.DataFrame(
{
'ISP.MI': np.random.normal(2,1,10),
'Ctrv' : np.random.normal(200,150,10)
},
index=convert_to_date(create_date_range())
)
a.plot()
</code></pre>
<p><a href="https://i.stack.imgur.com/QkofY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QkofY.png" alt="enter image description here"/></a></p>
<p>但是,我相信您的数据帧在两个方面是不同的:</p>
<ol>
<li>索引中似乎有两个级别(日期标题似乎位于Ticker标题的第二行)。我想这可能是因为您使用了诸如.groupby()或.unstack()或其他聚合/透视方法。我建议您查看reset_index()方法。</li>
</ol>
<p>2.你的数据框有更多你需要的列。正如@jezrael所建议的,你应该首先只选择这些。你可以这样做:</p>
<pre><code>df[['ISP.MI','Ctrv']]
</code></pre>
<p>然后在较小的数据帧上使用.plot()方法,让pandas处理其余部分。</p>