matplotlib 散点图改变选点的颜色

发布时间:2024年01月22日
# -*- coding: utf-8 -*-
'''
TODO:LQD
'''
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FC
from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow, QVBoxLayout, QWidget
import traceback 
data="""YSBS20255b-2009-1  0.155   0.1536  0.162725172
YSBS20255b-2009-2  0.43    0.4327  0.458406131
YSBS20255b-2009-3  0.67    0.6538  0.692641388
YSBS20255b-2009-5  0.0028  0.0027  0.002860403
YSBS20255b-2009-6  0.142   0.1394  0.147681568
YSBS20255b-2009-7  0.664   0.6183  0.65503238
GSB03-2453-2008-2  0.256   0.2409  0.255211548
GSB03-2453-2008-4  0.085   0.0849  0.089943796
YSBS11279-2000  0.803   0.7395  0.783432711
9902-GBW(E)010137   1.21    1.1099  1.175837682
Q235-YSBS11173a-2007    0.188   0.1942  0.205737164
16MnV5-YSBC35201-97-211 0.171   0.1646  0.174378667
Q345b-YSBS11264a-2011   0.158   0.1593  0.168763801
YSBS11078d-2012    0.0018  0.0016  0.001695054
BS1020 0.21    0.2138  0.226501573
GCr15-YSBS11273b-2007   0.999   0.962   1.01915114"""
data=data.split("\n")
for i in range(len(data)):
    data[i]=data[i].split(" ")
    data[i] = list(filter(lambda x: x!="", data[i]))
std=[]
measure=[]
yfited=[]
for i in range(len(data)):
    std.append(float(data[i][1]))
    measure.append(float(data[i][2]))
    yfited.append(float(data[i][3]))
colors = [50 for one in std]
print(measure,std,yfited) 
selected=[]
class QtDraw(QMainWindow):
    flag_btn_start = True
 
    def __init__(self):
        super().__init__()
        self.init_ui()
        self.slot_btn_start()
    def init_ui(self):
        self.resize(800, 600)
        self.setWindowTitle('PyQt5 Draw')
 
        # TODO:这里是结合的关键
        self.fig = plt.Figure()
        self.canvas = FC(self.fig)
        self.btn_start = QPushButton(self)
        self.btn_start.setText('draw')
        self.btn_start.clicked.connect(self.slot_btn_start)
 
        widget = QWidget()
        layout = QVBoxLayout()
        layout.addWidget(self.canvas)
        layout.addWidget(self.btn_start)
        widget.setLayout(layout)
        self.setCentralWidget(widget)
        self.fig.canvas.mpl_connect('pick_event', self.onpick)

    def onpick(self,event):
        global selected,colors
        thisline = event.artist
        print(dir(thisline),thisline)
        # xdata = thisline.get_xdata()
        # ydata = thisline.get_ydata()
        ind = event.ind
        print(ind)
        colors[ind[0]]=80
        # print(dir(thisline),thisline)
        # points = tuple(zip(xdata[ind], ydata[ind]))
        # thisline.set_alpha(0.5)
        # for point in points:
        #     if point in selected:
        #         pass
        #     else:
        #         selected.append(point)
        # print(selected)
        # self.fig.canvas.draw()
        self.slot_btn_start()
    def slot_btn_start(self):
        try:
            ax = self.fig.add_subplot(111)
            ax.cla()  # TODO:删除原图,让画布上只有新的一次的图
            # ax.plot(measure,std,"o")
            ax.scatter(measure, std,c=colors, cmap='viridis',picker=5)
            ax.plot(measure,yfited)
            # cbbar=ax.scatter(x,y,'-o')
            # self.fig.colorbar(cbbar)
            self.canvas.draw()  # TODO:这里开始绘制
        except Exception as e:
            traceback.print_exc()
            print(e)
 
 
def ui_main():
    app = QApplication(sys.argv)
    w = QtDraw()
    w.show()
    sys.exit(app.exec_())
 
 
if __name__ == '__main__':
    ui_main()

文章来源:https://blog.csdn.net/mahongquan/article/details/135752732
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。