Python 3.x 如何在不创建新窗口的情况下更新matplotlib热图打印
我有一个从列表继承的矩阵类。此类可以将自身显示为矩阵的matplotlib热图表示形式 我试图编写这样的类,当我更改矩阵中的值时,我可以调用矩阵的方法Python 3.x 如何在不创建新窗口的情况下更新matplotlib热图打印,python-3.x,matrix,matplotlib,heatmap,Python 3.x,Matrix,Matplotlib,Heatmap,我有一个从列表继承的矩阵类。此类可以将自身显示为矩阵的matplotlib热图表示形式 我试图编写这样的类,当我更改矩阵中的值时,我可以调用矩阵的方法plot(),它将更新plot以反映热图中的矩阵更改 但是,每次运行方法plot(),它都会在新窗口中创建一个新的热图,而不是更新现有的绘图。我怎样才能简单地更新现有的绘图 在下面的代码中,有三个主要部分:main函数显示如何创建、绘制和更新矩阵类的实例;矩阵类基本上是一个列表对象,附带一些次要功能(包括绘图);函数plotList()是矩阵类为最
plot()
,它将更新plot以反映热图中的矩阵更改
但是,每次运行方法plot()
,它都会在新窗口中创建一个新的热图,而不是更新现有的绘图。我怎样才能简单地更新现有的绘图
在下面的代码中,有三个主要部分:main函数显示如何创建、绘制和更新矩阵类的实例;矩阵类基本上是一个列表对象,附带一些次要功能(包括绘图);函数plotList()
是矩阵类为最初生成绘图对象而调用的函数
import time
import random
import matplotlib.pyplot as plt
plt.ion()
import numpy as np
def main():
print("plot 2 x 2 matrix and display it changing in a loop")
matrix = Matrix(
numberOfColumns = 2,
numberOfRows = 2,
randomise = True
)
# Plot the matrix.
matrix.plot()
# Change the matrix, redrawing it after each change.
for row in range(len(matrix)):
for column in range(len(matrix[row])):
input("Press Enter to continue.")
matrix[row][column] = 10
matrix.plot()
input("Press Enter to terminate.")
matrix.closePlot()
class Matrix(list):
def __init__(
self,
*args,
numberOfColumns = 3,
numberOfRows = 3,
element = 0.0,
randomise = False,
randomiseLimitLower = -0.2,
randomiseLimitUpper = 0.2
):
# list initialisation
super().__init__(self, *args)
self.numberOfColumns = numberOfColumns
self.numberOfRows = numberOfRows
self.element = element
self.randomise = randomise
self.randomiseLimitLower = randomiseLimitLower
self.randomiseLimitUpper = randomiseLimitUpper
# fill with default element
for column in range(self.numberOfColumns):
self.append([element] * self.numberOfRows)
# fill with pseudorandom elements
if self.randomise:
random.seed()
for row in range(self.numberOfRows):
for column in range(self.numberOfColumns):
self[row][column] = random.uniform(
self.randomiseLimitUpper,
self.randomiseLimitLower
)
# plot
self._plot = plotList(
list = self,
mode = "return"
)
# for display or redraw plot behaviour
self._plotShown = False
def plot(self):
# display or redraw plot
self._plot.draw()
if self._plotShown:
#self._plot = plotList(
# list = self,
# mode = "return"
# )
array = np.array(self)
fig, ax = plt.subplots()
heatmap = ax.pcolor(array, cmap = plt.cm.Blues)
self._plot.draw()
else:
self._plot.show()
self._plotShown = True
def closePlot(self):
self._plot.close()
def plotList(
list = list,
mode = "plot" # plot/return
):
# convert list to NumPy array
array = np.array(list)
# create axis labels
labelsColumn = []
labelsRow = []
for rowNumber in range(0, len(list)):
labelsRow.append(rowNumber + 1)
for columnNumber in range(0, len(list[rowNumber])):
labelsColumn.append(columnNumber)
fig, ax = plt.subplots()
heatmap = ax.pcolor(array, cmap = plt.cm.Blues)
# display plot or return plot object
if mode == "plot":
plt.show()
elif mode == "return":
return(plt)
else:
Exception
if __name__ == '__main__':
main()
我在Ubuntu中使用Python3。方法plot(self)
在fig,ax=plt.subplots()
行中创建一个新图形。要使用现有地物,可以在plotList()
中首次创建时为其指定一个数字或名称:
然后使用
plt.figure('matrix figure')
ax = gca() # gets current axes
使其成为活动图形和。或者,您可能希望在
绘图列表中创建图形和轴,并将其传递给绘图
请提供一个非常感谢你在这方面的帮助。我按照您的建议做了,从绘图创建函数返回图形和轴对象。@d3pd您有最终的工作代码吗?
plt.figure('matrix figure')
ax = gca() # gets current axes