问题
From time to time I returned to the code that eyllanesc helped me with.
Need to display every 6 placemarks on the axis. It would also be better to display datetime(date)
[datetime.date(1991, 2, 4) datetime.date(1991, 2, 5)
datetime.date(1991, 2, 6) ... datetime.date(2019, 7, 31)
datetime.date(2019, 8, 1) datetime.date(2019, 8, 2)]
instead of indexes
(0, 1, 2, 3....70)
When displaying datetime data in C#, I used string data, since on weekends datetime was also displayed and empty spaces on the axis Y were obtained. Python will probably have the same data datetime problems. The data file is located here file
I displayed what I wanted to do in the image.
P.S. Made the code shorter for better understanding.
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
from PyQt5.QtCore import Qt
import math
import numpy as np
import pandas as pd
df = pd.read_csv('file.txt',
index_col='DATE',
parse_dates=True,
infer_datetime_format=True)
date = df.iloc[:, 0].index.date
o = df.iloc[:, 0].values
h = df.iloc[:, 1].values
l = df.iloc[:, 2].values
z = df.iloc[:, 3].values
x = len(z)
x_ = x - 1
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.step = 30
self._chart_view = QtChart.QChartView()
self.scrollbar = QtWidgets.QScrollBar(
QtCore.Qt.Horizontal,
sliderMoved=self.onAxisSliderMoved,
pageStep=self.step,
)
self.scrollbar.setRange(0, x_)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
for w in (self._chart_view, self.scrollbar):
lay.addWidget(w)
self._chart = QtChart.QChart()
self._candlestick_serie = QtChart.QCandlestickSeries()
self._candlestick_serie.setDecreasingColor(Qt.red)
self._candlestick_serie.setIncreasingColor(Qt.green)
tm = []
for i in range(0, len(z)):
o_ = o[i]
h_ = h[i]
l_ = l[i]
c_ = z[i]
self._candlestick_serie.append(QtChart.QCandlestickSet(o_, h_, l_, c_))
tm.append(str(i))
min_x, max_x = 0, x_
self._chart.addSeries(self._candlestick_serie)
self._chart.createDefaultAxes()
self._chart.legend().hide()
self._chart.axisX(self._candlestick_serie).setCategories(tm)
self._chart_view.setChart(self._chart)
self.lims = np.array([min_x, max_x])
self.onAxisSliderMoved(self.scrollbar.value())
self.adjust_axes(1, 30)
def adjust_axes(self, value_min, value_max):
if value_min > 0 and value_max > 0 and value_max <= x_ and value_max > value_min:
self._chart.axisX(self._candlestick_serie).setRange(str(value_min), str(value_max))
@QtCore.pyqtSlot(int)
def onAxisSliderMoved(self, value):
value2 = value + self.step
value1 = value
if value2 >= x_:
value2 = x_
value1 = value2 - self.step
self.adjust_axes(math.floor(value1), math.ceil(value2))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I tried to determine the axes separately, but nothing comes out. Code below.
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
from PyQt5.QtCore import Qt
from PyQt5.QtChart import *
import math
import numpy as np
import pandas as pd
import datetime
df = pd.read_csv('file.txt',
index_col='DATE',
parse_dates=True,
infer_datetime_format=True)
date = df.iloc[:, 0].index.date
o = df.iloc[:, 0].values
h = df.iloc[:, 1].values
l = df.iloc[:, 2].values
z = df.iloc[:, 3].values
x = len(z)
x_ = x - 1
qt = [None] * x
for i in range(0, x):
qt[i] = (date[i].strftime("%Y/%m/%d"))
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.step = 30
self._chart_view = QtChart.QChartView()
self.scrollbar = QtWidgets.QScrollBar(
QtCore.Qt.Horizontal,
sliderMoved=self.onAxisSliderMoved,
pageStep=self.step,
)
self.scrollbar.setRange(0, x_)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QVBoxLayout(central_widget)
for w in (self._chart_view, self.scrollbar):
lay.addWidget(w)
self._chart = QtChart.QChart()
self._candlestick_serie = QtChart.QCandlestickSeries()
for i in range(0, len(z)):
o_ = o[i]
h_ = h[i]
l_ = l[i]
c_ = z[i]
_time = qt[i]
self._candlestick_serie.append(QtChart.QCandlestickSet(o_, h_, l_, c_, _time))
min_x, max_x = 0, x_
self._chart.addSeries(self._candlestick_serie)
self._chart.legend().hide()
#self._chart.createDefaultAxes()
axisX = QDateTimeAxis()
axisX.setTickCount(5)
axisX.setFormat("dd.MM.yyyy")
self._chart.addAxis(axisX, Qt.AlignBottom)
self._candlestick_serie.attachAxis(axisX)
# Y Axis Settings
axisY = QValueAxis()
axisY.setLabelFormat("%i")
self._chart.addAxis(axisY, Qt.AlignLeft)
self._candlestick_serie.attachAxis(axisY)
self._chart_view.setChart(self._chart)
self.lims = np.array([min_x, max_x])
self.onAxisSliderMoved(self.scrollbar.value())
self.adjust_axes(1, 31)
#self._chart.axisX(self._candlestick_serie).setTickCount(7)
def adjust_axes(self, value_min, value_max):
if value_min > 0 and value_max > 0 and value_max <= x_ and value_max > value_min:
self._chart.axisX(self._candlestick_serie).setRange(str(value_min), str(value_max))
@QtCore.pyqtSlot(int)
def onAxisSliderMoved(self, value):
value2 = value + self.step
value1 = value
if value2 >= x_:
value2 = x_
value1 = value2 - self.step
self.adjust_axes(math.floor(value1), math.ceil(value2))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Matplotlib has an example where string values are set instead of indexes enter link description here
来源:https://stackoverflow.com/questions/62204034/how-not-to-display-all-x-axis-coordinate-labels-pyqt5