Home > front end >  Plot resize and saving empty
Plot resize and saving empty

Time:12-22

I'm trying to plot a heatmap overlap an image, using seaborn and matplotlib as shown in the code below:

import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.image as mpimg 

df = pd.read_csv('/home/diegonachon/eye-tracker/eyedata/csv_heatmap/cl/Suj5_Control_HO_1ph.csv')
df['xpl'] = pd.to_numeric(df['xpl'], errors = 'coerce')
df['ypl'] = pd.to_numeric(df['ypl'], errors = 'coerce')
df = df[(df['event'] == 'FIX') & (~(df['xpl'].isnull()))]
hmax = sns.kdeplot(x = df.xpl, y = df.ypl, cmap="Reds", shade=True, bw_adjust= 0.6, clip = ((0,800), (0,600)), 
                   alpha = 0.6, antialiased = True)
hmax.collections[0].set_alpha(0)

map_img = mpimg.imread('/home/diegonachon/eye-tracker/eyedata/asd.jpg') 

plt.imshow(map_img, zorder=0, extent=[0, 800, 0, 600])

# image = plt.imshow(map_img, zorder=0, extent=[0, 800, 0, 600], aspect = 250)
# figure(figsize=(8, 6), dpi=80)
# plt.imsave('test.png', image)
# plt.savefig('fig8_6_80dpi.png')   

plt.show()

Result: https://imgur.com/a/4rdgCOH

  1. The first thing is that I can't resize it, I've tried changing 'aspect' and 'figsize', but nothing happen (as the code shown on comment)

  2. When I tried to save the plot generate, is either and empty file (savefig) or an error (uncommenting image = plt.imswho()....) - AttributeError: 'tuple' object has no attribute 'shape')

Thanks!

CodePudding user response:

Here are the changes I have made to your code. As I don't have access to your data, so I have used random numbers instead but this will give you the idea. Refer to the UPPERCASE comments for the answers.

import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.image as mpimg 

# CREATED FOR TEST
x = np.random.rand(100,)
y = np.random.rand(100,)

fig, ax = plt.subplots(figsize=(10,10)) # YOU CAN MODIFY FIGSIZE FROM HERE
ax = sns.kdeplot(x = x, y = y, cmap="Reds", shade=True, bw_adjust= 0.6, 
clip = ((0,800), (0,600)), 
               alpha = 0.6, antialiased = True)
ax.collections[0].set_alpha(0)

# map_img = mpimg.imread('/home/diegonachon/eye-tracker/eyedata/asd.jpg') 

#plt.imshow(map_img, zorder=0, extent=[0, 800, 0, 600])

# image = plt.imshow(map_img, zorder=0, extent=[0, 800, 0, 600], 
#aspect = 250)
# figure(figsize=(8, 6), dpi=80)
# plt.imsave('test.png', image)
# plt.savefig('fig8_6_80dpi.png')  

# THIS WILL SAVE YOUR IMAGE AS A PNG FILE
plt.savefig('test.png') 
plt.show()
  • Related