4

I was wondering if there is any options to make following picture which are outputs of sns.heatmap(df) subplots smoothy: img I've just found one relevant answer here which is suggested by using zsmooth:

data = [go.Heatmap(z=[[1, 20, 30],
                      [20, 1, 60],
                      [30, 60, 1]],
                   zsmooth = 'best')]
iplot(data)

The snippet of my codes which I used seaborn are following:

#plotting all columns ['A','B','C'] in-one-window side by side
fig, axes = plt.subplots(nrows=1, ncols=3 , figsize=(20,10))

plt.subplot(131)
sns.heatmap(df1, vmin=-1, vmax=1, cmap ="coolwarm", linewidths=.75 , linecolor='black', cbar=True , cbar_kws={"ticks":[-1.0,-0.75,-0.5,-0.25,0.00,0.25,0.5,0.75,1.0]})
fig.axes[-1].set_ylabel('[MPa]', size=20) #cbar_kws={'label': 'Celsius'}
plt.title('A', fontsize=12, color='black', loc='center', style='italic')
plt.axis('off')

plt.subplot(132)
sns.heatmap(df2, vmin=-1, vmax=1, cmap ="coolwarm", linewidths=.75 , linecolor='black', cbar=True , cbar_kws={"ticks":[-1.0,-0.75,-0.5,-0.25,0.00,0.25,0.5,0.75,1.0]})
fig.axes[-1].set_ylabel('[Mpa]', size=20) #cbar_kws={'label': 'Celsius'}
plt.title('B', fontsize=12, color='black', loc='center', style='italic')
plt.axis('off')

plt.subplot(133)
sns.heatmap(df3, vmin=-40, vmax=150, cmap ="coolwarm" , linewidths=.75 , linecolor='black', cbar=True , cbar_kws={"ticks":[-40,150,-20,0,25,50,75,100,125]}) 
plt.title('C', fontsize=12, color='black', loc='center', style='italic')
plt.axis('off')


plt.suptitle(f'Analysis of data in cycle Nr.: {count}', color='yellow', backgroundcolor='black', fontsize=48, fontweight='bold')
plt.subplots_adjust(top=0.7, bottom=0.3, left=0.05, right=0.95, hspace=0.2, wspace=0.2)
plt.savefig(f'{i}/{i}{i}{count}.png') 
plt.show()

Problem is I'm not sure if can i use it since it calls following library while mine is another. it would be great if someone explain me if it's possible and how can I implement it on my snippet?

from plotly.offline import download_plotlyjs, init_notebook_mode, plot
import plotly.graph_objs as go
Mario
  • 1,631
  • 2
  • 21
  • 51

1 Answers1

5

The question you linked uses plotly. If you don't want to use that and want to simply smooth the way your data looks, I suggest just using a gaussian filter using scipy.

At the top, import

from scipy.ndimage.filters import gaussian_filter

Then use it like this:

df3_smooth = gaussian_filter(df3, sigma=1)
sns.heatmap(df3_smooth, vmin=-40, vmax=150, cmap ="coolwarm" , cbar=True , cbar_kws={"ticks":[-40,150,-20,0,25,50,75,100,125]}) 

You can change the amount of smoothing, using e.g. sigma=3, or any other number that gives you the amount of smoothing you want.

Keep in mind that that will also "smooth out" any maximum data peaks you have, so your minimum and maximum data will not be the same that you specified in your normalization anymore. To still get good looking heatmaps I would suggest not using fixed values for your vmin and vmax, but:

