如何获得matplotlib OHLC烛台图的y轴显示,以显示OHLC值而不是y轴光标位置

时间:2018-12-30 15:34:37

标签: python-3.x matplotlib candlestick-chart

我正在使用matplotlib创建OHLC烛台股价图表。我正在使用mpl_finance的Candlestick_ohlc模块来创建图表。创建图表很简单,但是图表底部的x和y轴显示显示给定光标位置的日期和y轴值,但是我希望x和y轴显示显示日期和打开的日期,高,低,关闭(ohlc)值,而不仅仅是日期和y轴光标位置值。报价数据集采用元组列表的格式,其中每个元组都包含日期(数字),后跟打开,最高,最低,关闭和成交量。我正在尝试使用matplotlib的format_coord函数来指定ohlc值,但是我不知道如何获取format_coord函数来接受包含日期和相​​关ohlc值的列表作为输入,然后给出所需的日期和OHLC输出。以下是我编写的一些简化的代码,它们展示了我的问题: 现在,下面的代码已被修改,可以完全正常工作:

import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY
from mpl_finance import candlestick_ohlc
from matplotlib.dates import date2num, num2date


def ohlc_daily_date_axis():
    mondays = WeekdayLocator(MONDAY)  
    alldays = DayLocator()            
    weekFormatter = DateFormatter('%b %d %Y')  # e.g., Jan 12 2018

    quotes = [(737042.0, 2.72, 2.78, 2.6815, 2.74, 414378.0),
              (737045.0, 2.71, 2.77, 2.57, 2.63, 578841.0),
              (737046.0, 2.64, 2.64, 2.4228, 2.47, 1451450.0),
              (737047.0, 2.9, 3.15, 2.7, 2.96, 7230260.0),
              (737048.0, 2.92, 3.29, 2.67, 2.83, 2784110.0),
              (737049.0, 2.78, 2.82, 2.4701, 2.51, 822776.0),
              (737052.0, 2.56, 2.6344, 2.49, 2.5, 278883.0),
              (737054.0, 2.5, 2.619, 2.34, 2.6, 606002.0),
              (737055.0, 2.57, 2.63, 2.45, 2.57, 1295820.0),
              (737056.0, 2.57, 2.75, 2.51, 2.65, 435838.0)]

    fig, ax = plt.subplots(figsize=(18,5))
    plt.subplots_adjust(bottom=0.2)
    ax.xaxis.set_major_locator(mondays)
    ax.xaxis.set_minor_locator(alldays)
    ax.xaxis.set_major_formatter(weekFormatter)

    candlestick_ohlc(ax, quotes, width=0.6)

    ax.xaxis_date()
    ax.autoscale_view()
    plt.setp(plt.gca().get_xticklabels(), rotation=45, 
horizontalalignment='right')
    #the following line puts the ohlc data in the y axis display
    ax.format_coord = get_ohlc_from_date_xy
    # the following line puts the ohlc data in the x axis display
    #ax.fmt_xdata = get_ohlc_from_date_x

    plt.show()

def get_ohlc_from_date_x(dateasnum):
    print('dateasnum: ', int(dateasnum))
    quotes = [(737042.0, 2.72, 2.78, 2.6815, 2.74, 414378.0),
              (737045.0, 2.71, 2.77, 2.57, 2.63, 578841.0),
              (737046.0, 2.64, 2.64, 2.4228, 2.47, 1451450.0),
              (737047.0, 2.9, 3.15, 2.7, 2.96, 7230260.0),
              (737048.0, 2.92, 3.29, 2.67, 2.83, 2784110.0),
              (737049.0, 2.78, 2.82, 2.4701, 2.51, 822776.0),
              (737052.0, 2.56, 2.6344, 2.49, 2.5, 278883.0),
              (737054.0, 2.5, 2.619, 2.34, 2.6, 606002.0),
              (737055.0, 2.57, 2.63, 2.45, 2.57, 1295820.0),
              (737056.0, 2.57, 2.75, 2.51, 2.65, 435838.0)]
    for i in range(len(quotes)):
        if int(dateasnum) == quotes[i][0]:
            open = quotes[i][1]
            high = quotes[i][2]
            low = quotes[i][3]
            close = quotes[i][4]
            vol = quotes[i][5]
    dte = str(num2date(dateasnum).date())
    print('type(dte): ', type(dte))
    print('open: ', open)
    ohlc_str = dte + ' open: ' + str(open) + ' high: ' + str(high) + ' 
