Python——pyside6中Matplotlib视图动态获取数据并绘制图表+美化

心已赠人 2024-05-06 23:25 67阅读 0赞

目录

一、前言

二、找到了相关美化文章

三、创建一个绘图线程QThread

四、美化的图表demo

4.1、赛博朋克风格

4.2、其他风格仓库地址


一、前言

  • 用pyside6需要一个子线程,来动态的绘制出计算结果
  • 而且原来的图表不好看,需要进行美化!!

二、找到了相关美化文章

4个Python库来美化你的Matplotlib图表! - 知乎大家好,我是小F~ Matplotlib是一个被广泛使用的Python数据可视化库,相信很多人都使用过。 但是有时候总会觉得,Matplotlib做出来的图表不是很好看、不美观。 今天小F就给大家分享四个美化Matplotlib图表的Pytho…apple-touch-icon-152.81060cab.pnghttps://zhuanlan.zhihu.com/p/624890496

三、创建一个绘图线程QThread

  1. # -*- coding: utf-8 -*-
  2. # @Author : pan
  3. import time
  4. from PySide6.QtCore import QThread, Signal
  5. import matplotlib.pyplot as plt
  6. # mplcyberpunk不可去掉!
  7. import mplcyberpunk
  8. import matplotlib
  9. matplotlib.use('TkAgg')
  10. class WorkerThread(QThread):
  11. finished = Signal()
  12. count_signal = Signal() # 数据信号
  13. def __init__(self):
  14. super().__init__()
  15. self.is_stopped = True
  16. self.is_continue = True
  17. self.is_close = True
  18. # 线程执行
  19. def run(self):
  20. self.is_stopped = False
  21. self.is_continue = False
  22. self.is_close = False
  23. # 添加样式 赛博朋克
  24. plt.style.use("cyberpunk")
  25. # plt显示中文
  26. plt.rcParams['font.sans-serif'] = ['SimHei']
  27. # 隐藏默认的工具栏
  28. plt.rcParams['toolbar'] = 'None'
  29. plt.figure("MTSP系统动态图表")
  30. fig = plt.gcf()
  31. # 注册窗口关闭事件的回调函数
  32. fig.canvas.mpl_connect("close_event", self.on_close)
  33. while True:
  34. # 终止信号
  35. if self.is_stopped:
  36. plt.show()
  37. return
  38. # 如果暂停
  39. if self.is_continue:
  40. time.sleep(1)
  41. continue
  42. # 如果关闭窗口
  43. if self.is_close:
  44. return
  45. # 清除当前坐标轴上的绘图内容,保留其他设置
  46. plt.cla()
  47. # 动态数据
  48. from classes.yolo import y_axis_count_graph as y
  49. # from classes.yolo import x_axis_time_graph as x
  50. plt.xlabel('时间')
  51. plt.ylabel('车流量/辆')
  52. plt.title('实时流量折线图')
  53. plt.plot(y, linestyle='-', marker='o')
  54. # plt.plot(y, ls='-', marker='o', mec='skyblue', mfc='white', color="skyblue")
  55. # 发光效果+渐变填充
  56. mplcyberpunk.add_gradient_fill(alpha_gradientglow=0.5, gradient_start='zero')
  57. plt.xticks([])
  58. plt.pause(2)
  59. plt.show()
  60. try:
  61. plt.close()
  62. return
  63. except:
  64. plt.close()
  65. pass
  66. # 窗口关闭方法
  67. def on_close(self, event):
  68. self.is_close = True
  69. # 停止方法
  70. def stop(self):
  71. self.is_stopped = True
  72. # 暂停方法
  73. def pause(self):
  74. self.is_continue = True
  75. # 继续方法
  76. def run_continue(self):
  77. self.is_continue = False

以上方法,仅供参考

⚠️踩坑点:

1、画图之后,要用cla()方法来清空图表内容!(否则会重叠!)

2、**线程开启后,关闭窗口,还会让窗口弹窗!(用fig的窗口关闭回调函数解决!)**

  1. # 清除当前坐标轴上的绘图内容,保留其他设置
  2. plt.cla()

?美化方法:

1、隐藏默认的工具栏

2、设置自己喜欢的样式

3、设置中文字体

4、当x轴数据过多时,隐藏其内容


在主线程中,创建线程即可!

根据主线程的设置,可以控制画图线程的开启、暂停、继续、终止

  1. # 画图线程
  2. self.draw_thread = WorkerThread()
  3. # 线程开启
  4. self.draw_thread.start()
  5. # 折线图继续
  6. self.draw_thread.run_continue()
  7. # 折线图暂停
  8. self.draw_thread.pause()
  9. # 停止画图线程
  10. if self.draw_thread.isRunning():
  11. print("画图线程退出")
  12. self.draw_thread.stop()
  13. # 退出画图线程
  14. self.draw_thread.quit()

四、美化的图表demo

4.1、赛博朋克风格

  1. # 安装库
  2. pip install mplcyberpunk

项目地址:https://github.com/dhaitz/mplcyberpunk

cf7416cb22b64d699df120bc040a7c46.png

  1. import matplotlib.pyplot as plt
  2. import mplcyberpunk
  3. # 添加样式 赛博朋克
  4. plt.style.use("cyberpunk")
  5. # plt显示中文
  6. plt.rcParams['font.sans-serif'] = ['SimHei']
  7. # 隐藏默认的工具栏
  8. plt.rcParams['toolbar'] = 'None'
  9. plt.figure("MTSP系统动态图表")
  10. plt.xlabel('时间')
  11. plt.ylabel('车流量/辆')
  12. plt.title('实时流量折线图')
  13. plt.plot([1, 3, 9, 5, 2, 1, 1], marker='o')
  14. plt.plot([4, 5, 5, 7, 9, 8, 6], marker='o')
  15. mplcyberpunk.add_glow_effects()
  16. plt.show()

4.2、其他风格仓库地址

GitHub - nschloe/matplotx: :bar_chart: More styles and useful extensions for Matplotlib:bar_chart: More styles and useful extensions for Matplotlib - GitHub - nschloe/matplotx: :bar_chart: More styles and useful extensions for Matplotlibfluidicon.pnghttps://github.com/nschloe/matplotxb34a40906f024765a78d475b2a05c617.png
https://github.com/quantumblacklabs/qbstylesicon-default.png?t=N6B9https://github.com/quantumblacklabs/qbstyles2e860dd9291042d9ae8918a1d2fe27e4.png

科学绘图

GitHub - garrettj403/SciencePlots: Matplotlib styles for scientific plottingMatplotlib styles for scientific plotting. Contribute to garrettj403/SciencePlots development by creating an account on GitHub.fluidicon.pnghttps://github.com/garrettj403/SciencePlots228f13a74648414c91ec39fa7d1774c3.png

发表评论

表情:
评论列表 (有 0 条评论,67人围观)

还没有评论,来说两句吧...

相关阅读