动态更改matplotlib图的内容 [英] Change dynamically the contents of a matplotlib plot
问题描述
我前一段时间,我在比较使用python和matplotlib的两个函数的输出.结果非常简单,因为使用matplotlib进行绘制非常容易:我只是绘制了两个带有不同标记的数组.小菜一碟.
I while ago, I was comparing the output of two functions using python and matplotlib. The result was as good as simple, since plotting with matplotlib is quite easy: I just plotted two arrays with different markers. Piece of cake.
现在我发现自己遇到了同样的问题,但是现在我要比较很多曲线.最初,我尝试使用不同的颜色和标记来绘制所有内容.这让我不满意,因为每条曲线的范围都不完全相同.除此之外,我很快就用尽了足以识别的颜色和标记(RGBCMYK,自定义颜色类似于以前的任何颜色).
Now I find myself with the same problem, but now I have a lot of pair of curves to compare. I initially tried plotting everything with different colors and markers. This did not satisfy me since the ranges of each curve are not quite the same. In addition to this, I quickly ran out of colors and markers that were sufficiently different to identify (RGBCMYK, after that, custom colors resemble any of the previous ones).
我还尝试对每对曲线进行子图绘制,以获得包含许多图的窗口.太拥挤. 我在每个图上尝试了一个窗口,但窗口太多了.
I also tried subplotting each pair of curves, obtaining a window with many plots. Too crowded. I tried one window per plot, too many windows.
所以我只是想知道是否有任何现有的小部件,或者您是否有任何建议(或不同的想法)来完成此任务:
So I was just wondering if there is any existing widget or if you have any suggestion (or a different idea) to accomplish this:
我希望看到一对曲线,然后使用滑块,按钮,鼠标滚动或任何其他小部件或事件轻松选择下一条曲线.通过更改曲线,前一条应消失,图例也应改变,其轴也应改变.
I want to see a pair of curves and then select easily the next one, with a slidebar, button, mouse scroll, or any other widget or event. By changing curves, the previous one should disappear, the legend should change and its axis as well.
推荐答案
好吧,我设法通过一个事件处理程序来实现此操作,以进行鼠标单击.我将其更改为更有用的内容,但无论如何我都会发布解决方案.
Well I managed to do it with an event handler for mouse clicks. I will change it for something more useful, but I post my solution anyway.
import matplotlib.pyplot as plt
figure = plt.figure()
# plotting
plt.plot([1,2,3],[10,20,30],'bo-')
plt.grid()
plt.legend()
def on_press(event):
print 'you pressed', event.button, event.xdata, event.ydata
event.canvas.figure.clear()
# select new curves to plot, in this example [1,2,3] [0,0,0]
event.canvas.figure.gca().plot([1,2,3],[0,0,0], 'ro-')
event.canvas.figure.gca().grid()
event.canvas.figure.gca().legend()
event.canvas.draw()
figure.canvas.mpl_connect('button_press_event', on_press)
这篇关于动态更改matplotlib图的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!