skip to Main Content

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?

2

Answers


  1. You need to do this:

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

    which gives

    enter image description here

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search