Search code examples
pythonmatplotlibplotdata-visualizationvisualization

How do I make Order Book Depth Chart in Matplotlib?


I am trying to create a "Depth Chart" from the Order book which will look like the below:

enter image description here

I figured out that histogram will be used but unable to set the proper bins hence producing an improper chart. 'ASK/QTY' are given below:

[49579.41, 49580.66, 49581.39, 49585.42, 49585.43, 49586.99, 49588.0, 49588.74, 49589.62, 49589.8, 49589.81, 49590.0, 49590.37, 49590.38, 49591.38, 49591.87, 49592.84, 49593.32, 49593.86, 49594.17, 49594.36, 49595.32, 49595.54, 49595.92, 49595.97, 49596.87, 49596.88, 49596.99, 49597.06, 49597.2, 49597.21, 49597.52, 49597.64, 49597.79, 49598.08, 49598.46, 49598.69, 49598.98, 49599.52, 49600.0, 49600.14, 49600.26, 49600.34, 49600.47, 49600.62, 49600.73, 49601.0, 49601.25, 49601.3, 49601.71, 49601.85, 49601.93, 49602.2, 49602.7, 49602.93, 49603.12, 49603.15, 49603.23, 49603.56, 49604.27, 49604.31, 49604.32, 49604.89, 49604.9, 49605.12, 49605.21, 49605.5, 49605.59, 49606.0, 49606.13, 49606.39, 49606.62, 49607.2, 49607.5, 49607.68, 49607.69, 49607.71, 49607.76, 49608.05, 49608.11, 49608.67, 49609.71, 49610.0, 49610.07, 49610.18, 49610.53, 49610.54, 49611.72, 49612.1, 49612.25, 49612.42, 49612.71, 49612.99, 49613.0, 49613.42, 49613.45, 49613.53, 49613.62, 49613.84, 49613.92]
[4.514622, 1.0, 1.0, 0.014002, 0.04, 0.012384, 0.005441, 0.04, 0.254507, 0.219849, 0.559163, 0.14, 0.219849, 0.02, 0.010085, 0.399503, 0.50407, 0.05, 0.133538, 0.000952, 0.03, 0.023, 0.1205, 0.063524, 0.038323, 0.371299, 0.03, 0.041181, 0.182644, 0.088, 0.022, 0.040338, 0.137032, 0.464753, 0.05, 0.893, 1.248, 0.022, 0.3, 4.683597, 0.797, 0.004717, 0.504112, 0.009785, 0.03, 0.006436, 0.005641, 0.2937, 0.116174, 0.0183, 0.853, 0.041181, 0.03, 0.005512, 0.4, 0.24, 0.040373, 0.364929, 0.008, 0.005515, 0.163396, 0.03, 0.00377, 0.03943, 0.02386, 0.765352, 0.46, 0.115285, 0.251659, 0.049, 0.111, 0.4, 0.4, 0.364734, 0.022, 0.319989, 0.141003, 0.426611, 0.022, 0.06, 0.626486, 0.5, 0.004243, 0.059612, 0.3, 4.747239, 0.04, 0.2406, 0.021046, 0.237078, 0.428943, 0.000626, 0.000932, 0.001094, 1.906807, 0.03, 0.349154, 0.040366, 0.004549, 0.038309]

My code is given below:

import matplotlib
from utilities import get_order_book
import matplotlib.pyplot as plt

if __name__ == '__main__':
    pair = 'BTC/USDT'
    asks, bids = get_order_book(pair)
    ask_x = []
    ask_y = []
    for ask in asks:
        ask_x.append(ask[0])
        ask_y.append(ask[1])

    print(ask_x)
    print(ask_y)
    plt.bar(ask_x, ask_y, 2)
    plt.hist(ask_x, bins=len(ask_x))
    plt.show()