low: ' + str(low) + ' close: ' + str(close) + ' vol: ' + str(int(vol)) 
+ '    '

    return ohlc_str



def get_ohlc_from_date_xy(dateasnum,y):
    print('dateasnum: ', int(dateasnum))
    quotes = [(737042.0, 2.72, 2.78, 2.6815, 2.74, 414378.0),
              (737045.0, 2.71, 2.77, 2.57, 2.63, 578841.0),
              (737046.0, 2.64, 2.64, 2.4228, 2.47, 1451450.0),
              (737047.0, 2.9, 3.15, 2.7, 2.96, 7230260.0),
              (737048.0, 2.92, 3.29, 2.67, 2.83, 2784110.0),
              (737049.0, 2.78, 2.82, 2.4701, 2.51, 822776.0),
              (737052.0, 2.56, 2.6344, 2.49, 2.5, 278883.0),
              (737054.0, 2.5, 2.619, 2.34, 2.6, 606002.0),
              (737055.0, 2.57, 2.63, 2.45, 2.57, 1295820.0),
              (737056.0, 2.57, 2.75, 2.51, 2.65, 435838.0)]
    for i in range(len(quotes)):
        if int(dateasnum) == quotes[i][0]:
            open = quotes[i][1]
            high = quotes[i][2]
            low = quotes[i][3]
            close = quotes[i][4]
            vol = quotes[i][5]
    dte = str(num2date(dateasnum).date())
    #print('type(dte): ', type(dte))
    #print('open: ', open)
    ohlc_str = 'open: ' + str(open) + ' high: ' + str(high) + ' 
low: ' + str(low) + ' close: ' + str(close) + ' vol: ' + str(int(vol))

    return dte, ohlc_str



# This def does not work
def format_coord(x,y, quotes):
    for i in range(len(quotes)):
        if int(x) == quotes[i]:
            open = quotes[i][1]
            high = quotes[i][2]
            low = quotes[i][3]
            close = quotes[i][4]
            vol = quotes[i][5]
    y = 'open: ' + open # I'm just using open to simplify things
    x = DateFormatter('%b %d %Y')
    return (x,y)


if __name__ == '__main__':
    ohlc_daily_date_axis()

如果按原样运行此代码,则会收到以下错误(这是我使用不正确的def format_coord(x,y,quotes)方法时遇到的错误):

File "/Users/Me/Mee/python_db_programs/learn_matplotlib_test.py", line 33, 
in ohlc_daily_date_axis
    ax.format_coord = format_coord(quotes)
TypeError: format_coord() missing 2 required positional arguments: 'y' 
and 'quotes'

如果我注释掉ax.format_coord = format_coord(quotes)行,那么代码可以正常运行,但是在x和y显示中没有我想要的日期和ohlc值。对于如何进行的任何帮助将不胜感激。

我最终没有尝试更改y显示,而是将ohlc值添加到x显示。这意味着我将ax.format_coord = format_coord(quotes)更改为仅格式化x坐标的命令,即ax.fmt_xdata,然后编写了一个def,使用引号列表获取对应于ohlc数据的每个日期:

ax.fmt_xdata = get_ohlc_from_date

代替

ax.format_coord = format_coord(quotes)

,然后添加以下定义:

