假设我们有一个价格和数量的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
答案 0 :(得分:2)
这是一个可能的实现
我导入了以下内容:
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()
您还可以通过直接转到plt.annotate()的annotate_plot函数传递任意参数。请注意,大多数这些参数都来自this answer。
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()