python - QValueAxis 显示困惑的代码(中文)?

标签 python pyqt pyqt5 qchart

在我的图表中,x轴需要显示中文,y轴需要显示英文,但x轴显示困惑的代码。有人可以帮助我吗?

self.chart.createDefaultAxes()
axis_x, axis_y = self.chart.axes()
axis_x.setLabelFormat('%.2f分')
axis_y.setLabelFormat('%dmA')

它看起来像这样: enter image description here

最佳答案

QString 在 QtCharts 和 str 中使用的编码器之间似乎不兼容(甚至找出问题的原因),但我已经设法实现了一个进行转换的解决方案:

import random
from PyQt5 import QtCore, QtWidgets, QtChart


def convert(word):
    return "".join(chr(e) for e in word.encode())


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        series = QtChart.QLineSeries(name="random serie")

        for i in range(20):
            series << QtCore.QPointF(0.1 * i, random.uniform(-10, 10))

        self.chart = QtChart.QChart()
        self.chart.setTitle("Title")
        self.chart.addSeries(series)
        self.chart.createDefaultAxes()
        axis_x, axis_y = self.chart.axes()
        axis_x.setLabelFormat(convert("%.2f分"))
        axis_y.setLabelFormat("%dmA")

        view = QtChart.QChartView(self.chart)
        self.setCentralWidget(view)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    w = MainWindow()
    w.resize(640, 240)
    w.show()
    sys.exit(app.exec_())

enter image description here

关于python - QValueAxis 显示困惑的代码(中文)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58654940/

相关文章:

python - 如何在 default.conf 文件中创建数组或列表

python - QWeb检查器: How to switch tab manually

python - QIcon.fromTheme 在 PyQt 中不显示图标

python-3.x - 在哪里可以找到 PyQt5 方法签名?

python - matplotlib 中多个散点图中的意外颜色

python - 替换所有列中的缺失值,除了 pandas 数据框中的一列

python - QProgressBar 中的 MySQL 咨询

python - 如何使用 QAbstractTableModel(模型/ View )将数据设置到 QComboBox?

python - sphinx-apidoc 获取子模块,但 autodoc 没有记录它们

python - 如何将我的数据框转换为二维列表,并将所有列作为列表