简体   繁体   中英

Set the y-axis to scale in a Seaborn heat map

I currently have a dataframe, df:

In  [1]: df
Out [1]:

   one        two
      1.5    11.22
        2    15.36
      2.5    11
      3.3    12.5
      3.5    14.78
        5    9
      6.2    26.14

I used this code to get a heat map:

In [2]:

plt.figure(figsize=(30, 7))
plt.title('Test')
ax = sns.heatmap(data=df, annot=True,)
plt.xlabel('Test')
ax.invert_yaxis()

value = 6
index = np.abs(df.index - value).argmin()
ax.axhline(index + .5, ls='--')
print(index)

Out [2]:

Out [2] Result

I am looking for the y-axis, instead, to automatically scale and plot the df[2] values in their respective positions on the full axis. For example, there should be a clear empty space between 3.5 and 5.0 as there aren't any values - I want the values in between on the y-axis with 0 value against them.

This can be easily achieved with a bar plot instead:

plt.bar(df['one'], df['two'], color=list('rgb'), width=0.2, alpha=0.4)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM