简体   繁体   中英

Pyplot plot horizontal line on specified value of Y axis on Heatmap

I currently have a dataframe, df:

In  [1]: df
Out [1]:    

   one        two
   0.5        4.62
   1.0       12.23        
   1.5       17.53          
   2.0       20.32        
   2.5       19.37
   3.0       17.77
   3.5        9.04

I have tried this to plot a heatmap with a horizontal line at the value 2 on the y axis however it instead plots the line at the 2nd bar of the heatmap:

In  [2]: plt.figure(figsize=(30,7))
         plt.title('Test')
         ax = sns.heatmap(data=df, annot=True, )
         plt.xlabel('Test')
         ax.invert_yaxis()
         ax.axhline(2, ls='--')

Actual Output

I am looking to be able to draw a horizontal line at specified values on the y-axis so that an entry of "2" will give the following chart (This was produced for demonstrating by substituting "ax.axhline(3.5, ls='--')" in my current code):

Desired Output

It looks like the plot is just an image. Hence, plotting a line at coordinate '2' places the line above the second row of pixels. To plot the line at a desired value, you can search for the appropriate row of pixels, eg using

value = 2
index = np.abs(df['one'] - value).argmin()
ax.axhline(index + .5, ls='--')

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