3

I'm trying to generate a heatmap showing the correlation between different stock prices before running through some ML for a project for a course I'm taking at work. The relevant code is shown below and a picture of the Pandas data frame with the data being run on is attached as well. There's no missing numbers in the dataframe either. When I run the code the plot simply doesn't load. It forever stays in limbo. I've tried to find other questions with the same problem but could not find any in that they had different errors such as not having plt.show().

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from statsmodels.stats.outliers_influence import variance_inflation_factor


data=pd.read_excel("Sto.xlsx") #read data in
data.set_index("Date", inplace= True)
sns.heatmap(data.corr(),  annot=True)

plt.show()

Pandas dataframe

Plot in limbo

Prospero
  • 109
  • 1
  • 11
  • 1
    Don't post data/code as images. Post them directly as text here on SO. – Mr. T Dec 05 '20 at 23:14
  • you can use `df.to_dict()` or `df.head(30).to_dict()` for example. This will make it much easier for us to help debug – anon01 Dec 05 '20 at 23:18
  • if you execute the lines one by one, which one does it hang on? also, how big is your dataset? – anon01 Dec 05 '20 at 23:19
  • @anon01 It hangs on the plt.show() line. The dataframe is (768,6) – Prospero Dec 06 '20 at 00:33
  • are you plotting in a jupyter notebook, from ipython, run from `python myscript.py`, or something else? Can you see other plots? – anon01 Dec 06 '20 at 00:34
  • it sounds like it is plotting, you just can't see it – anon01 Dec 06 '20 at 00:35
  • @anon01 I updated the description with what keeps showing up. This is from the Spyder (ipython) IDE. No I can't see any other plots as this is the only plot I've created here. – Prospero Dec 06 '20 at 00:35
  • @anon01 It just stays blank white. I've never had this issue before. Since no error is coming out of the sns.heatmap() line, does this mean it's an issue with the plotting function somehow? – Prospero Dec 06 '20 at 00:37
  • hard to say, you just gotta debug it. I would start with: 1) try to plot a simple line 2) try to make heatmap with matplotlib, 3) try very simple data – anon01 Dec 06 '20 at 00:40
  • or use plotly: https://plotly.com/python/heatmaps/ :). Seriously the plots and api are better. – anon01 Dec 06 '20 at 00:41
  • 1
    @anon01 Thank you. It's resolved. It was an issue with Spyder. – Prospero Dec 06 '20 at 06:36

1 Answers1

4

This code works completely fine with the below data in a Jupyter Notebook. I imagine the issue is with Spyder and has nothing to do with your data/Seaborn. To get it to work with spyder, you can also ty to add: %matplotlib inline into your code. Another StackOverflow user recently (~5 months ago) had a similar issue with plots not appearing when using Seaborn with Spyder and this solved for them (Seaborn Plot is not displaying):

data = pd.DataFrame({'QQQ': {0: 97.8, 1: 96.4, 2: 95.1, 3: 96.3, 4: 98.2},
     'TSLA': {0: 43.9, 1: 42.0, 2: 42.26, 3: 42.19, 4: 42.12},
     'VIX': {0: 16903, 1: 16821, 2: 18470, 3: 19872, 4: 20120},
     'SPY': {0: 183.5, 1: 180.3, 2: 178.5, 3: 180.7, 4: 183.9},
     'MSFT': {0: 41.5, 1: 41.2, 2: 40.6, 3: 41.1, 4: 42.3}})
data

    QQQ     TSLA    VIX     SPY     MSFT
0   97.8    43.90   16903   183.5   41.5
1   96.4    42.00   16821   180.3   41.2
2   95.1    42.26   18470   178.5   40.6
3   96.3    42.19   19872   180.7   41.1
4   98.2    42.12   20120   183.9   42.3

import seaborn as sns
import matplotlib.pyplot as plt
data = df.corr()
sns.heatmap(data.corr(), annot=True)
plt.show()

enter image description here

David Erickson
  • 16,433
  • 2
  • 19
  • 35