2017-04-22 146 views
0

我使用(matplotlib.pyplot作爲PLT)matplotlib繪製柱狀圖。在該條形圖上,我用灰色的axhline()函數繪製了一條水平線。我希望水平線開始處的點(y軸上的值= 42000)也應該顯示值,即42000。該怎麼辦?顯示y軸的值的水平線繪製在條形圖

這是我當前的圖像:

enter image description here

在下面的圖片,看到 '39541.52' 點?我想顯示完全一樣,我的形象,我的點值上是「42000」

enter image description here

+0

我想我已經提供了[這個答案](http://stackoverflow.com/questions/42876219/point-picker-event-處理器繪製線和顯示座標matplotlib)和[這個答案](http://stackoverflow.com/questions/43133017/how-to-change-colors-automatically-once-a-parameter-被改變)在完全相同的問題表上。如果您包含您的代碼並確切地說明問題所在,我們一定會幫助您。 – ImportanceOfBeingErnest

回答

2

標籤可以創建例如使用ax.text()。要定位標籤,一個很好的竅門是使用允許使用x座標的座標和y位置的數據座標的變換。

ax.text(1.02, 4.2e4, "42000", .. , transform=ax.get_yaxis_transform()) 

完整代碼:

import matplotlib.pyplot as plt 
import matplotlib.colors 
import numpy as np 

fig = plt.figure() 
ax = fig.add_subplot(111) 
x = [0,1,2,3] 
y = np.array([34,40,38,50])*1e3 
norm = matplotlib.colors.Normalize(30e3, 60e3) 
ax.bar(x,y, color=plt.cm.plasma_r(norm(y))) 
ax.axhline(4.2e4, color="gray") 
ax.text(1.02, 4.2e4, "42000", va='center', ha="left", bbox=dict(facecolor="w",alpha=0.5), 
     transform=ax.get_yaxis_transform()) 
plt.show() 

enter image description here