Categories
Coding Pandas Plotly Python Tutorial

Candlestick Charts using Python with Pandas and Plotly

I’ve only just discovered the Python extension Plotly and can’t recommend it enough. I’ve only just began using it and scraping the surface of what it can do but it’s very clever. Candlestick charts are just for starters.

I just want to go through how easy it is to created candlestick charts. The data I will be using is from one of my previous tutorials.

Required Imports

To begin we just need to import the graph_objects extension from Plotly along with Pandas. We will then use the read_csv function to store our CSV file within a DataFrame.

import plotly.graph_objects as go
import pandas as pd

ltc = pd.read_csv('LTC Day History.csv')

Plotting the Data Points

Now all we need to do is tell Plotly our start time, high, low, open and close columns and it will take care of the rest.

fig = go.Figure(
    data = [go.Candlestick(
        x = ltc['Start Time'],
        open = ltc['Price Open'],
        high = ltc['Price High'],
        low = ltc['Price Low'],
        close = ltc['Price Close']
    )]
)

fig.show()

Outputting the Chart

After that Plotly will plot this beautiful candlestick graph that has been options available. These include exporting the chart as an image, zooming/panning, lasso selection and much more.

Candlestick Charts using Python with Pandas and Plotly Graph Output

Full Code Example

import plotly.graph_objects as go
import pandas as pd

ltc = pd.read_csv('LTC Day History.csv')

fig = go.Figure(
    data = [go.Candlestick(
        x = ltc['Start Time'],
        open = ltc['Price Open'],
        high = ltc['Price High'],
        low = ltc['Price Low'],
        close = ltc['Price Close']
    )]
)

fig.show()