简体   繁体   English

matplotlib可以只更新最新的数字吗?

[英]Can matplotlib only update the newest point to the figure?

Is it possible for matplotlib only update the newest point to the figure instead of re-draw the whole figure? matplotlib是否有可能只更新最新的数字而不是重新绘制整个数字?

For example: this may be the fastest way for dynamic plotting 例如:这可能是动态绘图的最快方式

initiate:
fig1 = Figure(figsize = (8.0,8.0),dpi = 100)
axes1 = fig1.add_subplot(111)
line1, = axes1.plot([],[],animated = True)

when new data is coming:
line1.set_data(new_xarray,new_yarray)
axes1.draw_artist(line1)
fig1.canvas.update()
fig1.canvas.flush_events()

But this will re-draw the whole figure! 但这将重新绘制整个数字! I'm think whether this is possible: 我认为这是否可行:

when new data is coming:
axes1.draw_only_last_point(new_x,new_y)
update_the_canvas()

It will only add this new point(new_x,new_y) to the axes instead of re-draw every point. 它只会将这个新点(new_x,new_y)添加到轴而不是重新绘制每个点。

And if you know which graphic library for python can do that, please answer or comment, thank you so much!!!!! 如果您知道python的哪个图形库可以做到这一点,请回答或评论,非常感谢!!!!!

Really appreciate your help! 真的很感谢你的帮助!

Is only redrawing the entire figure the problem, ie it is ok to redraw the line itself as long as the figure is unchanged? 只重绘整个图形的问题,即只要数字不变就可以重绘线条本身? Is the data known beforehand? 事先知道数据吗?

If the answer to those questions are NO, and YES, then it might be worth looking into the animate -class for matplotlib. 如果这些问题的答案是否定的,那么可能值得查看matplotlib的animate -class。 One example where the data is known beforehand, but the points are plotted one by one is this example . 预先知道数据但是逐个绘制点的一个示例此示例 In the example, the figure is redrawn if the newest point is outside of the current x-lim. 在该示例中,如果最新点在当前x-lim之外,则重绘该图。 If you know the range of your data you can avoid it by setting the limits beforehand. 如果您知道数据的范围,可以通过预先设置限制来避免它。

You might also want to look into this answer , the animate example list or the animate documentation . 您可能还想查看此答案动画示例列表动画文档

this is my (so far) little experience. 这是我(迄今为止)的一点经验。
I started some month ago with Python(2.x) and openCV (2.4.13) as graphic library.I found in may first project that openCV for python works with numpy structure as much as matplotlib and (with slight difference) they can work together. 我在几个月前开始使用Python(2.x)和openCV(2.4.13)作为图形库。我发现可能首先预测openCV for python的工作方式与matplotlib一样多,并且(略有不同)它们可以工作一起。

I had to update some pixel after some condition. 在某些条件之后我不得不更新一些像素。 I first did my elaboration from images with opencv obtaining a numpy 2D array, like a matrix. 我首先使用opencv从图像中进行了详细说明,获得了一个numpy 2D数组,就像一个矩阵。 The trick is: opencv mainly thinks about input as images, in terms of X as width first, then Y as height. 诀窍是:opencv主要将输入视为图像,首先是X作为宽度,然后是Y作为高度。 The numpy structure wants rows and columns wich in fact is Y before X. numpy结构希望行和列实际上是X之前的Y.

With this in mind I updated pixel by pixel the image-matrix A and plot it again with a colormap 考虑到这一点,我逐个像素地更新了图像矩阵A并再次用色彩图绘制它

import matplotlib as plt
import cv2
A = cv2.imread('your_image.png',0) # 0 means grayscale
# now you loaded an image in a numpy array A
for every new x,y pixel
A[y,x] = new pixel intensity value
plot = plt.imshow(A, 'CMRmap')
plt.show()

If you want images again, consider use this 如果您想再次拍摄图片,请考虑使用此图片

import matplotlib.image as mpimg
#previous code
mpimg.imsave("newA.png", A)

If you want to work with colors remember that images in colour are X by Y by 3 numpy array but matplotlib has RGB as the right order of channels, openCv works with BGR order. 如果你想使用颜色,请记住颜色的图像是X乘Y 3 numpy数组,但matplotlib有RGB作为正确的通道顺序,openCv使用BGR顺序。 So 所以

C = cv2.imread('colour_reference.png',1) # 1 means BGR
A[y,x,0] = newRedvalue = C[y,x][2]
A[y,x,1] = newGreenvalue = C[y,x][1]
A[y,x,2] = newBluevalue = C[y,x][0]

I hope this will help you in some way 我希望这会以某种方式帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM