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

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

How to plot timeseries data in VSCode using pandas

问题

以下是代码的翻译部分:

  1. 这里是一些我正在使用的代码尝试在VSCodeJupyter笔记本中绘制来自yfinanceBP股票数据的时间序列
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. %matplotlib inline
  5. import seaborn as sns
  6. # 导入yfinance包
  7. import yfinance as yf
  8. # 设置开始和结束日期
  9. start_date = '1990-01-01'
  10. end_date = '2021-07-12'
  11. # 设置股票代号
  12. ticker = 'BP'
  13. # 获取数据
  14. data = yf.download(ticker, start_date, end_date)
  15. # 打印5行数据
  16. data.tail()

结果:

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

到目前为止一切正常。

然后,我只想绘制数据。

我的代码:

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

结果是:

  1. <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.

  1. import pandas as pd
  2. import matplotlib.pyplot as plt
  3. %matplotlib inline
  4. import seaborn as sns
  5. # Import yfinance package
  6. import yfinance as yf
  7. # Set the start and end date
  8. start_date = &#39;1990-01-01&#39;
  9. end_date = &#39;2021-07-12&#39;
  10. # Set the ticker
  11. ticker = &#39;BP&#39;
  12. # Get the data
  13. data = yf.download(ticker, start_date, end_date)
  14. # Print 5 rows
  15. data.tail()

The result:

  1. Open High Low Close Adj Close Volume
  2. Date
  3. 2021-07-02 26.959999 27.049999 26.709999 26.980000 25.208590 5748800
  4. 2021-07-06 26.900000 26.920000 25.730000 25.969999 24.264900 17917900
  5. 2021-07-07 25.780001 26.120001 25.469999 25.730000 24.040659 13309100
  6. 2021-07-08 25.230000 25.790001 25.200001 25.580000 23.900505 10075500
  7. 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:

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

The result is:

  1. &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

你需要执行以下代码:

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

这将生成如下图表:

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

英文:

You need to do this:

  1. sns.lineplot(x=data.index, y=data[&#39;Close&#39;], label=&#39;Close&#39;)
  2. 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:

确定