Heatmap not displaying properly

I read that post as well. That’s why I suggested using plt.ylim() because in the end, that was the “best fix” I could see in that post.

1 Like

hi,

by any chance would you know how to get a correlation
between the columns
‘happiness scores’ and ‘freedom’
‘happiness scores’ and ‘generosity’
‘happiness scores’ and ‘trust’
?
I did not want it output as a correlation between
freedom vs trust
freedom vs generosity
generosity vs trust

this is how the correlation table (df.corr()) is output in the code

(upload://eyt2KHzCjNWMnKf6u0OVxdkagSr.png)

```import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

df=combined[['Happiness Score','Freedom','Generosity','Trust (Government Corruption)']]
df.set_index('Happiness Score', inplace=True)

#df.set_index('Fruit', inplace=True)

#sn.heatmap(df.corr(),annot=True,cmap='coolwarm',fmt='.1g',vmin=-1, vmax=1, center= 0,
 #         linewidths=3, linecolor='black',square=True,
  #cbar_kws= {'orientation': 'horizontal'})

#returns the lower triangle of any matrix
#https://heartbeat.fritz.ai/seaborn-heatmaps-13-ways-to-customize-correlation-matrix-visualizations-f1c49c816f07
matrix = np.triu(df.corr())
sns.heatmap(df.corr(), annot=True, mask=matrix)

#mask = np.tril(df.corr())
#sns.heatmap(df.corr(), annot=True, mask=mask)


plt.figure(figsize=(20,20))
plt.ylim(bottom=30, top=200)
plt.show()```

with reference to the happiness score scatterplot correlations in this link

Please do not use screenshots, they are very difficult to view and often do not contain the truly useful information. You can try saving the image of the plot as a *.png file and just upload that. Or if it’s your code you want to show, please place within three backticks ``` so that it is easier to read.

I do not know how to do that (yet!) off the top of my head but I am really good at Google searches! :sunglasses:

hi,

I tried this line
savefig('happi.png', bbox_inches='tight')
from here
but I do not see the png file in jupyter home directory so
I am not sure where to find the image

Have you tried just right-clicking on the image and selecting “save image as”?

1 Like
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

df=combined[['Happiness Score','Freedom','Generosity','Trust (Government Corruption)']]
df.set_index('Happiness Score', inplace=True)

#df.set_index('Fruit', inplace=True)
#sn.heatmap(df.corr(),annot=True,cmap='coolwarm',fmt='.1g',vmin=-1, vmax=1, center= 0,
#         linewidths=3, linecolor='black',square=True,
#cbar_kws= {'orientation': 'horizontal'})
#returns the lower triangle of any matrix
#https://heartbeat.fritz.ai/seaborn-heatmaps-13-ways-to-customize-correlation-matrix-visualizations-f1c49c816f07
matrix = np.triu(df.corr())
sns.heatmap(df.corr(), annot=True, mask=matrix)

plt.figure(figsize=(20,20))
plt.ylim(bottom=30, top=200)
plt.show()```

Very nice! That is much better!

But this is looking much better! Can you place the code that you used to produce it above the image? I still feel like we can use that workaround that involves adjusting the ylim values rather than rolling back versions…

happi

I had to to do separately as the three backticks would not display image .
It was treated as code even though the image text came after the last three backticks

or i can try here

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

df=combined[['Happiness Score','Freedom','Generosity','Trust (Government Corruption)']]
df.set_index('Happiness Score', inplace=True)

#df.set_index('Fruit', inplace=True)
#sn.heatmap(df.corr(),annot=True,cmap='coolwarm',fmt='.1g',vmin=-1, vmax=1, center= 0,
#         linewidths=3, linecolor='black',square=True,
#cbar_kws= {'orientation': 'horizontal'})
#returns the lower triangle of any matrix
#https://heartbeat.fritz.ai/seaborn-heatmaps-13-ways-to-customize-correlation-matrix-visualizations-f1c49c816f07
matrix = np.triu(df.corr())
sns.heatmap(df.corr(), annot=True, mask=matrix)

plt.figure(figsize=(20,20))
plt.ylim(bottom=30, top=200)
plt.show()

I do not see any code…just the pretty plot and an empty one…

1 Like

I have been googling that all day - about 6 hours roughly

No worries friend…as I said, I’m good at Googling…I ask A LOT of questions because I just HAVE to know :sunglasses:

1 Like

code comes after this reply since I did needed to input in separate boxes

Oh no! We are back to using screenshots again! That said, this is a great example of why we never use screenshots in the community forums…all I want to do is scroll down in that image! I can see it’s a screen shot of your code but it’s cut off. Another reason why screenshots are a big NONO here is because we can’t select and copy/paste your code when it’s tied up in an image.

1 Like

hmm sorry about that it was not meant to display the code ,
just to point to location of it since you mentioned you did not see any code.

But, I had fixed it now to all be in one box.

for some reason if the image link cam after the backticks then image link was treated as text

you can see where the code is now?

I don’t think I’ve used these commands yet…I just found them while searching to solve your problem. Have you tried without keyword arguments? In specific words:

plt.ylim(30,200)

You may want to drop that 30 down to 20 or 10 depending on the data.

1 Like

I just scrolled to the top of this thread and read your original post to see the code again.

Have you tried various ranges for the plt.ylim() parameter?

I’ve also noticed that with matplotlib, sometimes the order you call your commands can make a difference (I don’t know why that would be) but it’s worth playing around to see what you see.

Try putting your plt.figure() statement before generating the heatmap. Same goes for plt.ylim()

1 Like

Hi,

I just tried , but they is no difference in output

Hey many thanks again.
Putting it at the top solved the problem, for the figsize

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

plt.figure(figsize=(20,20))
plt.ylim(10,20)

df=combined[['Happiness Score','Freedom','Generosity','Trust (Government Corruption)']]
df.set_index('Happiness Score', inplace=True)

#df.set_index('Fruit', inplace=True)
#sn.heatmap(df.corr(),annot=True,cmap='coolwarm',fmt='.1g',vmin=-1, vmax=1, center= 0,
#         linewidths=3, linecolor='black',square=True,
#cbar_kws= {'orientation': 'horizontal'})
#returns the lower triangle of any matrix
#https://heartbeat.fritz.ai/seaborn-heatmaps-13-ways-to-customize-correlation-matrix-visualizations-f1c49c816f07
matrix = np.triu(df.corr())
sns.heatmap(df.corr(), annot=True, mask=matrix)


plt.show()```

NICELY DONE! :sunglasses: Congrats!

Kindly mark this as done (although I don’t think either of us knows why exactly! :laughing:)

In case you’re curious, I saw this “order of operations” behaviour when I did one of the first missions in Exploratory Data Visualization and I even posted about it in the forums but never really figured out why the order would matter. Best I could figure out is that default settings/objects/widgets/gremlins were being over written between/before/after fig instantiation and the plot being generated.

In any event, are you good now? Plot looks good? Can you post it for me?

Hi there

I have marked it as solution/done 3 boxes above from here,
but please confirm if cannot find.

Yes what you said makes sense in retrospect that the fig should come first
as I suppose the plot is embedded in the fig.

I still have problem with numbers cut in half at bottom although figsize part is fixed, but it is good enough.