gpt4 book ai didi

python - 如何获取 QtChart QLineSeries 中的轴标签

转载 作者:行者123 更新时间:2023-12-01 00:07:16 25 4
gpt4 key购买 nike

我能够使用 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。

这就是我得到的:

This is what I get:

这就是我想要的(向我的轴添加标签):

enter image description here

我似乎在文档中找不到如何执行此操作...任何人都可以帮助我吗?

最佳答案

您必须使用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>
# ...

enter image description here

关于python - 如何获取 QtChart QLineSeries 中的轴标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59884038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com