2

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?

4
  • 1
    plt.show() is not needed and should be disabled. Commented Feb 8, 2023 at 12:32
  • It doesn't work either way. Commented Feb 8, 2023 at 12:53
  • 1
    Assuming you're using Jupyter Notebook. Try to set %matplotlib inline before the matplotlib import. You also don't need plt.show(). See also: stackoverflow.com/a/43550882/15637435 Commented Feb 8, 2023 at 13:01
  • That worked. I also didn't know about output renderers in VSCode. Thank you both. Commented Feb 8, 2023 at 13:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to do this:

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

which gives

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.