[英]How to plot timeseries data in a dataframe using matplotlib
I have loaded some stock price data into a dataframe.我已将一些股票价格数据加载到 dataframe 中。 I want to quickly plot the close price on Y axis, and date on the X axis.我想快速 plot Y 轴上的收盘价和 X 轴上的日期。
This is what my dataframe looks like:这是我的 dataframe 的样子:
Open High Low Close Adjusted_close Volume
Date
1980-12-11 22.000 22.000 22.000 22.000 0.0308 0
1980-12-12 28.750 28.876 28.750 28.750 0.0395 2093900
1980-12-15 27.250 27.376 27.250 27.250 0.0375 785200
1980-12-16 25.250 25.376 25.250 25.250 0.0350 472000
1980-12-17 25.876 26.000 25.876 25.876 0.0359 385900
When I type df.plot()
it plots something that looksl like a histogram.当我输入df.plot()
时,它会绘制一些看起来像直方图的东西。
When I type df.plot('Close')
it plots a bunch of squiggly lines.当我输入df.plot('Close')
时,它会绘制一堆波浪线。
I have two questions:我有两个问题:
I just tried with the same data.我只是尝试使用相同的数据。 I had to draw 02 plots in the same figure and had to add colors based on the new column as shown.我必须在同一图中绘制 02 个图,并且必须根据新列添加 colors,如图所示。
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
# data wrangling
columns = ['date', 'open', 'high', 'low', 'close', 'adjusted_close', 'volume']
df = pd.read_csv('timeseries.csv', parse_dates=True, names=columns)
#df['date'] = pd.to_datetime(df['date'])
signals = [True, True, False, False, True]
df['signals'] = signals
# plots
plt.plot(df['date'], df['close'])
kcolors = ['green' if s else 'red' for s in df['signals']]
plt.scatter(df['date'], df['close'], c=kcolors)
#rotate label if too long
plt.xticks(rotation=60)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.