def get_ohlc_from_date(dateasnum):
    print('dateasnum: ', int(dateasnum))
    quotes = [(737042.0, 2.72, 2.78, 2.6815, 2.74, 414378.0),
              (737045.0, 2.71, 2.77, 2.57, 2.63, 578841.0),
              (737046.0, 2.64, 2.64, 2.4228, 2.47, 1451450.0),
              (737047.0, 2.9, 3.15, 2.7, 2.96, 7230260.0),
              (737048.0, 2.92, 3.29, 2.67, 2.83, 2784110.0),
              (737049.0, 2.78, 2.82, 2.4701, 2.51, 822776.0),
              (737052.0, 2.56, 2.6344, 2.49, 2.5, 278883.0),
              (737054.0, 2.5, 2.619, 2.34, 2.6, 606002.0),
              (737055.0, 2.57, 2.63, 2.45, 2.57, 1295820.0),
              (737056.0, 2.57, 2.75, 2.51, 2.65, 435838.0)]
    for i in range(len(quotes)):
        if int(dateasnum) == quotes[i][0]:
            open = quotes[i][1]
            high = quotes[i][2]
            low = quotes[i][3]
            close = quotes[i][4]
            vol = quotes[i][5]
    dte = str(num2date(dateasnum).date())
    print('type(dte): ', type(dte))
    print('open: ', open)
    ohlc_str = dte + ' open: ' + str(open) + ' high: ' + str(high) + ' 
low: ' + str(low) + ' close: ' + str(close) + ' vol: ' + str(int(vol)) 
+ '    '

    return ohlc_str

而且因为我使用了matplotlibs dateasnum函数,所以我也必须导入它:

from matplotlib.dates import num2date

虽然这不会用ohlc值代替y轴坐标,但确实在x和y轴显示中提供了ohlc值

弄清楚如何将ohlc值添加到x轴显示之后,我意识到我用来将ohlc值添加到x轴显示的逻辑可以应用于y轴显示,从而允许显示ohlc值在y轴参数中。这是通过使用ax.format_coord = format_coord命令并创建一个将ohlc值分配给y轴返回值的新def来完成的。我已经修改了发布的原始代码,以便根据ax.format_coord = format_coord行还是ax.fmt_xdata = get_ohlc_from_date行被注释掉来确定ohlc值是作为x轴显示的一部分还是作为一部分显示的y轴显示的方向

2 个答案:

答案 0 :(得分:0)

以下是允许matplotlib OHLC烛形图的x和y轴读数在Y轴读数而不是Y轴光标位置显示OHLC值的解决方案。

import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, 
MONDAY
from mpl_finance import candlestick_ohlc
from matplotlib.dates import date2num, num2date


def ohlc_daily_date_axis():
    mondays = WeekdayLocator(MONDAY)  
    alldays = DayLocator()            
    weekFormatter = DateFormatter('%b %d %Y')  # e.g., Jan 12 2018

    quotes = [(737042.0, 2.72, 2.78, 2.6815, 2.74, 414378.0),
              (737045.0, 2.71, 2.77, 2.57, 2.63, 578841.0),
              (737046.0, 2.64, 2.64, 2.4228, 2.47, 1451450.0),
              (737047.0, 2.9, 3.15, 2.7, 2.96, 7230260.0),
              (737048.0, 2.92, 3.29, 2.67, 2.83, 2784110.0),
              (737049.0, 2.78, 2.82, 2.4701, 2.51, 822776.0),
              (737052.0, 2.56, 2.6344, 2.49, 2.5, 278883.0),
              (737054.0, 2.5, 2.619, 2.34, 2.6, 606002.0),
              (737055.0, 2.57, 2.63, 2.45, 2.57, 1295820.0),
              (737056.0, 2.57, 2.75, 2.51, 2.65, 435838.0)]

    fig, ax = plt.subplots(figsize=(18,5))
    plt.subplots_adjust(bottom=0.2)
    ax.xaxis.set_major_locator(mondays)
    ax.xaxis.set_minor_locator(alldays)
    ax.xaxis.set_major_formatter(weekFormatter)

    candlestick_ohlc(ax, quotes, width=0.6)

    ax.xaxis_date()
    ax.autoscale_view()
    plt.setp(plt.gca().get_xticklabels(), rotation=45, 
horizontalalignment='right')
    #the following line puts the ohlc data in the y axis display
    ax.format_coord = get_ohlc_from_date_xy
    # the following line puts the ohlc data in the x axis display
    #ax.fmt_xdata = get_ohlc_from_date_x

    plt.show()

