pandas 如何在熊猫数据系列上绘制任意标记?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19939084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to plot arbitrary markers on a pandas data series?
提问by P-Rod
I'm trying to place marks along a pandas data series (to show buy/sell events on a stock market graph)
我正在尝试沿着 Pandas 数据系列放置标记(以在股票市场图表上显示买入/卖出事件)
I'm able to do this on a simple array that I create using pyplot, however I can't find reference as to how to indicate arbitrary events in a pandas time series.
我可以在使用 pyplot 创建的简单数组上执行此操作,但是我找不到有关如何在 Pandas 时间序列中指示任意事件的参考。
Perhaps pandas doesn't have this functionality built in. Could someone provide help in the way one would take this series and add some arbitrary marks along the curve...
也许Pandas没有内置此功能。有人可以提供帮助,以获取本系列并沿曲线添加一些任意标记的方式......
import datetime
import matplotlib.pyplot as plt
import pandas
from pandas import Series, date_range
import numpy as np
import random
ts = Series(randn(1000), index=date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
#-- the markers should be another pandas time series with true/false values
#-- We only want to show a mark where the value is True
tfValues = np.random.randint(2, size=len(ts)).astype('bool')
markers = Series(tfValues, index=date_range('1/1/2000', periods=1000))
fig, ax1 = plt.subplots()
ts.plot(ax=ax1)
ax1.plot(markers,'g^') # This is where I get held up.
plt.show()
回答by KyungHoon Kim
Use the option
使用选项
ts.plot(marker='o')
or
或者
ts.plot(marker='.')
回答by Paul H
I had to take a slightly different approach avoiding pandas plotting methods altogether. That's somewhat a shame, since they format the x-axis so nicely. Nonetheless:
我不得不采取稍微不同的方法,完全避免使用Pandas绘图方法。这有点可惜,因为它们很好地格式化了 x 轴。尽管如此:
import datetime
import matplotlib.pyplot as plt
import numpy as np
import pandas
from pandas import Series, date_range
markers = Series([True, False, False, True, True, True, False, False, True, True],
index=date_range('1/1/2000', periods=10))
ts = Series(np.random.uniform(size=10), index=date_range('1/1/2000', periods=10))
ts = ts.cumsum()
ts2 = ts[markers]
fig, ax1 = plt.subplots()
ax1.plot(ts.index, ts, 'b-')
ax1.plot(ts2.index, ts2,'g^')
fig.autofmt_xdate()
Gives me:

给我:


