Home > Mobile >  How to display data from csv file
How to display data from csv file

Time:10-05

I have .csv file with data

Date,Open,High,Low,Close,Volume

2021-09-26 07:00:00,41973.56,42056.51,40754.88,41226.1,177401.278

2021-09-26 08:00:00,41225.28,41664.01,41000.0,41580.58,197624.913

2021-09-26 09:00:00,41580.59,43728.96,41501.79,43250.07,186953.864

2021-09-26 10:00:00,43254.5,43480.56,42858.0,43026.85,148568.335

2021-09-26 11:00:00,43026.85,43315.47,43008.0,43065.18,110506.689

when I trying to display it using this code I got incorrect visualization


from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from mpl_finance import candlestick_ohlc

import matplotlib.dates as mpl_dates
import matplotlib.pyplot as plt
import pandas as pd

import tkinter  as Tk


if __name__ == '__main__':


    root = Tk.Tk()
    root.title("candle")
    root.geometry('600x550')

    file  = 'Code.csv'

    data = pd.read_csv(file)
    ohlc = data.loc[:, ['Date', 'Open', 'High', 'Low', 'Close']]
    ohlc['Date'] = pd.to_datetime(ohlc['Date'])
    ohlc['Date'] = ohlc['Date'].apply(mpl_dates.date2num)
    ohlc = ohlc.astype(float)

    # Creating Subplots
    fig = plt.Figure(figsize=(6, 5), dpi=100)
    ax = fig.add_subplot(111)

    candlestick_ohlc(ax, ohlc.values, width=0.6, colorup='green', colordown='red', alpha=0.8)

    # Setting labels & titles
    ax.set_xlabel('Date')
    ax.set_ylabel('Price')
    fig.suptitle('Daily Candlestick Chart ')

    # Formatting Date
    date_format = mpl_dates.DateFormatter('%d-%m-%Y %H:%M')
    ax.xaxis.set_major_formatter(date_format)
    fig.autofmt_xdate()

    fig.tight_layout()

    canvas = FigureCanvasTkAgg(fig, master=root)

    canvas.get_tk_widget().pack()
    # creating toolbar

    root.mainloop()

screnshot

what am I doing wrong?

CodePudding user response:

mpl-finance is deprecated. You should use mplfinance instead:

pip install --upgrade mplfinance

Then, simply call the plot function on your DataFrame:

import mplfinance as mpf
columns = ['Date', 'Open', 'High', 'Low', 'Close']
df = pd.read_csv("data.csv", usecols=columns, parse_dates=["Date"])
df = df[columns].set_index("Date")

mpf.plot(df, type='candle')

enter image description here

  • Related