Python Forum
How to increase the size of a png picture for the heatmap of the correlation?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to increase the size of a png picture for the heatmap of the correlation?
#1
Good evening to the community Smile
I am working with the file "Santé3".
I would like to create and download a png picture with a good definition for my heatmap of correlation where we can see the entire heatmap of the correlation.
I tried some codes but I have still words which are not showed.
from pandas import read_csv
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import numpy.ma as ma
import pandas as pd

dataset = pd.read_csv('Santé3.csv', sep= ';', encoding='latin-1', index_col=0)
dataset.drop(['Dynaverage'], axis=1, inplace=True)
corr = dataset.corr()
plt.figure(figsize=(200,30))
plt.subplots(figsize=(15,8))
sns.heatmap(corr, cmap='coolwarm', vmin=-1, vmax=1, annot=True, mask=mask)
plt.savefig("correlation.png")
Thank you for your answer.

Attached Files

Thumbnail(s)
   

.csv   Santé3.csv (Size: 5 KB / Downloads: 207)
Reply
#2
Have you tried setting dpi in the savefig call or saving to a svg file?
Reply
#3
Dear deanhystad,
I think dpi is related to the quality of the image, not the size.

from pandas import read_csv
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import numpy.ma as ma
import pandas as pd
 
dataset = pd.read_csv('Santé3.csv', sep= ';', encoding='latin-1', index_col=0)
dataset.drop(['Dynaverage'], axis=1, inplace=True)
corr = dataset.corr()
plt.figure(figsize=(200,30))
plt.subplots(figsize=(15,8))
sns.heatmap(corr, cmap='coolwarm', vmin=-1, vmax=1, annot=True, mask=mask)
plt.savefig("correlation.png", dpi=400)
Reply
#4
Did you try it? If I capture a 3x3 plot at 100dpy the image is 300x300 pixels. At the 1000dpi the same image is 3000x3000 pixels
Reply
#5
Dear deanhystad,
it's not working with svg file.

from pandas import read_csv
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import numpy.ma as ma
import pandas as pd
 
dataset = pd.read_csv('Santé3.csv', sep= ';', encoding='latin-1', index_col=0)
dataset.drop(['Dynaverage'], axis=1, inplace=True)
corr = dataset.corr()
plt.figure(figsize=(200,30))
plt.subplots(figsize=(15,8))
sns.heatmap(corr, cmap='coolwarm', vmin=-1, vmax=1, annot=True, mask=mask)
plt.savefig("correlation.svg", dpi=400)
Reply
#6
(Oct-05-2021, 07:06 PM)deanhystad Wrote: Did you try it? If I capture a 3x3 plot at 100dpy the image is 300x300 pixels. At the 1000dpi the same image is 3000x3000 pixels

With a DPI of 1000, you can see that it doesn't change. I can't see the labels.

from pandas import read_csv
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import numpy.ma as ma
import pandas as pd
 
dataset = pd.read_csv('Santé3.csv', sep= ';', encoding='latin-1', index_col=0)
dataset.drop(['Dynaverage'], axis=1, inplace=True)
corr = dataset.corr()
plt.figure(figsize=(200,30))
plt.subplots(figsize=(15,8))
sns.heatmap(corr, cmap='coolwarm', vmin=-1, vmax=1, annot=True, mask=mask)
plt.savefig("correlation.png", dpi=1000)

Attached Files

Thumbnail(s)
   
Reply
#7
It works as I mentioned. When capturing a pyplot plot (I don't have seaborn) my 100dpi plot was 300x300 pixels and the 1000dpi plot was 3000x3000 pixels when opened in Paint. This must be a seaborn thing.

I found this when looking for resizing a heatmap. Looks like the kitchen sink approach.

https://stackoverflow.com/questions/4333...ng-seaborn
plt.figure(figsize=(6, 6),  dpi = 600)   #  I would go higher than this
sns.heatmap(corr, cmap="Blues", annot=True, annot_kws={"size": 8})  #Make labels and annotation bigger too
plt.tick_params(axis = 'x', labelsize = 12) # x font label size
plt.tick_params(axis = 'y', labelsize = 12) # y font label size
lulu43366 likes this post
Reply
#8
Thank you so much deanhystad. I can see the labels with a good definition with this code.

from pandas import read_csv
import matplotlib.pyplot as plt
import numpy as np
import numpy.ma as ma
import pandas as pd

 
dataset = pd.read_csv('Santé3.csv', sep= ';', encoding='latin-1', index_col=0)
dataset.drop(['Dynaverage'], axis=1, inplace=True)
corr = dataset.corr()
plt.figure(figsize=(6,6), dpi=1000)
plt.subplots(figsize=(15,8))
sns.heatmap(corr, cmap='coolwarm', vmin=-1, vmax=1, annot=True, mask=mask, annot_kws={"size":8})
plt.tick_params(axis = 'y', labelsize = 5)
plt.tick_params(axis = 'x', labelsize = 5)
plt.savefig("correlation.png", dpi=2000)
Reply
#9
I bet it is the dpi setting in plt.figure(). You have to set the dpi before making the heatmap so seaborn can use the higher dpi setting.

Why are you calling plt.figure and plt.subplots? When I run your code it shows two plot windows; an empty 6x6 window and a 15x8 window that contains a plot (mine is a line plot). You can remove either and you will still get a png image file.

From what you've said in earlier posts I don't think the dpi setting in plt.savefig() is doing anything. Seaborne must be drawing a raster image, and changing the dpi is not going to increase the resolution of the png file. Setting the dpi in plt.savefig worked for me because the plot I was saving was vector graphics, and vector graphics rescale very well.
Reply
#10
A few questions.

In post #5 you say "it's not working with svg file." Does the program crash? Doesn't it generate the file? Can't you use the file? How is it "not working"?

In post #6 you post an image and say "I can't see the labels". Which labels? When I look at the attached image I can see axis labels and annotations. And the labels and annotations look good. Where are there supposed to be labels that I cannot see?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am getting a valueError. And not sure why? My goal is to visualize the correlation ReadytoCode 0 416 Dec-11-2023, 05:33 AM
Last Post: ReadytoCode
  openpyxl insert picture problem cools0607 2 1,399 May-03-2023, 06:48 AM
Last Post: cools0607
  How to plot seaborn heatmap on top of a background circle SriRajesh 0 1,368 Jul-09-2022, 04:00 AM
Last Post: SriRajesh
  Error in find pearson correlation function erneelgupta 1 1,810 Mar-01-2022, 03:41 PM
Last Post: stevendaprano
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,290 Jan-21-2022, 06:24 PM
Last Post: mcva
  How to remove a column or two columns in a correlation heatmap? lulu43366 3 5,081 Sep-30-2021, 03:47 PM
Last Post: lulu43366
  Using SoX in Python to Increase mp3 Bitrate DRT 1 1,715 Jul-10-2021, 08:41 PM
Last Post: DRT
  Clicker count increase by 1 each time blakefindlay 1 5,530 Feb-03-2021, 03:50 PM
Last Post: deanhystad
  Picture changing triggered by GPIO q_nerk 2 2,521 Dec-14-2020, 03:32 PM
Last Post: DeaD_EyE
  increase and decrease a slice value? KEYS 2 2,055 Nov-10-2020, 11:35 PM
Last Post: KEYS

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020