def get_ohlc_from_date_x(dateasnum):
    print('dateasnum: ', int(dateasnum))
    quotes = [(737042.0, 2.72, 2.78, 2.6815, 2.74, 414378.0),
              (737045.0, 2.71, 2.77, 2.57, 2.63, 578841.0),
              (737046.0, 2.64, 2.64, 2.4228, 2.47, 1451450.0),
              (737047.0, 2.9, 3.15, 2.7, 2.96, 7230260.0),
              (737048.0, 2.92, 3.29, 2.67, 2.83, 2784110.0),
              (737049.0, 2.78, 2.82, 2.4701, 2.51, 822776.0),
              (737052.0, 2.56, 2.6344, 2.49, 2.5, 278883.0),
              (737054.0, 2.5, 2.619, 2.34, 2.6, 606002.0),
              (737055.0, 2.57, 2.63, 2.45, 2.57, 1295820.0),
              (737056.0, 2.57, 2.75, 2.51, 2.65, 435838.0)]
    for i in range(len(quotes)):
        if int(dateasnum) == quotes[i][0]:
            open = quotes[i][1]
            high = quotes[i][2]
            low = quotes[i][3]
            close = quotes[i][4]
            vol = quotes[i][5]
    dte = str(num2date(dateasnum).date())
    print('type(dte): ', type(dte))
    print('open: ', open)
    ohlc_str = dte + ' open: ' + str(open) + ' high: ' + str(high) + ' 
low: ' + str(low) + ' close: ' + str(close) + ' vol: ' + str(int(vol)) 
+ '    '

    return ohlc_str



def get_ohlc_from_date_xy(dateasnum,y):
    print('dateasnum: ', int(dateasnum))
    quotes = [(737042.0, 2.72, 2.78, 2.6815, 2.74, 414378.0),
              (737045.0, 2.71, 2.77, 2.57, 2.63, 578841.0),
              (737046.0, 2.64, 2.64, 2.4228, 2.47, 1451450.0),
              (737047.0, 2.9, 3.15, 2.7, 2.96, 7230260.0),
              (737048.0, 2.92, 3.29, 2.67, 2.83, 2784110.0),
              (737049.0, 2.78, 2.82, 2.4701, 2.51, 822776.0),
              (737052.0, 2.56, 2.6344, 2.49, 2.5, 278883.0),
              (737054.0, 2.5, 2.619, 2.34, 2.6, 606002.0),
              (737055.0, 2.57, 2.63, 2.45, 2.57, 1295820.0),
              (737056.0, 2.57, 2.75, 2.51, 2.65, 435838.0)]
    for i in range(len(quotes)):
        if int(dateasnum) == quotes[i][0]:
            open = quotes[i][1]
            high = quotes[i][2]
            low = quotes[i][3]
            close = quotes[i][4]
            vol = quotes[i][5]
    dte = str(num2date(dateasnum).date())
    #print('type(dte): ', type(dte))
    #print('open: ', open)
    ohlc_str = 'open: ' + str(open) + ' high: ' + str(high) + ' 
low: ' + str(low) + ' close: ' + str(close) + ' vol: ' + str(int(vol))

    return dte, ohlc_str






if __name__ == '__main__':
    ohlc_daily_date_axis()

