简体   繁体   English

Pyplot 在热图上 Y 轴的指定值上绘制水平线

[英]Pyplot plot horizontal line on specified value of Y axis on Heatmap

I currently have a dataframe, df:我目前有一个数据框,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:我已经尝试用 y 轴上的值为 2 的水平线绘制热图,但是它改为在热图的第二条上绘制线:

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):我希望能够在 y 轴上的指定值处绘制一条水平线,以便输入“2”将给出以下图表(这是通过替换"ax.axhline(3.5, ls='--')"在我当前的代码中):

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.因此,在坐标“2”处绘制一条线会将这条线放在第二行像素的上方。 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='--')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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