1

In my program i have generate a candlestick in axis and a data plot in axix2. For candlestick graph i have created a variable named data. it is python dictionary type variable. where i have assign date .'date': ['2018/10/30', '2018/11/03', '2018/11/04', '2018/11/05', '2018/11/07', '2018/11/10', '2018/11/11']. other key is 'open','high','low','close' and 'volume'
Here date '2018/10/30' and '2018/11/03' there is a gap of date.
Here also date '2018/11/07' and '2018/11/10' there is a gap of date
.
So when i generate my candlestick graph missing date space is taken.How can i remove this missing date space. my program:

import sys
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from PyQt5.QtWidgets import QMainWindow,QVBoxLayout
from PyQt5.QtWidgets import QApplication
from PyQt5 import QtCore, QtGui, QtWidgets
import datetime
from matplotlib.dates import num2date, date2num
from mpl_finance import candlestick_ochl as candlestick
import numpy as np
import matplotlib.ticker as ticker
class MainWindow_code_serarch(object):

    def setup_code_serarch(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(870, 680)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.verticalLayoutWidget1 = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget1.setGeometry(QtCore.QRect(17, 30, 341, 153))
        self.verticalLayoutWidget1.setObjectName("verticalLayoutWidget")
        self.verticalLayout1 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget1)
        self.verticalLayout1.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout1.setObjectName("verticalLayout1")

        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(17, 150, 741, 553))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.figure = Figure(figsize=None, dpi=80, facecolor='k')
        self.canvas = FigureCanvas(self.figure)
        # self.navigation_toolbar = MyToolbar(self.canvas, self)
        # self.navigation_toolbar.coordinates = False

        self.verticalLayout.addWidget(self.canvas)
        # self.verticalLayout1.addWidget(self.navigation_toolbar)
        axes, axes2 = self.figure.subplots(nrows=2, sharex=True)


        data={
        'date' : ['2018/10/30', '2018/11/03', '2018/11/04', '2018/11/05', '2018/11/07', '2018/11/10', '2018/11/11'],
        'open': [8824, 8726.31, 8642.14, 8531.51, 8630.25, 8602.50, 8640.22],
        'high':[8858, 8748.60, 8551.36, 8653.16, 8476.69, 8630, 8570.56],
        'low' :[8688, 8743.67, 8550.76, 8449.50, 8631.83, 8602.18, 8743.22],
        'close':[8820, 8747.17, 8550.52, 8553., 8517.10, 8628.78, 8588.52],
        'volume': [17759.56, 120000.17, 18739.52, 38599.50, 16517.10, 17723.78, 15588.52]
        }

        x = date2num([datetime.datetime.strptime(d, '%Y/%m/%d').date() for d in data['date']])
        candle_trace = zip(x, data['open'], data['high'], data['low'], data['close'], data['volume'])
        candlestick(axes, candle_trace, width=.75, colorup='w', colordown='r');

        axes2.plot(x,[1, 2, 3, 4,7,8,9])

        axes.set_position([0.02, 0.37, 0.88, 0.6])
        axes2.set_position([0.02, 0.15, 0.88, 0.22])
        axes.tick_params(axis='both', color='#ffffff', labelcolor='#ffffff')
        axes.yaxis.tick_right()
        axes2.tick_params(axis='both', color='#ffffff', labelcolor='#ffffff')
        axes2.grid(color='lightgray', linewidth=.5, linestyle=':')
        axes.grid(color='lightgray', linewidth=.5, linestyle=':')
        axes2.yaxis.tick_right()
        axes.autoscale_view()
        axes2.autoscale_view()
        axes2.xaxis_date()
        axes.xaxis_date()

        axes.margins(0, .5)
        axes2.margins(0, .5)
        axes.set_facecolor('#041105')
        axes2.set_facecolor('#041105')
        # axes.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
        # axes2.xaxis.set_major_formatter(ticker.FuncFormatter(format_date))
        #self.multi = FigureCursor(self.figure, horizOn=True, vertOn=True, color='r', lw=1)
        self.canvas.draw()

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 246, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        # self.pushButton.clicked.connect(self.graphShowCode)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        # self.pushButton.setText(_translate("MainWindow", "OK"))

    # def format_date(x, pos=None):
    #         thisind = np.clip(int(x + 0.5), 0, N - 1)
    #         return r.date[thisind].strftime('%Y-%m-%d')


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = MainWindow_code_serarch()
    ui.setup_code_serarch(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

output: Output

1 Answer 1

3

I have solved "Remove missing date space and xtricklabel from graph matplotlib" In this way that, in my data array i have replace dates with integers starting from 0. I have also set xtricklabel with time.

My solution code:

import sys
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from PyQt5.QtWidgets import QMainWindow,QVBoxLayout
from PyQt5.QtWidgets import QApplication
from PyQt5 import QtCore, QtGui, QtWidgets
import datetime
from matplotlib.dates import num2date, date2num
from mpl_finance import candlestick_ochl as candlestick
import numpy as np
import matplotlib.ticker as ticker
import matplotlib.dates as mdates
import pylab as pl
class MainWindow_code_serarch(object):

    def setup_code_serarch(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(870, 680)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.verticalLayoutWidget1 = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget1.setGeometry(QtCore.QRect(17, 30, 341, 153))
        self.verticalLayoutWidget1.setObjectName("verticalLayoutWidget")
        self.verticalLayout1 = QtWidgets.QVBoxLayout(self.verticalLayoutWidget1)
        self.verticalLayout1.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout1.setObjectName("verticalLayout1")

        self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(17, 150, 741, 553))
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.figure = Figure(figsize=None, dpi=80, facecolor='k')
        self.canvas = FigureCanvas(self.figure)
        # self.navigation_toolbar = MyToolbar(self.canvas, self)
        # self.navigation_toolbar.coordinates = False

        self.verticalLayout.addWidget(self.canvas)
        # self.verticalLayout1.addWidget(self.navigation_toolbar)
        axes,axes2 = self.figure.subplots(nrows=2, sharex=True)


        data={
        'date' : ['2018/10/30', '2018/11/03', '2018/11/04', '2018/11/05', '2018/11/07', '2018/11/10', '2018/11/11'],
        'open': [8824, 8726.31, 8642.14, 8531.51, 8630.25, 8602.50, 8640.22],
        'high':[8858, 8748.60, 8551.36, 8653.16, 8476.69, 8630, 8570.56],
        'low' :[8688, 8743.67, 8550.76, 8449.50, 8631.83, 8602.18, 8743.22],
        'close':[8820, 8747.17, 8550.52, 8553., 8517.10, 8628.78, 8588.52],
        'volume': [17759.56, 120000.17, 18739.52, 38599.50, 16517.10, 17723.78, 15588.52]
        }
        # result = [( d['low'],d['close'],d['volume']) for d in data]
        # print(result)
        x = date2num([datetime.datetime.strptime(d, '%Y/%m/%d').date() for d in data['date']])
        t= np.arange(len(data['date']))

        candle_trace = zip(t, data['open'], data['high'], data['low'], data['close'], data['volume'])
        # print(list(candle_trace))
        candlestick(axes, candle_trace, width=.75, colorup='g', colordown='r');

        axes2.plot(t,[1, 2, 3, 4,7,8,9])

        axes.set_position([0.02, 0.37, 0.88, 0.6])
        axes2.set_position([0.02, 0.15, 0.88, 0.22])
        axes.tick_params(axis='both', color='#ffffff', labelcolor='#ffffff')
        axes.yaxis.tick_right()
        axes2.tick_params(axis='both', color='#ffffff', labelcolor='#ffffff')
        axes2.grid(color='lightgray', linewidth=.5, linestyle=':')
        axes.grid(color='lightgray', linewidth=.5, linestyle=':')
        axes2.yaxis.tick_right()
        axes.autoscale_view()
        axes2.autoscale_view()
        # axes2.xaxis_date()
        # axes.xaxis_date()

        # axes.margins(0, .5)
        # axes2.margins(0, .5)
        axes.set_facecolor('#041105')
        axes2.set_facecolor('#041105')

        #self.multi = FigureCursor(self.figure, horizOn=True, vertOn=True, color='r', lw=1)
        # N = len(dates)

        axes.set_xticks(range(0, len((x)), 1))
        # axes.set_xticklabels([mdates.num2date(d).strftime('%b-%d') for d in x])
        axes.set_xticklabels([mdates.num2date(d).strftime('%Y-%m-%d') for d in x])
        axes2.set_xticklabels([mdates.num2date(d).strftime('%Y-%m-%d') for d in x])

        self.canvas.draw()

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 246, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        # self.pushButton.clicked.connect(self.graphShowCode)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        # self.pushButton.setText(_translate("MainWindow", "OK"))




if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = MainWindow_code_serarch()
    ui.setup_code_serarch(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Output is : enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.