使用图表库在 Python 中的烛台图上覆盖成交量剖面

Tys*_*onU 2 python matplotlib plotly bokeh plotly-dash

我想在 python 的烛台图上绘制成交量剖面图,这会导致类似这样的结果。

带有成交量曲线的蜡烛棒

我的主要 ohlc 数据将位于 pandas 数据框中。

Date,       Open,  High,  Low,   Close
2019-10-18, 54.09, 54.62, 53.35, 53.78
2019-10-17, 52.99, 54.16, 52.62, 53.93
2019-10-16, 52.92, 53.74, 52.51, 53.36
Run Code Online (Sandbox Code Playgroud)

然后我的卷信息将位于另一个像这样的数据框中。

Price, Volume
54.75, 150
54.50, 135
54.25, 140
54.00, 140
53.75, 125
53.50, 145
53.25, 130
53.00, 135
52.75, 155
52.50, 150
Run Code Online (Sandbox Code Playgroud)

我尝试过我所知道的几乎所有库,Matplotlib、Plotly、Bokeh。我尝试过简单地在烛台旁边绘制条形图,但缩放通常会关闭。我有兴趣使用 python 中的任何标准图表库,它可以以相当简单的方式产生这个结果。希望这里有人知道一些方法来做到这一点。

Tys*_*onU 6

好吧,我决定深入研究情节文档,看看是否能找到一种方法来做到这一点。事实证明这没什么大不了的。我开始越来越喜欢 Plotly。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 23 22:17:44 2019

@author: TysonU
"""
from plotly.offline import plot
import plotly.graph_objects as go
import random
import pandas as pd

#Create random OHLC and Volume
high = 40
low = 20
dev = 1
days = 100

fake_market = []
for each in range(days):
    ohlc = []
    ohlc.append(each)
    if each == 0:
        o = random.randrange(low, high)
        ohlc.append(o)
    else:
        ohlc.append(c) #I know
    h = random.randrange(o, high)
    ohlc.append(h)
    l = random.randrange(low, o)
    ohlc.append(l)
    c = random.randrange(l, h)
    ohlc.append(c)
    fake_market.append(ohlc)

fake_volume = [[x, random.randrange(10, 30)] for x in range(low, (high+1))]
df = pd.DataFrame(fake_market, columns=["Date", "Open", "High", "Low", "Close"])
df2 = pd.DataFrame(fake_volume, columns=["Volume", "Price"])

#Do all the plotly stuff
fig = go.Figure(
    data=[
        go.Bar(
            x=[str(x) for x in df2.Price.to_list()],
            y=[str(x) for x in df2.Volume.to_list()],
            orientation="h",
            xaxis="x",
            yaxis="y",
            visible=True,
            showlegend=False
        ),
        go.Candlestick(
            x=[str(x) for x in df.Date.to_list()],
            open=[str(x) for x in df.Open.to_list()],
            high=[str(x) for x in df.High.to_list()],
            low=[str(x) for x in df.Low.to_list()],
            close=[str(x) for x in df.Close.to_list()],
            xaxis="x2",
            yaxis="y2",
            visible=True,
            showlegend=False
        )
    ],
    layout=go.Layout(
        title=go.layout.Title(text="Candlestick With Volume Profile"),
        xaxis=go.layout.XAxis(
            side="top",
            range=[0, 300],
            rangeslider=go.layout.xaxis.Rangeslider(visible=False),
            showticklabels=False
        ),
        yaxis=go.layout.YAxis(
            side="left",
            range=[low, high],
            showticklabels=False
        ),
        xaxis2=go.layout.XAxis(
            side="bottom",
            title="Date",
            rangeslider=go.layout.xaxis.Rangeslider(visible=False),
            overlaying="x"
        ),
        yaxis2=go.layout.YAxis(
            side="right",
            title="Price",
            range=[low, high],
            overlaying="y"
        )
    )
)
template = ["plotly", "plotly_white", "plotly_dark", "ggplot2", "seaborn", "none"]
fig.update_layout(template=template[2])

plot(fig)
Run Code Online (Sandbox Code Playgroud)

不确定公开发布股票数据的法律是什么,因此我构建了一个简单的生成器来生成 OHLC 数据和交易量。真实的股票数据会让图表看起来不那么混乱。

烛台上的成交量剖面

我还没有搞清楚酒吧所在的一侧。目前它们位于左侧,但如果它们位于右侧就好了。不过应该是很容易的事情。

好吧,希望这对某人有帮助。祝你有美好的一天!