[英]how to plot arbitrary markers on a pandas data series?
我正在尝试在熊猫数据系列上放置标记(以在股市图上显示买/卖事件)
我可以在使用pyplot创建的简单数组上执行此操作,但是找不到有关如何指示熊猫时间序列中任意事件的参考。
也许熊猫没有内置此功能。有人可以以一些方式提供帮助吗,那就是沿用这个系列并在曲线上添加一些任意标记...
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()
使用选项
ts.plot(marker='o')
要么
ts.plot(marker='.')
我不得不采取一种略有不同的方法来完全避免使用熊猫绘图方法。 有点可惜,因为它们很好地格式化了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()
给我:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.