答案 1 :(得分:0)

您可以使用 Plotly's CandleStick Chart,内置您想要的一切。

Here is a example

元组到数据帧的序列

要做到这一点,您需要将数据放在包含 ["Date", "High", "Low", "Open", "Close"] 列的数据框中,查看 pandas.DataFrame.from_records 将您的数据导入到 DataFrame 中,因为它会从一系列元组创建一个 DataFrame 对象.

其他

  • 您可能需要将日期转换为日期时间,请查看 pandas.to_datetime 进行转换

问题的答案

来自情节的documentation

<块引用>

hoverinfo – 确定在悬停时显示哪些跟踪信息。如果设置 none 或 skip ,则悬停时不显示任何信息。但是,如果没有设置,点击和悬停事件仍然会被触发。

另外值得一读: Hover Text and Formatting in Python

最终考虑

我知道它不是按照您的意愿使用 matplotlib 制作的,但我认为它与答案相关。

代码

def generatePlotly(df):
    layout = go.Layout(
        plot_bgcolor="#FFF",      # Sets background color to white
        hovermode="x",
        hoverdistance=100,        # Distance to show hover label of data point
        spikedistance=1000,       # Distance to show spike
        xaxis=dict(
            title="Data",         # X Axis Title
            linecolor="#BCCCDC",  # Sets color of X-axis line
            showgrid=False,       # Removes X-axis grid lines
            showspikes=True,      # Show spike line for X-axis
            gridcolor="#BCCCDC",  # Grid color, if enabled
            # Format spike - Show a Line at the pointer
            spikethickness=2,
            spikedash="dot",
            spikecolor="#999999",
            spikemode="across",
            fixedrange=True,
            spikesnap="cursor",
        ),
        yaxis=dict(
            title="Preço (R$)",   # Y Axis Title
            linecolor="#BCCCDC",  # Sets color of Y-axis line
            showgrid=False,       # Removes Y-axis grid lines
            gridcolor="#BCCCDC",  # Grid color, if enabled
            showspikes=True,      # Show spike line for X-axis
            # Format spike - Show a Line at the pointer
            spikethickness=2,
            spikedash="dot",
            spikecolor="#999999",
            spikemode="across",
            fixedrange=True,
            side="right",
            spikesnap="cursor",
        ),
        margin=go.layout.Margin(
            l=0,  # left margin
            r=0,  # right margin
            b=0,  # bottom margin
            t=0,  # top margin
        ),
    )

    fig = go.Figure(
        data=[
            go.Candlestick(
                x=df["Date"],      # Your data
                open=df["Open"],   
                high=df["High"],
                low=df["Low"],
                close=df["Close"],
            )
        ],
        layout=layout,
    )
    # Remove rangeslider from the chart, you can just comment the next line
    fig.update_layout(xaxis_rangeslider_visible=False)

    # Legend position
    fig.update_layout(legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01)) 

    ############################################################
    # For this part look at EXTRAS at the bottom of the answer #
    ############################################################
    # build complete timepline from start date to end date
    dt_all = pd.date_range(start=df["Date"].iloc[0], end=df["Date"].iloc[-1])
    # retrieve the dates that ARE in the original datset
    dt_obs = [d.strftime("%Y-%m-%d") for d in pd.to_datetime(df["Date"])]
    # define dates with missing values
    dt_breaks = [d for d in dt_all.strftime("%Y-%m-%d").tolist() if not d in dt_obs]
    fig.update_xaxes(
        rangebreaks=[
            # dict(bounds=["sat", "mon"]),  # hide weekends
            dict(values=dt_breaks)
        ]
    )

    # Hover Distance and Hover Info
    # fig.update_layout(hoverdistance=0)
    # fig.update_traces(xaxis="x", hoverinfo="none")

    fig.show()

额外内容

Plotly: How to style a plotly figure so that it doesn't display gaps for missing dates?