如何在VSCode中使用pandas绘制时间序列数据

huangapple go评论54阅读模式
英文:

How to plot timeseries data in VSCode using pandas

问题

以下是代码的翻译部分:

这里是一些我正在使用的代码尝试在VSCode的Jupyter笔记本中绘制来自yfinance的BP股票数据的时间序列

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns

# 导入yfinance包
import yfinance as yf

# 设置开始和结束日期
start_date = '1990-01-01'
end_date = '2021-07-12'

# 设置股票代号
ticker = 'BP'

# 获取数据
data = yf.download(ticker, start_date, end_date)

# 打印5行数据
data.tail()

结果:

            Open        High        Low        Close    Adj Close    Volume
Date
2021-07-02  26.959999   27.049999   26.709999   26.980000   25.208590   5748800
2021-07-06  26.900000   26.920000   25.730000   25.969999   24.264900   17917900
2021-07-07  25.780001   26.120001   25.469999   25.730000   24.040659   13309100
2021-07-08  25.230000   25.790001   25.200001   25.580000   23.900505   10075500
2021-07-09  25.840000   26.100000   25.690001   26.010000   24.302273   7059700

到目前为止一切正常。

然后,我只想绘制数据。

我的代码:

sns.lineplot(data=data['Close'], label='Close')
plt.show()

结果是:

<Figure size 640x480 with 1 Axes>

但没有绘图。

是否需要更改设置来绘制数据?

英文:

here's some code that I'm using to try to plot a timeseries of BP share data from yfinance in a Jupyter notebook in VSCode.

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns

# Import yfinance package
import yfinance as yf

# Set the start and end date
start_date = &#39;1990-01-01&#39;
end_date = &#39;2021-07-12&#39;

# Set the ticker
ticker = &#39;BP&#39;

# Get the data
data = yf.download(ticker, start_date, end_date)

# Print 5 rows
data.tail()

The result:

	        Open	    High	    Low	        Close	    Adj Close	Volume
Date						
2021-07-02	26.959999	27.049999	26.709999	26.980000	25.208590	5748800
2021-07-06	26.900000	26.920000	25.730000	25.969999	24.264900	17917900
2021-07-07	25.780001	26.120001	25.469999	25.730000	24.040659	13309100
2021-07-08	25.230000	25.790001	25.200001	25.580000	23.900505	10075500
2021-07-09	25.840000	26.100000	25.690001	26.010000	24.302273	7059700

So far, so hunky dory.

Then I just want to plot the data.

My code:

sns.lineplot(data=data[&#39;Close&#39;], label=&#39;Close&#39;)
plt.show()

The result is:

&lt;Figure size 640x480 with 1 Axes&gt;

But no plot.

Is there a setting I need to change to plot the data?

答案1

得分: 1

你需要执行以下代码:

sns.lineplot(x=data.index, y=data['Close'], label='Close')
plt.show()

这将生成如下图表:

如何在VSCode中使用pandas绘制时间序列数据

英文:

You need to do this:

sns.lineplot(x=data.index, y=data[&#39;Close&#39;], label=&#39;Close&#39;)
plt.show()

which gives

如何在VSCode中使用pandas绘制时间序列数据

答案2

得分: 1

你需要将程序顶部的%matplotlib inline注释掉。
在Jupyter笔记本中绘制数据时需要这样做。当你想要通过Python脚本在单独的窗口中绘制结果时,不要使用它。

英文:

You need to comment out %matplotlib inline at the top of your program.
This is needed when plotting data in jupyter notebook. When you want to plot the results in a separate window via a python script, don't use it.

huangapple
  • 本文由 发表于 2023年2月8日 20:09:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定