Search code examples
pythonmatplotlibfinance

creating a chart with high/low and percentile box plus other points


Hi I'd like to recreate the following plot with matplotlib and pandas. I started to use boxplot but i'm struggling to manipulate the kwargs. Is there a simple way to use boxplot or do I need to recreate the chart enitrely. One issue I had was also adding the current data? Best regards

Correlation Plot


Solution

  • The boxplot from matplotlib has indeed some limitations. For you to have full control over how the plot looks I would advise using Patches to draw Rectangles for example (code from Rectangles link):

    import matplotlib.pyplot as plt
    import matplotlib.patches as patches
    
    fig1 = plt.figure()
    ax1 = fig1.add_subplot(111, aspect='equal')
    ax1.add_patch(
        patches.Rectangle(
            (0.1, 0.1),   # (x,y)
            0.5,          # width
            0.5,          # height
        )
    )
    fig1.savefig('rect1.png', dpi=90, bbox_inches='tight')
    

    This is useful because you'll only need this and a normal plot command (for lines) in matplotlib to do a boxplot. This will give you immense control about color and shape and it's fairly easy to build. You also have text there you'll need for which you can use matplotlib text. The last thing are those markers which are very doable with a scatter.

    A boxplot is a shape that tells you information such a minimum, maximum, and percentiles (25,50,75). You can calculate this very easily with numpy percentile.

    The details of the plot (labels at the bottom, legend, title in box, and so on) can also be achieved but tinkering with labels, manually building a title box and so on.

    It will give you some work but these are the commands you need.