Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

python - Matplotlib Candlestick (Intraday) Chart is One Big Blob

I am trying to plot a Candlestick chart using Matplotlib with data I am acquiring for a REST API call. However since the call uses a unique access token I've downloaded a sample data and loaded it into a csv for the purposes of this question. Here is a pastebin link to what the sample data looks like. To process the data within Python I am using Pandas to create a data frames. Here is what my code looks like:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from matplotlib.finance import candlestick_ohlc
from datetime import date

""" Pandas """
historic_df = pd.read_csv("sample_data.csv")

dates = pd.to_datetime(historic_df['time'], format="%Y-%m-%dT%H:%M:%S.%fZ")
openp = historic_df['openAsk']
highp =  historic_df['highAsk']
lowp =  historic_df['lowAsk']
closep =  historic_df['closeAsk']

""" Matplotlib """
ax1 = plt.subplot2grid((1,1), (0,0))
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))

x = 0
ohlc = []

while x < len(dates):
    d = mdates.date2num(dates[x])
    append_me = d, openp.values[x], highp.values[x], lowp.values[x], closep.values[x]
    ohlc.append(append_me)
    x += 1

candlestick_ohlc(ax1, ohlc, width=0.4, colorup='#77d879', colordown='#db3f3f')
plt.show()

And here is what my output looks like:

enter image description here

You can sort of make out the vertical lines for the candlestick's, however the bars seems really wide. Any ideas on how I can solve this? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just change your width on the chart and it will be fine:

candlestick_ohlc(ax1, ohlc, width=0.001, colorup='#77d879', colordown='#db3f3f')

Tested using your data and it looks good.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...