Heatmap not displaying properly

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'})

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.show()

This code creates a heatmap but the numbers at bottom of the square are cut
in half please advise how to display them?

Also this line does not seem to work
because when I change dimensions of length and width nothing changes
in size.
plt.figure(figsize=(20,20))

https://heartbeat.fritz.ai/seaborn-heatmaps-13-ways-to-customize-correlation-matrix-visualizations-f1c49c816f07

Have you tried adjusting the y-axis with:

plt.ylim(bottom, top)
1 Like

I just tried your code and it works fine on my end…sounds like something is messed up in your environment. Version conflicts maybe? Update/revert to previous versions? Dunno…but your code is good!
heatmap

2 Likes

Hi ,
thanks for your response.
Originally seaborn was not working until I downgraded
so yes it was something in environment and
so it could be some conflict.

someone else had problem fix was this
This was a matplotlib regression introduced in 3.1.1 which has been fixed in 3.1.2 (still forthcoming). For now the fix is to downgrade matplotlib to a prior version.

1 Like

hi

that does not work on my end; it made another plot below

plt.ylim(bottom=30, top=200)

Screenshots do not work so well for seeing the entire plot…can you try “save image as” for your plot and upload that instead?

1 Like

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?