假设我们有一个包含价格和数量的 DataFrame(想想金融)。
用该价格点的数量标记每个价格点的最佳方式是什么?
Price Volume
2013-04-10 04:46 1300 19
2013-04-10 04:47 1305 20
2013-04-10 04:48 1302 6
2013-04-10 04:49 1301 10
这是一种可能的实现
我已导入以下内容:
import pandas as pd
import datetime as dt
import matplotlib.pyplot as plt
现在我们可以重新创建数据
ind = pd.date_range(start=dt.datetime(2013, 4, 10, 4, 46),
periods=4, freq='Min')
data = pd.DataFrame([[1200, 19], [1302, 20], [1302, 6], [1301, 10]],
index=ind, columns=['Price', 'Volume'])
现在我将定义 annotate_plot 函数。文档字符串应该有足够的信息来弄清楚它在做什么。
def annotate_plot(frame, plot_col, label_col, **kwargs):
"""
Annotate the plot of a given DataFrame using one of its columns
Should be called right after a DataFrame or series plot method,
before telling matplotlib to show the plot.
Parameters
----------
frame : pandas.DataFrame
plot_col : str
The string identifying the column of frame that was plotted
label_col : str
The string identifying the column of frame to be used as label
kwargs:
Other key-word args that should be passed to plt.annotate
Returns
-------
None
Notes
-----
After calling this function you should call plt.show() to get the
results. This function only adds the annotations, it doesn't show
them.
"""
import matplotlib.pyplot as plt # Make sure we have pyplot as plt
for label, x, y in zip(frame[label_col], frame.index, frame[plot_col]):
plt.annotate(label, xy=(x, y), **kwargs)
这个函数现在可以用来做一个带有标签的基本图
data.Price.plot(marker='*')
annotate_plot(data, 'Price', 'Volume')
plt.show()
您还可以通过 annotate_plot 函数直接传递给 plt.annotate() 的任意参数。请注意,这些论点中的大多数都取自这个答案。
bbox = dict(boxstyle='round,pad=0.5', fc='green', alpha=0.3)
ha = 'right'
va = 'bottom'
arrowprops = dict(arrowstyle='->', connectionstyle='arc3,rad=0')
xytext = (-20, 20)
textcoords = 'offset points'
data.Price.plot(marker='*')
annotate_plot(data, 'Price', 'Volume', bbox=bbox, ha=ha, va=va,
xytext=xytext, textcoords=textcoords)
plt.show()