Matplotlib烛台(盘中)图表是一个大问题 [英] Matplotlib Candlestick (Intraday) Chart is One Big Blob
问题描述
我正在尝试使用Matplotlib绘制蜡烛图,其中包含我为REST API调用获取的数据.但是,由于该调用使用唯一的访问令牌,因此出于此问题的目的,我下载了示例数据并将其加载到csv中. 这是一个粘贴框链接,指向示例数据的外观.为了在Python中处理数据,我使用Pandas创建数据框架.这是我的代码:
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()
这是我的输出内容:
您可以整理出烛台的垂直线,但是条形看起来确实很宽.关于如何解决这个问题有什么想法吗?谢谢.
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.
推荐答案
只需更改图表上的宽度,就可以了:
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.
这篇关于Matplotlib烛台(盘中)图表是一个大问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!