Home > OS >  Pyplot plot horizontal line on specified value of Y axis on Heatmap
Pyplot plot horizontal line on specified value of Y axis on Heatmap

Time:06-25

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

CodePudding user response:

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, e.g. using

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