Random Candlestick Chart Generator with Seaborn

  • Share this:

Code introduction


This function generates a candlestick chart using the Seaborn library's candlestick function with random stock price data.


Technology Stack : Seaborn, Matplotlib, NumPy

Code Type : Function

Code Difficulty : Intermediate


                
                    
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

def generate_random_candlestick_plot(data):
    """
    Generates a random candlestick plot using Seaborn.
    """
    # Select a random subset of data
    data = data.sample(n=50)
    
    # Create a new figure and axis
    fig, ax = plt.subplots()
    
    # Plot the candlestick chart
    sns.candlestick(ax=ax, data=data, width=0.6, colorup='g', colordown='r')
    
    # Set title and labels
    ax.set_title('Random Candlestick Plot')
    ax.set_xlabel('Date')
    ax.set_ylabel('Price')
    
    # Rotate date labels for better readability
    plt.xticks(rotation=45)
    
    # Display the plot
    plt.show()

# Example usage:
# data = pd.DataFrame({
#     'Date': pd.date_range(start='1/1/2020', periods=100),
#     'Open': np.random.rand(100) * 100,
#     'High': np.random.rand(100) * 100,
#     'Low': np.random.rand(100) * 100,
#     'Close': np.random.rand(100) * 100
# })
# generate_random_candlestick_plot(data)