只需插上我自己的項目,看看mpldatacursor
:https://github.com/joferkington/mpldatacursor
作爲一個基本的例子,只是打電話datacursor(hover=True, point_labels=df['E'])
將讓你90%的方式,你想要什麼。例如,把你的代碼上面的代碼片段:
from StringIO import StringIO
import pandas as pd
import matplotlib.pyplot as plt
from mpldatacursor import datacursor
f = StringIO(
"""931,Oxfordshire,9314125,123255,Larkmead School,Abingdon,125,124,20,SUPP,8
931,Oxfordshire,9314126,123256,John Mason School,Abingdon,164,164,25,6,16
931,Oxfordshire,9314127,123257,Fitzharrys School,Abingdon,150,149,9,0,11
931,Oxfordshire,9316076,123298,Our Lady's Abingdon,Abingdon,57,57,SUPP,SUPP,16
""")
df = pd.read_csv(f, names=['A','B','C','D','E','F','G', 'H','I','J', 'K'],
header=None)
df.replace('SUPP', 3.0, inplace=True)
df = df.convert_objects(convert_numeric=True)
df['KG'] = df['K']*1.0/df['G']
plt.plot(df['KG'], marker='o')
datacursor(hover=True, point_labels=df['E'])
plt.show()
我們會得到每當線懸停在彈出的標籤。
但是,通過設計,默認行爲是在線路懸停/點擊時顯示彈出窗口。因此,使用point_labels
選項時,結果可能不完全是你腦子裏想的是什麼:
如果你只希望在頂點盤旋在要顯示的彈出窗口中,你可以使用類似這樣的一種解決方法:(將有隻顯示彈出在下一版本的頂點選擇,所以這種解決方法不會在將來需要)
from StringIO import StringIO
import pandas as pd
import matplotlib.pyplot as plt
from mpldatacursor import datacursor
f = StringIO(
"""931,Oxfordshire,9314125,123255,Larkmead School,Abingdon,125,124,20,SUPP,8
931,Oxfordshire,9314126,123256,John Mason School,Abingdon,164,164,25,6,16
931,Oxfordshire,9314127,123257,Fitzharrys School,Abingdon,150,149,9,0,11
931,Oxfordshire,9316076,123298,Our Lady's Abingdon,Abingdon,57,57,SUPP,SUPP,16
""")
df = pd.read_csv(f, names=['A','B','C','D','E','F','G', 'H','I','J', 'K'],
header=None)
df.replace('SUPP', 3.0, inplace=True)
df = df.convert_objects(convert_numeric=True)
df['KG'] = df['K']*1.0/df['G']
plt.plot(df['KG'], marker='o')
l, = plt.plot(df['KG'], marker='o', linestyle='', visible=False)
datacursor(l, hover=True, point_labels=df['E'])
plt.show()
此外,您可能只想顯示有問題的學校,而不是x,y座標等。要更改此設置,請使用自定義formatter
功能:
datacursor(l, hover=True, point_labels=df['E'],
formatter=lambda **kwargs: kwargs['point_label'][0])
最後,你可能想要一個白色的盒子票友箭頭和不同的相對位置:
datacursor(l, hover=True, point_labels=df['E'], bbox=dict(fc='white'),
formatter=lambda **kwargs: kwargs['point_label'][0], xytext=(0, 25),
arrowprops=dict(arrowstyle='simple', fc='white', alpha=0.5))
只是把它在最後一個例子中全部集成到可運行版本中:
from StringIO import StringIO
import pandas as pd
import matplotlib.pyplot as plt
from mpldatacursor import datacursor
f = StringIO(
"""931,Oxfordshire,9314125,123255,Larkmead School,Abingdon,125,124,20,SUPP,8
931,Oxfordshire,9314126,123256,John Mason School,Abingdon,164,164,25,6,16
931,Oxfordshire,9314127,123257,Fitzharrys School,Abingdon,150,149,9,0,11
931,Oxfordshire,9316076,123298,Our Lady's Abingdon,Abingdon,57,57,SUPP,SUPP,16
""")
df = pd.read_csv(f, names=['A','B','C','D','E','F','G', 'H','I','J', 'K'],
header=None)
df.replace('SUPP', 3.0, inplace=True)
df = df.convert_objects(convert_numeric=True)
df['KG'] = df['K']*1.0/df['G']
plt.plot(df['KG'], marker='o')
l, = plt.plot(df['KG'], marker='o', linestyle='', visible=False)
datacursor(l, hover=True, point_labels=df['E'], bbox=dict(fc='white'),
formatter=lambda **kwargs: kwargs['point_label'][0], xytext=(0, 25))
plt.show()
看看https://github.com/jakevdp/mpld3 – TomAugspurger
只需插入我自己的項目,看看'mpldatacursor'。在你的情況下,你會想使用'hover = True'和'point_labels'選項。看看這裏的例子:https://github.com/joferkington/mpldatacursor/blob/master/examples/labeled_points_example.py如果我有機會,我會根據你的例子發佈一個更詳細的例子。 –