Matplotlib仅绘制日期,而不是给定的DateTime变量
我正在尝试绘制烛台,但由于某种原因,该图不会抓住整个DateTime变量。但是只有日期。 给出的数据。
将所有蜡烛绘制在相同的X轴上,而不是所有15分钟,因为在代码之后
## PLOT Candles
plt.figure()
#define width of candlestick elements
width = .5
width2 = .05
#define up and down prices
up = df[df.close>=df.open]
down = df[df.close<df.open]
print(up)
#define colors to use
col1 = 'green'
col2 = 'red'
col3 = 'blue'
col4 = 'grey'
#plot up prices
plt.bar(up.index,up.close-up.open,width,bottom=up.open,color=col1)
plt.bar(up.index,up.high-up.close,width2,bottom=up.close,color=col1)
plt.bar(up.index,up.low-up.open,width2,bottom=up.open,color=col1)
#plot down prices
plt.bar(down.index,down.close-down.open,width,bottom=down.open,color=col2)
plt.bar(down.index,down.high-down.open,width2,bottom=down.open,color=col2)
plt.bar(down.index,down.low-down.close,width2,bottom=down.close,color=col2)
plt.show()
exit()
{'Open':{Timestamp('2016-01-14 08:15:00'):1.08719, 时间戳('2016-01-14 08:30:00'):1.08735,时间戳('2016-01-14 08:45:00'):1.08674,时间戳('2016-01-14 09:00:00'):1.08674, 时间戳('2016-01-14 09:15:00'):1.08671},'High': {Timestamp('2016-01-14 08:15:00'):1.08749,时间戳('2016-01-14 08:30:00'):1.08739,时间戳('2016-01-14 08:45:00'):1.08734, 时间戳('2016-01-14 09:00:00'):1.08722,时间戳('2016-01-14 09:15:00'):1.08673},'low':{timestamp('2016-01-14 08:15:00'): 1.0869,时间戳('2016-01-14 08:30:00'):1.08673,时间戳记('2016-01-14 08:45:00'):1.08669,Timestamp('2016-01-01-14 09:00:00'):1.08666,时间戳('2016-01-14 09:15:00'):1.08582}, “关闭”:{timestamp('2016-01-14 08:15:00'):1.08736, 时间戳('2016-01-14 08:30:00'):1.08673,时间戳('2016-01-14 08:45:00'):1.08673,时间戳('2016-01-14 09:00:00'):1.08671, 时间戳('2016-01-14 09:15:00'):1.08618},'卷': {Timestamp('2016-01-14 08:15:00'):2181,时间戳('2016-01-14 08:30:00'):1738,时间戳('2016-01-14 08:45:00'):1938, 时间戳('2016-01-14 09:00:00'):3010,时间戳('2016-01-14 09:15:00'):2734}}
I am trying to plot candle sticks but for some reason, the plot does not grab the entire DateTime variable. But only the date. Plotting all candles on the same X axis rather than all 15 min as the data is given in.
Following code.
## PLOT Candles
plt.figure()
#define width of candlestick elements
width = .5
width2 = .05
#define up and down prices
up = df[df.close>=df.open]
down = df[df.close<df.open]
print(up)
#define colors to use
col1 = 'green'
col2 = 'red'
col3 = 'blue'
col4 = 'grey'
#plot up prices
plt.bar(up.index,up.close-up.open,width,bottom=up.open,color=col1)
plt.bar(up.index,up.high-up.close,width2,bottom=up.close,color=col1)
plt.bar(up.index,up.low-up.open,width2,bottom=up.open,color=col1)
#plot down prices
plt.bar(down.index,down.close-down.open,width,bottom=down.open,color=col2)
plt.bar(down.index,down.high-down.open,width2,bottom=down.open,color=col2)
plt.bar(down.index,down.low-down.close,width2,bottom=down.close,color=col2)
plt.show()
exit()
print(df[:5].to_dict())
{'open': {Timestamp('2016-01-14 08:15:00'): 1.08719,
Timestamp('2016-01-14 08:30:00'): 1.08735, Timestamp('2016-01-14
08:45:00'): 1.08674, Timestamp('2016-01-14 09:00:00'): 1.08674,
Timestamp('2016-01-14 09:15:00'): 1.08671}, 'high':
{Timestamp('2016-01-14 08:15:00'): 1.08749, Timestamp('2016-01-14
08:30:00'): 1.08739, Timestamp('2016-01-14 08:45:00'): 1.08734,
Timestamp('2016-01-14 09:00:00'): 1.08722, Timestamp('2016-01-14
09:15:00'): 1.08673}, 'low': {Timestamp('2016-01-14 08:15:00'):
1.0869, Timestamp('2016-01-14 08:30:00'): 1.08673, Timestamp('2016-01-14 08:45:00'): 1.08669, Timestamp('2016-01-14
09:00:00'): 1.08666, Timestamp('2016-01-14 09:15:00'): 1.08582},
'close': {Timestamp('2016-01-14 08:15:00'): 1.08736,
Timestamp('2016-01-14 08:30:00'): 1.08673, Timestamp('2016-01-14
08:45:00'): 1.08673, Timestamp('2016-01-14 09:00:00'): 1.08671,
Timestamp('2016-01-14 09:15:00'): 1.08618}, 'volume':
{Timestamp('2016-01-14 08:15:00'): 2181, Timestamp('2016-01-14
08:30:00'): 1738, Timestamp('2016-01-14 08:45:00'): 1938,
Timestamp('2016-01-14 09:00:00'): 3010, Timestamp('2016-01-14
09:15:00'): 2734}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,让我们这样做!
第一件事首先:
给定样本我问您:
将“数据”转换为
df
,然后将index
转换为正确的格式:然后绘制它!
输出:
Well, let's do this!
First things first:
https://pypi.org/project/mplfinance/
Given the sample I asked you:
Transform "data" into a
df
and convert theindex
to the right format:Then plot it!
Output: