我使用pyqt5绘制一个简单的图表,需要添加自定义轴,但是当我添加axis时,该图表未显示它,并且当悬停信号发出时,我需要显示相应的点,但它也未显示,它需要单击以显示。
class Demo(QChartView):
def __init__(self):
super().__init__()
self.chart = QChart()
self.setChart(self.chart)
self.setRenderHint(QPainter.Antialiasing)
axis_x = QValueAxis()
axis_x.setTickCount(10)
axis_x.setTitleText('x')
self.chart.addAxis(axis_x, Qt.AlignBottom)
axis_y= QValueAxis()
axis_y.setLinePenColor(Qt.red)
self.chart.addAxis(axis_y, Qt.AlignLeft)
series = QLineSeries()
series.setPointsVisible(True)
series.hovered.connect(self.show_tool_tip)
series << QPointF(1, 5) << QPointF(3.5, 18) << QPointF(4.8, 7.5) << QPointF(10, 2.5)
series.attachAxis(axis_x)
series.attachAxis(axis_y)
self.chart.addSeries(series)
self.value_label = QLabel(self)
def show_tool_tip(self, pt, state):
pos = self.chart.mapToPosition(pt)
if state:
self.value_label.move(int(pos.x()), int(pos.y()))
self.value_label.setText(f'{pt}')
self.value_label.show()
else:
self.value_label.hide()
如果您在控制台/ CMD中运行代码,您将获得以下命令:
Series not in the chart. Please addSeries to chart first.
Series not in the chart. Please addSeries to chart first.
这显然表明您必须先将系列添加到QChart,然后添加轴:
self.chart.addSeries(series) # first add the series
series.attachAxis(axis_x)
series.attachAxis(axis_y)
正在等待我的评论以提出解决方案。]]