如何在 QtChart QLineSeries 中获取轴标签
How do I get the axis labels in QtChart QLineSeries
我可以使用 Python 中的 QtCharts 库生成图表,但我似乎无法弄清楚如何向我的图表添加轴标签...
我看到了 QValueAxis 的 setLabelFormat() 功能,但没有关于如何设置文本本身的内容。我确定我缺少一些简单的东西。
这是生成图表的代码:
def plotLossesChart(self):
set0 = QBarSet('Conduction Loss')
set1 = QBarSet('Switching Loss')
set0.append([random.randint(0, 10) for i in range(5)])
set1.append([random.randint(0, 10) for i in range(5)])
series = QStackedBarSeries()
series.append(set0)
series.append(set1)
chart = QChart()
chart.addSeries(series)
chart.setTitle('Loss Summary')
chart.setAnimationOptions(QChart.SeriesAnimations)
months = ('Upper FET', 'Lower FET', 'Driver', 'Inductor', 'Cap ESR')
axisX = QBarCategoryAxis()
axisX.append(months)
axisY = QValueAxis()
axisY.setRange(0, 15)
chart.addAxis(axisX, Qt.AlignBottom)
chart.addAxis(axisY, Qt.AlignLeft)
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
return chart
def plotEfficiencyChart(self):
series = QLineSeries()
load = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30]
eff = [0, 0.5726, 0.7321, 0.7978, 0.8398, 0.8616, 0.8798,
0.8903, 0.9002, 0.9062, 0.9127, 0.9267, 0.9379, 0.9430, 0.9448]
for i, e in zip(load, eff):
series.append(QPointF(float(i), float(e)*100))
chart = QChart()
chart.addSeries(series)
chart.setTitle('Efficiency vs. Load')
chart.setAnimationOptions(QChart.SeriesAnimations)
axisX = QValueAxis()
axisX.setRange(0, 30)
axisX.setLabelFormat("%.1f")
axisX.setTickCount(7)
axisY = QValueAxis()
axisY.setRange(0, 100)
axisY.setLabelFormat("%d")
axisY.setMinorTickCount(5)
chart.addAxis(axisX, Qt.AlignBottom)
chart.addAxis(axisY, Qt.AlignLeft)
series.attachAxis(axisX)
series.attachAxis(axisY)
chart.legend().setVisible(False)
return chart
在主程序中,我将图表添加到 ChartView 并将其附加到 QGridLayout。
这是我得到的:
这就是我想要的(为我的轴添加标签):
我似乎无法在文档中找到如何执行此操作...有人可以帮助我吗?
你必须使用轴的setTitleText()
方法:
def plotLossesChart(self):
# ...
axisY = QValueAxis()
axisY.setRange(0, 15)
<b>axisY.setTitleText("Loss (W)")</b>
# ...
def plotEfficiencyChart(self):
# ...
axisX = QValueAxis()
axisX.setRange(0, 30)
axisX.setLabelFormat("%.1f")
axisX.setTickCount(7)
<b>axisX.setTitleText("Load (A)")</b>
axisY = QValueAxis()
axisY.setRange(0, 100)
axisY.setLabelFormat("%d")
axisY.setMinorTickCount(5)
<b>axisY.setTitleText("Efficiency (%)")</b>
# ...
我可以使用 Python 中的 QtCharts 库生成图表,但我似乎无法弄清楚如何向我的图表添加轴标签...
我看到了 QValueAxis 的 setLabelFormat() 功能,但没有关于如何设置文本本身的内容。我确定我缺少一些简单的东西。
这是生成图表的代码:
def plotLossesChart(self):
set0 = QBarSet('Conduction Loss')
set1 = QBarSet('Switching Loss')
set0.append([random.randint(0, 10) for i in range(5)])
set1.append([random.randint(0, 10) for i in range(5)])
series = QStackedBarSeries()
series.append(set0)
series.append(set1)
chart = QChart()
chart.addSeries(series)
chart.setTitle('Loss Summary')
chart.setAnimationOptions(QChart.SeriesAnimations)
months = ('Upper FET', 'Lower FET', 'Driver', 'Inductor', 'Cap ESR')
axisX = QBarCategoryAxis()
axisX.append(months)
axisY = QValueAxis()
axisY.setRange(0, 15)
chart.addAxis(axisX, Qt.AlignBottom)
chart.addAxis(axisY, Qt.AlignLeft)
chart.legend().setVisible(True)
chart.legend().setAlignment(Qt.AlignBottom)
return chart
def plotEfficiencyChart(self):
series = QLineSeries()
load = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30]
eff = [0, 0.5726, 0.7321, 0.7978, 0.8398, 0.8616, 0.8798,
0.8903, 0.9002, 0.9062, 0.9127, 0.9267, 0.9379, 0.9430, 0.9448]
for i, e in zip(load, eff):
series.append(QPointF(float(i), float(e)*100))
chart = QChart()
chart.addSeries(series)
chart.setTitle('Efficiency vs. Load')
chart.setAnimationOptions(QChart.SeriesAnimations)
axisX = QValueAxis()
axisX.setRange(0, 30)
axisX.setLabelFormat("%.1f")
axisX.setTickCount(7)
axisY = QValueAxis()
axisY.setRange(0, 100)
axisY.setLabelFormat("%d")
axisY.setMinorTickCount(5)
chart.addAxis(axisX, Qt.AlignBottom)
chart.addAxis(axisY, Qt.AlignLeft)
series.attachAxis(axisX)
series.attachAxis(axisY)
chart.legend().setVisible(False)
return chart
在主程序中,我将图表添加到 ChartView 并将其附加到 QGridLayout。
这是我得到的:
这就是我想要的(为我的轴添加标签):
我似乎无法在文档中找到如何执行此操作...有人可以帮助我吗?
你必须使用轴的setTitleText()
方法:
def plotLossesChart(self):
# ...
axisY = QValueAxis()
axisY.setRange(0, 15)
<b>axisY.setTitleText("Loss (W)")</b>
# ...
def plotEfficiencyChart(self):
# ...
axisX = QValueAxis()
axisX.setRange(0, 30)
axisX.setLabelFormat("%.1f")
axisX.setTickCount(7)
<b>axisX.setTitleText("Load (A)")</b>
axisY = QValueAxis()
axisY.setRange(0, 100)
axisY.setLabelFormat("%d")
axisY.setMinorTickCount(5)
<b>axisY.setTitleText("Efficiency (%)")</b>
# ...