問題描述
我想在同一個 qchart 上繪制燭臺和 5 天平均線,它應該顯示 一個 x 軸,但給出 兩個 x 軸.這是代碼和情節.
導入系統從 PyQt5.QtChart 導入(QCandlestickSeries、QChart、QChartView)從 PyQt5.QtWidgets 導入 QApplication、QMainWindow從 PyQt5.QtCore 導入 Qt從 PyQt5 導入 QtChart 作為 qc"""要加載的數據如下所示:num, 打開, 高, 低, 關閉, ma51 7380 7520 7380 7510 73242 7520 7580 7410 7440 73723 7440 7650 7310 7520 74344 7450 7640 7450 7550 74805 7510 7590 7460 7490 75026 7500 7590 7480 7560 75127 7560 7830 7540 7800 7584……"""應用程序 = QApplication(sys.argv)#系列 = QCandlestickSeries()series.setDncreasingColor(Qt.red)series.setIecreasingColor(Qt.green)ma5 = qc.QLineSeries() # 5天平均數據線candle_x_axis_label = [] # 存儲str類型數據# 在一個循環中,series 和 ma5 追加對應的數據對于數據中的 num、o、h、l、c、m:series.append(QCandlestickSet(o, h, l, c))ma5.append(m)蠟燭x軸標簽附加(str(num))圖表 = QChart()chart.addSeries(series) # 蠟燭圖chart.addSeries(ma5) # ma5 線chart.setAnimationOptions(QChart.SeriesAnimations)chart.createDefaultAxes()chart.legend().hide()# 這里是重點# tm 是 str 列表,就像 '1, 2, 3, ..., n'chart.axes(Qt.Horizo??ntal)[0].setCategories(candle_x_axis_label)#chartview = QChartView(圖表)ui = QMainWindow()ui.setGeometry(50, 50, 500, 300)ui.setCentralWidget(圖表視圖)ui.show()sys.exit(app.exec_())
和劇情
我查看qtchart代碼,發現燭臺x軸的類類型是QBarCategoryAxis,而ma5 x軸的類類型是QValueAxis.那么如何去除兩個x軸呢?
你只需要獲取QLineSeries
的X軸對應的QAbstractAxis
并用
I want to plot candlestick and 5-days average line on the same qchart, it should show one x axis, but gives two x axis. here is the code and the plot.
import sys
from PyQt5.QtChart import (QCandlestickSeries, QChart, QChartView)
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt
from PyQt5 import QtChart as qc
"""
data to be load just like the following:
num, open, high, low, close, ma5
1 7380 7520 7380 7510 7324
2 7520 7580 7410 7440 7372
3 7440 7650 7310 7520 7434
4 7450 7640 7450 7550 7480
5 7510 7590 7460 7490 7502
6 7500 7590 7480 7560 7512
7 7560 7830 7540 7800 7584
... ...
"""
app = QApplication(sys.argv)
#
series = QCandlestickSeries()
series.setDncreasingColor(Qt.red)
series.setIecreasingColor(Qt.green)
ma5 = qc.QLineSeries() # 5-days average data line
candle_x_axis_label = [] # stores str type data
# in a loop, series and ma5 append corresponding data
for num, o, h, l, c, m in data:
series.append(QCandlestickSet(o, h, l, c))
ma5.append(m)
candle_x_axis_label.append(str(num))
chart = QChart()
chart.addSeries(series) # candle
chart.addSeries(ma5) # ma5 line
chart.setAnimationOptions(QChart.SeriesAnimations)
chart.createDefaultAxes()
chart.legend().hide()
# here is the point
# tm is str list, just like '1, 2, 3, ..., n'
chart.axes(Qt.Horizontal)[0].setCategories(candle_x_axis_label)
#
chartview = QChartView(chart)
ui = QMainWindow()
ui.setGeometry(50, 50, 500, 300)
ui.setCentralWidget(chartview)
ui.show()
sys.exit(app.exec_())
and the plot
i check qtchart code, and find out the class type of candlestick x axis is QBarCategoryAxis but the class type of ma5 x axis is QValueAxis. so how to remove of the two x axis?
You only have to obtain the QAbstractAxis
corresponding to the X axis of the QLineSeries
and hide it with setVisible(False)
, for this you must use QChart::axisX()
:
chart.axisX(ma5).setVisible(False)
Complete Code:
import sys
from PyQt5.QtChart import QCandlestickSeries, QChart, QChartView, QCandlestickSet
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt, QPointF
from PyQt5 import QtChart as qc
data = ((1, 7380, 7520, 7380, 7510, 7324),
(2, 7520, 7580, 7410, 7440, 7372),
(3, 7440, 7650, 7310, 7520, 7434),
(4, 7450, 7640, 7450, 7550, 7480),
(5, 7510, 7590, 7460, 7490, 7502),
(6, 7500, 7590, 7480, 7560, 7512),
(7, 7560, 7830, 7540, 7800, 7584))
app = QApplication(sys.argv)
#
series = QCandlestickSeries()
series.setDecreasingColor(Qt.red)
series.setIncreasingColor(Qt.green)
ma5 = qc.QLineSeries() # 5-days average data line
tm = [] # stores str type data
# in a loop, series and ma5 append corresponding data
for num, o, h, l, c, m in data:
series.append(QCandlestickSet(o, h, l, c))
ma5.append(QPointF(num, m))
tm.append(str(num))
chart = QChart()
chart.addSeries(series) # candle
chart.addSeries(ma5) # ma5 line
chart.setAnimationOptions(QChart.SeriesAnimations)
chart.createDefaultAxes()
chart.legend().hide()
chart.axisX(series).setCategories(tm)
chart.axisX(ma5).setVisible(False)
chartview = QChartView(chart)
ui = QMainWindow()
ui.setGeometry(50, 50, 500, 300)
ui.setCentralWidget(chartview)
ui.show()
sys.exit(app.exec_())
這篇關于在同一 qtchart 上繪制燭臺和 5 天平均線,但給出兩個 x 軸圖的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!