从图形 matplotlib 烛台中删除缺少的日期空间和技巧标签
Posted
技术标签:
【中文标题】从图形 matplotlib 烛台中删除缺少的日期空间和技巧标签【英文标题】:Remove missing date space and tricklabel from graph matplotlib candlestick 【发布时间】:2018-12-28 11:31:46 【问题描述】:在我的程序中,我在轴上生成了一个烛台,在 axix2 中生成了一个数据图。 对于烛台图,我创建了一个名为 data 的变量。它是python字典类型变量。我在哪里指定日期 .'date': ['2018/10/30', '2018/11/03', '2018/11/04', '2018/11/05', '2018/11 /07', '2018/11/10', '2018/11/11']. 其他键是 'open'、'high'、'low'、'close' 和 'volume'这里日期 '2018/10/30' 和 '2018/11/03' 有一个差距日期。 这里还有日期 '2018/11/07' 和 '2018/11/10' 日期有差距。 因此,当我生成烛台图时,缺少日期空间。我怎样才能删除这个缺少的日期空间。 我的程序:
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_())
输出:
【问题讨论】:
【参考方案1】:我已经解决了“从图形 matplotlib 中删除缺少的日期空间和 xtricklabel”这样,在我的数据数组中,我用从 0 开始的整数替换了日期。我还用时间设置了 xtricklabel。
我的解决方案代码:
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_())
输出是:
【讨论】:
以上是关于从图形 matplotlib 烛台中删除缺少的日期空间和技巧标签的主要内容,如果未能解决你的问题,请参考以下文章
使用Matplotlib v2.2.2的烛台图表? [重复]