简体   繁体   English

如何使用 matplotlib 在 dataframe 中的 plot 时间序列数据

[英]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:我有两个问题:

  1. How do I quickly plot close price versus date我如何快速 plot 收盘价与日期
  2. Assuming I have two other columns ('Buy' and 'Sell') which are boolean flags, in the dataframe, how can I plot the 'Buy' and 'Sell' points using say a green up arrow and a red down arrow on the same plot ? Assuming I have two other columns ('Buy' and 'Sell') which are boolean flags, in the dataframe, how can I plot the 'Buy' and 'Sell' points using say a green up arrow and a red down arrow on the相同的 plot吗?

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()

在此处输入图像描述

What you described works perfectly for me.你所描述的对我来说非常有效。

import pandas as pd
df = pd.read_clipboard()
df['Close'].plot()

在此处输入图像描述 Are you sure you converted the index to pandas datetime?您确定将索引转换为 pandas 日期时间吗? if not try如果不尝试

df.index = pd.to_datetime(df.index)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何绘制(在matplotlib中)python pandas数据帧包含两列,时间序列之一,值之一? - How to plot (in matplotlib) python pandas dataframe containing two columns, one of timeseries and one of value? 如何在 Python Matplotlib 中的同一 x 轴上 plot 具有不同开始日期的多个时间序列数据? - How to plot multiple timeseries data with different start date on the same x-axis in Python Matplotlib? 来自每日时间序列数据的 matplotlib 中的每月阴影误差/标准图 - Monthly shaded error/std plot in matplotlib from daily timeseries data 如何使用 groubby 使用 matplotlib 绘制数据 - how to plot data using matplotlib using groubby Plot 和 pandas dataframe 使用 ZF02113237A5A5FFF03E34C9EEEB464 数据组 - Plot a pandas dataframe using matplotlib with data grouped by year/month 时间序列 plot 和 Matplotlib 中的附加线 - An additional line in timeseries plot with Matplotlib 使用Pandas DataFrame和Matplotlib将CSV文件中的数据处理到绘图中 - Using pandas dataframe and matplotlib to manipulate data from a csv file into a plot 如何使用Matplotlib对熊猫数据框的数据进行分类和绘制? - How do I categorise and plot data from a Pandas dataframe using Matplotlib? 如果列表示月份并且索引是年份,如何按行读取 Pandas 的 dataframe 和 plot 中的值作为时间序列? - How to read the data in Pandas's dataframe row wise and plot the values as a timeseries if column represents months and index is years? 在matplotlib中使用数据框绘制3d图 - plot a 3d plot using dataframe in matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM