我目前正在创建一个带有3个matplotlib颜色Map的Qt窗口。
这个过程是这样的:
1.用户将图的x和y值输入到GUI中(每个图将具有相同的x和y值)。
1.这些值将被转换,然后被发送到微处理器,该微处理器将与DAC和ADC两者通信(仪器“填充”发生在DAC和ADC之间的“后端”上,即频谱分析仪进行噪声测量)。
1.对于每个x和y点,我们将为每个色图接收一个z值。强调:这意味着每个子图都需要同时更新。
我目前正在编写一个脚本,当数据被获取时,它将在Qt窗口中实时绘制这些颜色图。我可以找到容易获得的资源,就如何实时绘制线图;但是,我无法找到任何颜色Map表。以下是我到目前为止拥有的代码:
import sys
import time
import random
import numpy as np
from matplotlib.backends.qt_compat import QtWidgets
from matplotlib.backends.backend_qt5agg import (
FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
# Quick note, this will need to be a widget class in the final version so it can be run from the kalamari main window
# but I think that will just change how the class is initalized
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
# set up the window
super().__init__()
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
# It seems as though this layout is what is going to allow me to
# orient all 3 subplots onto the same y axis
layout = QtWidgets.QVBoxLayout(self._main)
# create seperate canvas objects
first_canvas = FigureCanvas(Figure(figsize=(9, 6)))
layout.addWidget(NavigationToolbar(first_canvas, self))
layout.addWidget(first_canvas)
second_canvas = FigureCanvas(Figure(figsize=(9, 3)))
layout.addWidget(second_canvas)
layout.addWidget(NavigationToolbar(second_canvas, self))
# add subplots to the first canvas
self._first_axs = first_canvas.figure.subplots(1, 3)
# create data and colormap
# Here I replace X and Y with coordinate vectors which are just np.linspace
x = np.linspace(0, 10, 10)
y = np.linspace(0, 10, 10)
# For the final version you can use x for flux and y for biases and then have
# each row of z be the voltages, such that:
# z[i,j] is V(bias[j], flux[i])
# Then here is the data I will use to determine the color,
# this needs to have the same dimension as the coordinates
z = np.random.rand(10, 10)
custom_cmap = mpl.colors.LinearSegmentedColormap.from_list(
"custom", ["#00008B", "blue", "cyan", "green", "yellow", "orange", "red", "#8B0000"])
# access each subplot using regular indexing
self._first_axs[0].set_title(
'I' + u'\u209B' + u'\u2092' + u'\u209C', size=40)
self._first_axs[1].set_title(
'dI' + u'\u209B' + u'\u2092' + u'\u209C' + '/dt', size=40)
self._first_axs[2].set_title('Noise', size=40)
# plot data and create colorbars
self.plot1 = self._first_axs[0].contourf(
x, y, z, levels=20, cmap=custom_cmap)
self.plot2 = self._first_axs[1].contourf(
x, y, z, levels=20, cmap=custom_cmap)
self.plot3 = self._first_axs[2].contourf(
x, y, z, levels=20, cmap=custom_cmap)
self.cbar1 = first_canvas.figure.colorbar(
self.plot1, ax=self._first_axs[0], orientation='horizontal')
self.cbar2 = first_canvas.figure.colorbar(
self.plot2, ax=self._first_axs[1], orientation='horizontal')
self.cbar3 = first_canvas.figure.colorbar(
self.plot3, ax=self._first_axs[2], orientation='horizontal')
# make the second canvas a dynamic plot
self._second_ax = second_canvas.figure.subplots()
t = list(range(50))
self.yData = [random.randint(0, 10) for i in range(50)]
# Set up a Line2D.
self._line, = self._second_ax.plot(t, self.yData)
self._timer = second_canvas.new_timer(50)
self._timer.add_callback(self._update_canvas)
self._timer.start()
def _update_canvas(self):
t = list(range(50))
self.yData = self.yData[1:] + [random.randint(0, 10)]
# set line data
self._line.set_data(t, self.yData)
self._line.figure.canvas.draw()
if __name__ == "__main__":
# Check for open QApplication.instance()
qapp = QtWidgets.QApplication.instance()
if not qapp:
qapp = QtWidgets.QApplication(sys.argv)
# run it!
app = ApplicationWindow()
app.show()
app.activateWindow()
app.raise_()
qapp.exec()
这段代码目前会产生一个Qt窗口和两个独立的“canvases”。第一个画布是3个颜色Map表,第二个是一个实时图形的线图。
热门图
- 这些是静态的,但我希望它们是动态的。
*是否有人能够帮助我确定如何在收集数据时实时绘制这些色图?
底部图
- 这是实时运行的
- 我不需要线图来实现我的目的。它只是作为实时绘图方法的参考。
1条答案
按热度按时间fv2wmkja1#
您可以尝试在每次更新调用时重新绘制轮廓。在绘制新信息之前,我使用
ax.cla()
清除当前的绘图。这个脚本就是一个例子,说明了它可能是什么样子。