1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| import sys, math, random
from PyQt5.QtWidgets import QApplication, QGridLayout, QWidget from PyQt5.QtGui import QPen, QColor, QFont, QBrush from PyQt5.QtCore import Qt, QLocale from QCustomPlot_PyQt5 import QCustomPlot, QCPGraph, QCPScatterStyle, QCPTextElement, QCPAxisTickerPi, QCPErrorBars
class MainForm(QWidget):
def __init__(self) -> None: super().__init__()
self.setWindowTitle("多轴演示") self.resize(600,400)
self.customPlot = QCustomPlot(self) self.gridLayout = QGridLayout(self).addWidget(self.customPlot)
self.customPlot.setLocale(QLocale(QLocale.English, QLocale.UnitedKingdom)) self.customPlot.legend.setVisible(True)
legendFont = self.font() legendFont.setPointSize(9) self.customPlot.legend.setFont(legendFont) self.customPlot.legend.setBrush(QBrush(QColor(255,255,255,230))) self.customPlot.axisRect().insetLayout().setInsetAlignment(0, Qt.AlignBottom|Qt.AlignRight)
self.customPlot.addGraph(self.customPlot.yAxis, self.customPlot.xAxis) self.customPlot.graph(0).setPen(QPen(QColor(255, 100, 0))) self.customPlot.graph(0).setLineStyle(QCPGraph.lsLine) self.customPlot.graph(0).setScatterStyle(QCPScatterStyle(QCPScatterStyle.ssDisc, 5)) self.customPlot.graph(0).setName("Left maxwell function")
self.customPlot.addGraph() self.customPlot.graph(1).setPen(QPen(Qt.red)) self.customPlot.graph(1).setLineStyle(QCPGraph.lsStepCenter) self.customPlot.graph(1).setScatterStyle(QCPScatterStyle(QCPScatterStyle.ssCircle, Qt.red, Qt.white, 7)) self.customPlot.graph(1).setName("Bottom maxwell function") errorBars = QCPErrorBars(self.customPlot.xAxis, self.customPlot.yAxis) errorBars.removeFromLegend() errorBars.setDataPlottable(self.customPlot.graph(1))
self.customPlot.addGraph(self.customPlot.xAxis2, self.customPlot.yAxis2) self.customPlot.graph(2).setPen(QPen(Qt.blue)) self.customPlot.graph(2).setName("High frequency sine")
self.customPlot.addGraph(self.customPlot.xAxis2, self.customPlot.yAxis2) blueDotPen = QPen(QColor(30, 40, 255, 150)) blueDotPen.setStyle(Qt.DotLine) blueDotPen.setWidthF(4) self.customPlot.graph(3).setPen(blueDotPen) self.customPlot.graph(3).setName("Sine envelope")
self.customPlot.addGraph(self.customPlot.yAxis2, self.customPlot.xAxis2) self.customPlot.graph(4).setPen(QPen(QColor(50, 50, 50, 255))) self.customPlot.graph(4).setLineStyle(QCPGraph.lsNone) self.customPlot.graph(4).setScatterStyle(QCPScatterStyle(QCPScatterStyle.ssCircle, 4)) self.customPlot.graph(4).setName("Some random data around\na quadratic function")
x0 = [3*i/25.0 for i in range(25)] y0 = [math.exp(-x*x*0.8)*(x*x+x) for x in x0] self.customPlot.graph(0).setData(x0, y0)
x1 = [3*i/15.0 for i in range(15)] y1 = [math.exp(-x*x)*(x*x)*2.6 for x in x1] y1err = [y*0.25 for y in y1] self.customPlot.graph(1).setData(x1, y1) errorBars.setData(y1err, y1err)
x2 = [i/250.0*3*math.pi for i in range(250)] y2 = [math.sin(x*12)*math.cos(x)*10 for x in x2] self.customPlot.graph(2).setData(x2, y2)
x3 = x2 y3 = [math.cos(x)*10 for x in x3] self.customPlot.graph(3).setData(x3, y3)
x4 = [i/250.0*100-50 for i in range(250)] y4 = [0.01*x*x + 1.5*(random.random()-0.5) + 1.5*math.pi for x in x4] self.customPlot.graph(4).setData(x4, y4)
self.customPlot.xAxis2.setVisible(True) self.customPlot.yAxis2.setVisible(True)
self.customPlot.xAxis.setRange(0, 2.7) self.customPlot.yAxis.setRange(0, 2.6) self.customPlot.xAxis2.setRange(0, 3.0*math.pi) self.customPlot.yAxis2.setRange(-70, 35)
self.customPlot.xAxis2.setTicker(QCPAxisTickerPi())
self.customPlot.plotLayout().insertRow(0) self.customPlot.plotLayout().addElement(0, 0, QCPTextElement(self.customPlot, "Way too many graphs in one plot", QFont("sans", 12, QFont.Bold)))
self.customPlot.xAxis.setLabel("Bottom axis with outward ticks") self.customPlot.yAxis.setLabel("Left axis label") self.customPlot.xAxis2.setLabel("Top axis label") self.customPlot.yAxis2.setLabel("Right axis label")
self.customPlot.xAxis.setTickLength(0, 5) self.customPlot.xAxis.setSubTickLength(0, 3)
self.customPlot.yAxis2.setTickLength(3, 3) self.customPlot.yAxis2.setSubTickLength(1, 1)
if __name__ == '__main__': app = QApplication(sys.argv) mainForm = MainForm() mainForm.show() sys.exit(app.exec())
|