九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
PyQt5 繪制柱狀圖(QPainter)

PyQt5 繪制柱狀圖(QPainter)

我們使用Qpainter根據(jù)像素點繪制,像素點的計算與數(shù)學(xué)中的坐標(biāo)軸類似但也有不同的地方。最主要的是它的原點坐標(biāo)不在左下方而是在右上方,如圖

# pointy - end_Pox 表示柱狀圖在Y方向使用了多少像素點,num_start - num_Spacing 表示柱狀圖
# 數(shù)據(jù)的最大范圍 兩者相除得出每個刻度對應(yīng)幾個像素點
every_pox = (pointy - end_Pox) / (num_start - num_Spacing)
# 獲取數(shù)值并計算出需要的像素 由于Y的坐標(biāo)是從上往下算的我們可以用柱狀圖中的原點像素(pointy)減去
# 計算出來的像素
pointy - Y[i][3] * every_pox
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys


class MainWindows(QWidget):
    def __init__(self):
        super(MainWindows, self).__init__()
        self.resize(700,500)

    def paintEvent(self, QPaintEvent):
        X = [1, 2, 3, 4, 5, 6]
        Y = [[141, 400, 700, 460], [120, 50, 340, 55], [141, 400, 700, 460], [120, 50, 340, 55], [120, 50, 340, 55],
             [141, 400, 700, 460]]


        painter = QPainter()
        painter.begin(self)
        painter.setPen(Qt.NoPen)
        painter.setRenderHint(QPainter.Antialiasing)
        pen = QPen(Qt.black, 2, Qt.SolidLine)
        painter.setPen(pen)
        pointx = 35
        pointy = self.height() - 50

        width = self.width() - 150 - pointx
        height = self.height()
        # X軸
        painter.drawLine(pointx, pointy, width + pointx, pointy)
        # Y軸
        painter.drawLine(pointx, pointy - height, pointx, pointy)

        X_Spacing = width / len(X)
        X_little_Spacing = width / len(X) / (len(Y[1]) + 1)

        Y_Spacing = height / len(Y)

        pen = QPen(Qt.red, 5, Qt.SolidLine)
        painter.setPen(pen)
        X_start = X_little_Spacing * 3 + 35
        X_pox = []
        for i in range(len(X)):
            painter.drawPoint(X_start - X_little_Spacing * 2, pointy)
            painter.drawPoint(X_start - X_little_Spacing, pointy)
            painter.drawPoint(X_start, pointy)
            painter.drawPoint(X_start + X_little_Spacing * 2, pointy)
            painter.drawPoint(X_start + X_little_Spacing, pointy)
            painter.drawText(X_start - 15, pointy + 20, str(X[i]))
            X_pox.append(X_start)

            X_start = X_start + X_Spacing
        # print(X_pox)
        Y_max = 0
        for i in range(len(Y)):
            if Y_max < max(Y[i]):
                Y_max = max(Y[i])
        num_Spacing = int(Y_max / (len(Y) - 1)) + 1

        num_start = 0
        Y_start_pox = 0
        end_Pox = 0
        for i in range(len(Y)):
            painter.drawPoint(35, pointy - Y_start_pox)
            end_Pox = pointy - Y_start_pox
            painter.drawText(pointx - 30, pointy - Y_start_pox + 5, str(num_start))
            Y_start_pox = Y_start_pox + Y_Spacing
            num_start = num_start + num_Spacing
        every_pox = (pointy - end_Pox) / (num_start - num_Spacing)

        pen = QPen(Qt.black, 1, Qt.SolidLine)
        painter.setPen(pen)
        print(X_pox)
        for i in range(len(X)):
            brush = QBrush(Qt.Dense1Pattern)
            painter.setBrush(brush)
            painter.drawText(X_pox[i] - X_little_Spacing * 2, pointy - Y[i][0] * every_pox - 5, str(Y[i][0]))
            painter.drawRect(X_pox[i] - X_little_Spacing * 2, pointy - Y[i][0] * every_pox, X_little_Spacing,
                             Y[i][0] * every_pox)
            brush = QBrush(Qt.DiagCrossPattern)
            painter.setBrush(brush)
            painter.drawText(X_pox[i] - X_little_Spacing, pointy - Y[i][1] * every_pox - 5, str(Y[i][1]))
            painter.drawRect(X_pox[i] - X_little_Spacing, pointy - Y[i][1] * every_pox, X_little_Spacing,
                             Y[i][1] * every_pox)
            brush = QBrush(Qt.Dense2Pattern)
            painter.setBrush(brush)
            painter.drawText(X_pox[i], pointy - Y[i][2] * every_pox - 5, str(Y[i][2]))
            painter.drawRect(X_pox[i], pointy - Y[i][2] * every_pox, X_little_Spacing, Y[i][2] * every_pox)
            brush = QBrush(Qt.Dense3Pattern)
            painter.setBrush(brush)
            painter.drawText(X_pox[i] + X_little_Spacing, pointy - Y[i][3] * every_pox - 5, str(Y[i][3]))
            painter.drawRect(X_pox[i] + X_little_Spacing, pointy - Y[i][3] * every_pox, X_little_Spacing,
                             Y[i][3] * every_pox)

        brush = QBrush(Qt.Dense1Pattern)
        painter.setBrush(brush)
        painter.drawRect(self.width() - 140, 10, 50, 20)
        painter.drawText(self.width() - 80, 25, "Dense1Pattern")

        brush = QBrush(Qt.DiagCrossPattern)
        painter.setBrush(brush)
        painter.drawRect(self.width() - 140, 35, 50, 20)
        painter.drawText(self.width() - 80, 50, "DiagCrossPattern")

        brush = QBrush(Qt.Dense2Pattern)
        painter.setBrush(brush)
        painter.drawRect(self.width() - 140, 60, 50, 20)
        painter.drawText(self.width() - 80, 75, "Dense2Pattern")

        brush = QBrush(Qt.Dense3Pattern)
        painter.setBrush(brush)
        painter.drawRect(self.width() - 140, 85, 50, 20)
        painter.drawText(self.width() - 80, 100, "Dense3Pattern")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    demo = MainWindows()
    demo.show()
    sys.exit(app.exec_())

效果圖如下:

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
python – QWidget上的QPixmap上的繪圖點(pyqt5)
練習(xí)QPainter,運(yùn)行時看不到圖形,只有一個灰色窗口
VBA中的尺寸單位
國內(nèi)主要地圖瓦片坐標(biāo)系定義及計算原理 | CntChen Blog
Fastreport使用經(jīng)驗(轉(zhuǎn))在Delphi程序中訪問報表對象
五步教你制作漂亮精致的HTML時鐘
更多類似文章 >>
生活服務(wù)
熱點新聞
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服