0

i'm going to trying to do a heatmap from a file that has 3 columns, The format of my data is:

1 3 65.0987

2 9 49.34

5 7 0.00056

. . .

. . .

3 1 65.0987 etc..

but I've separated it in 3 files. So I'm using the next code:

from numpy import *
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np

with open("1.txt") as f:
            lstx = [int(x) for x in f.read().split()]

with open("2.txt") as f:
            lsty = [int(x) for x in f.read().split()]

with open("3.txt") as f:
            lstz = [float(x) for x in f.read().split()]

x=np.array(lstx)
y=np.array(lsty)
z=np.array(lstz)

df = pd.DataFrame.from_dict(np.array([y,x,z]).T)
df.columns = ['X_value','Y_value','Z_value']
df['Z_value'] = pd.to_numeric(df['Z_value'])

pivotted= df.pivot('Y_value','X_value','Z_value')

sns.heatmap(pivotted,cmap='Paired')
plt.show()

It works, and have this heatmap:

enter image description here

I want to change the format of y axis, I mean, that the number 45 is up, and 1 is low. And want too, the both axes format are without point and decimals, i mean, the numbers are 1, 3, 45, etc., instead of 1.0, 3.0, etc.

I've tried with ylim, xlim, and xticks, yticks, but doesn't work good, the out of figure is distorsionated.

Can you help me please? Have you some idea what i must to change or add to my code? Thanks for your support.

  • To change the direction of the axes one can [invert the axis](https://stackoverflow.com/questions/2051744/reverse-y-axis-in-pyplot). To change the format on the axis, you may use a [formatter](https://stackoverflow.com/questions/29188757/matplotlib-specify-format-of-floats-for-tick-lables). – ImportanceOfBeingErnest Jan 13 '18 at 14:55
  • Thanks so much, I've resolve this problem, using this code ax.xaxis.set_major_formatter(FormatStrFormatter('%0.0f') now i have the format of axis as: 0,2,4,...44,, and i want to be between 1 and 45. Using plt.ylim and xlim(), i can' t fix it. Have you some idea what i can to change in my code? – Théré Hernandez Jan 13 '18 at 15:48
  • Thank you, I did it with the function MaxNLocator. :) Best regards – Théré Hernandez Jan 13 '18 at 16:04
  • Oh yes, I forgot that seaborn does strange things to the axes. In that case it's probably best to use fixed formatters. `ax.set_xticks(range(0,46,2))` and `ax.set_xticklabels(range(1,47,2))`. – ImportanceOfBeingErnest Jan 13 '18 at 16:05
  • Thanks so much! It's better your option than mine. Best regards :) – Théré Hernandez Jan 13 '18 at 16:24

1 Answers1

0

Well, I put on reverse y-axis with the function plt.gca().invert_yaxis(), put both axes with integer numbers with the function ax.xaxis.set_major_formatter(FormatStrFormatter('%0.0f')) and finally, I fixed the ticks with the function MaxNLocator.

Thanks so much

Best regards