英文:
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 = '1990-01-01'
end_date = '2021-07-12'
# Set the ticker
ticker = 'BP'
# 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['Close'], label='Close')
plt.show()
The result is:
<Figure size 640x480 with 1 Axes>
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()
这将生成如下图表:
英文:
You need to do this:
sns.lineplot(x=data.index, y=data['Close'], label='Close')
plt.show()
which gives
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论