sns.heatmap(df3_smooth, vmin=np.min(df3_smooth), vmax=np.max(df3_smooth), cmap ="coolwarm" , cbar=True , cbar_kws={"ticks":[-40,150,-20,0,25,50,75,100,125]}) 
Freya W
  • 487
  • 3
  • 11
  • 2nd option you mentioned worked nicely and I amazed by your clear answers however I noticed that It's better to use `sigma=1` since in higher sigmas `sigma=2` and `sigma=3` I lose the details ! Another issue is I shared as a question [here](https://stackoverflow.com/questions/54330676/normalization-by-using-2-times-gaussian-function-on-negative-and-positive-number) still after applying **super normalization** still when I check my normalized data in dataset which has about 4500 cycles still there some data out of normalization and I wanna include them to endpoints in intervals. Any ideas ? – Mario Jan 26 '19 at 19:15
  • You know considering I limited te data to confidence intervals(CI) during Gaussian normalization still I've face to leakage of some data out of desired scale but it seems I need to rescale them due to bad distribution and accumulation of data. Is there any elegant way to accomplish that? or I should use `df['A'] = scaler.fit_transform(data_to_use1.reshape(-1, 1))` and `df['C'] = scaler.fit_transform(data_to_use1.reshape(-40, 150))` by using `from sklearn import preprocessing` ? If so, where should I apply in in my scripts exactly to fix normalization of this huge dataset? – Mario Jan 26 '19 at 19:27
  • @Mario, you are getting data outside of the normalization for ‚A‘ / df1, because on the third loop through you are setting your minimum and maximum values for normalization of df1 by using df2 / ‚B‘. – Freya W Jan 27 '19 at 20:10
  • I think it's not true if you check the scripts under my [question](https://stackoverflow.com/questions/54330676/normalization-by-using-2-times-gaussian-function-on-negative-and-positive-number) maybe we can discuss clearly. I believe that in 3rd loop each `df1`, `df2`, `df3` normalized by their own max and min which are set base on their Mean value of positive and negative numbers of each `df` after individual **Gaussian normalization** on positive and negative values. or may I didn't get your point of view. so plz mention under question which line makes this normalization out of control? – Mario Jan 27 '19 at 22:29
  • @Mario, that was meant for the code in [this question](https://stackoverflow.com/q/54282812/10944175), where you set `min_val` and `max_val ` in your `for i in df:` loop. I haven't taken a look at the code in your other question. I'm not sure whether the scope of your questions is maybe a bit too broad for stackoverflow. Do you have any colleagues or an advisor that could help you get a bit more structured when writing code? – Freya W Jan 28 '19 at 09:09
  • you might right since I use in [that question](https://stackoverflow.com/questions/54282812/how-can-make-subplots-of-columns-in-pandas-dataframe-in-one-window-inside-of-for) `min_val = df[i].min()` & `max_val = df[i].max()` in that structure but in programming structure of [this question](https://stackoverflow.com/questions/54330676/normalization-by-using-2-times-gaussian-function-on-negative-and-positive-number) I used different independent values come from mean of production of Gaussian normalization on positive and negative values. I appreciate that you noticed the details. – Mario Jan 28 '19 at 19:32
  • I don't have any advisor unfortunately my only hope is SO and people like you. feel free to check out these questions : [Normalizing](https://stackoverflow.com/questions/54330676/normalization-by-using-2-times-gaussian-function-on-negative-and-positive-number) & [frequency of missing-data occurrence](https://stackoverflow.com/questions/54394457/visualisation-of-missing-data-occurrence-frequency-by-using-seaborn) to help me out. – Mario Jan 28 '19 at 19:36
  • @Mario, with the extent of these question it really feels like you should begin by taking a couple of online coding courses or look into how to debug in python, as they are basically asking someone else to do the work (debug) for you. If you have tried and tried and tried, and have identified your core problem, it should be possible to ask a question that uses at most 20 lines of code. If it's just not working the way you expect, you will need to troubleshoot some more: [This question](https://softwareengineering.stackexchange.com/q/11726) has some helpful answers that can get you started. – Freya W Jan 29 '19 at 10:02
  • Thanks a lot for tips for sure I'll try to improve my debugging skill however you might look at least to [this question](https://stackoverflow.com/questions/54394457/visualisation-of-missing-data-occurrence-frequency-by-using-seaborn) which needs to suggest a good idea to implement it . You may figure it out how can be implemented. – Mario Jan 29 '19 at 14:05
  • @Mario, ok, I'll take a look at that question today when I get around to it or else on Friday. If you liked my answers so far, you could also upvote them on top of accepting them. Thank you! – Freya W Jan 30 '19 at 10:08
  • Hi dear may I ask you to have a look to this [question](https://stackoverflow.com/questions/57233218/how-can-color-up-or-highlighted-outliers-in-scatter-plot-by-using-customized-ran) urgently. – Mario Jul 28 '19 at 13:17