Solution

  • If you are not interested in coloring the area under the curve, you can use matplotlib.pyplot.step (pay attention to where parameter, in this case I used the default pre value):

    x = [49579.41, 49580.66, 49581.39, 49585.42, 49585.43, 49586.99, 49588.0, 49588.74, 49589.62, 49589.8, 49589.81, 49590.0, 49590.37, 49590.38, 49591.38, 49591.87, 49592.84, 49593.32, 49593.86, 49594.17, 49594.36, 49595.32, 49595.54, 49595.92, 49595.97, 49596.87, 49596.88, 49596.99, 49597.06, 49597.2, 49597.21, 49597.52, 49597.64, 49597.79, 49598.08, 49598.46, 49598.69, 49598.98, 49599.52, 49600.0, 49600.14, 49600.26, 49600.34, 49600.47, 49600.62, 49600.73, 49601.0, 49601.25, 49601.3, 49601.71, 49601.85, 49601.93, 49602.2, 49602.7, 49602.93, 49603.12, 49603.15, 49603.23, 49603.56, 49604.27, 49604.31, 49604.32, 49604.89, 49604.9, 49605.12, 49605.21, 49605.5, 49605.59, 49606.0, 49606.13, 49606.39, 49606.62, 49607.2, 49607.5, 49607.68, 49607.69, 49607.71, 49607.76, 49608.05, 49608.11, 49608.67, 49609.71, 49610.0, 49610.07, 49610.18, 49610.53, 49610.54, 49611.72, 49612.1, 49612.25, 49612.42, 49612.71, 49612.99, 49613.0, 49613.42, 49613.45, 49613.53, 49613.62, 49613.84, 49613.92]
    y = [4.514622, 1.0, 1.0, 0.014002, 0.04, 0.012384, 0.005441, 0.04, 0.254507, 0.219849, 0.559163, 0.14, 0.219849, 0.02, 0.010085, 0.399503, 0.50407, 0.05, 0.133538, 0.000952, 0.03, 0.023, 0.1205, 0.063524, 0.038323, 0.371299, 0.03, 0.041181, 0.182644, 0.088, 0.022, 0.040338, 0.137032, 0.464753, 0.05, 0.893, 1.248, 0.022, 0.3, 4.683597, 0.797, 0.004717, 0.504112, 0.009785, 0.03, 0.006436, 0.005641, 0.2937, 0.116174, 0.0183, 0.853, 0.041181, 0.03, 0.005512, 0.4, 0.24, 0.040373, 0.364929, 0.008, 0.005515, 0.163396, 0.03, 0.00377, 0.03943, 0.02386, 0.765352, 0.46, 0.115285, 0.251659, 0.049, 0.111, 0.4, 0.4, 0.364734, 0.022, 0.319989, 0.141003, 0.426611, 0.022, 0.06, 0.626486, 0.5, 0.004243, 0.059612, 0.3, 4.747239, 0.04, 0.2406, 0.021046, 0.237078, 0.428943, 0.000626, 0.000932, 0.001094, 1.906807, 0.03, 0.349154, 0.040366, 0.004549, 0.038309]
    
    
    fig, ax = plt.subplots()
    
    ax.step(x, y)
    
    plt.show()
    

    enter image description here

    Otherwise, if you want to color the area under the curve, you can use matplotlib.pyplot.stairs, as already suggested by BigBen in the comment. Pay attention to the fact you need to add a value to x array, because this time it represents edges, not data points.

    x.append(x[-1])
    
    fig, ax = plt.subplots()
    
    ax.stairs(edges = x, values = y, fill = True)
    
    plt.show()
    

    enter image description here

    It seems the two plots are different: this because in matplotlib.pyplot.step I used the default value for where parameter; on the contrary, matplotlib.pyplot.stairs uses a post interpolation:

    x.append(x[-1])
    
    fig, ax = plt.subplots()
    
    ax.step(x[:-1], y, where = 'post')
    ax.stairs(edges = x, values = y, fill = True)
    
    plt.show()
    

    enter image description here