- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我希望下面显示一个情节,但我看不到情节并且解释器只是挂起(我的后端将自己报告为 TkAgg
)。
import matplotlib.pyplot as plt
from threading import Thread
def plot():
fig, ax = plt.subplots()
ax.plot([1,2,3], [1,2,3])
plt.show()
def main():
thread = Thread(target=plot)
thread.setDaemon(True)
thread.start()
print 'Done'
如何让绘图显示?
我正在运行一个包含大量迭代的模拟,并希望每 1000 次迭代更新一次我的绘图,以便我可以监控我的模拟是如何演变的。
下面的伪代码:
iterations = 100000
for i in iterations:
result = simulate(iteration=i)
if not i % 1000:
# Update/redraw plot here:
# Add some lines, add some points, reset axis limits, change some colours
将绘图放在主线程中会导致绘图 GUI 挂起/崩溃,大概是因为我还有其他工作正在进行。所以想法是在一个单独的线程中进行绘图。
我看到了使用进程而不是线程的建议(例如 here )。但是在我的模拟运行时我无法操纵图形或轴来添加线条等,因为图形对象处于远程进程中。
我不相信这个问题与 another one 重复因为该问题涉及为什么 pyplot
api 不能用于操作 两个不同的图,每个图都在一个单独的线程上。这是因为同时执行两个绘图产生的竞争条件会阻止 pyplot
确定哪个图是当前图。
但是,我只有 1 个绘图,因此 pyplot
只有一个唯一的当前图形。
最佳答案
正如其他人所说,Matplotlib is not thread safe ,你有一个选择是使用多处理。您说这对您不利,因为您需要访问来自不同进程的轴,但是您可以通过在模拟进程和根进程之间共享 数据 然后管理所有绘图来克服这个问题根进程中的相关事件。例如
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
import multiprocessing
import time
import random
from Tkinter import *
#Create a window
window=Tk()
def main():
#Create a queue to share data between process
q = multiprocessing.Queue()
#Create and start the simulation process
simulate=multiprocessing.Process(None,simulation,args=(q,))
simulate.start()
#Create the base plot
plot()
#Call a function to update the plot when there is new data
updateplot(q)
window.mainloop()
print 'Done'
def plot(): #Function to create the base plot, make sure to make global the lines, axes, canvas and any part that you would want to update later
global line,ax,canvas
fig = matplotlib.figure.Figure()
ax = fig.add_subplot(1,1,1)
canvas = FigureCanvasTkAgg(fig, master=window)
canvas.show()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1)
line, = ax.plot([1,2,3], [1,2,10])
def updateplot(q):
try: #Try to check if there is data in the queue
result=q.get_nowait()
if result !='Q':
print result
#here get crazy with the plotting, you have access to all the global variables that you defined in the plot function, and have the data that the simulation sent.
line.set_ydata([1,result,10])
ax.draw_artist(line)
canvas.draw()
window.after(500,updateplot,q)
else:
print 'done'
except:
print "empty"
window.after(500,updateplot,q)
def simulation(q):
iterations = xrange(100)
for i in iterations:
if not i % 10:
time.sleep(1)
#here send any data you want to send to the other process, can be any pickable object
q.put(random.randint(1,10))
q.put('Q')
if __name__ == '__main__':
main()
关于python - 为什么 matplotlib 不能在不同的线程中绘图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34764535/
我正在尝试编写一个函数来制作绘图并将其自动保存到文件中。 我努力用它来动态地做的技巧[plotname=varname & filename=varname &], 并使其与从循环中调用它兼容。 #
有人可以帮助我如何在绘图条形图中添加“下拉”菜单。 我在以下链接 ( https://plot.ly/python/v3/dropdowns/ ) 上找到了一些信息,但我正在努力调整代码,因此下拉选项
我不确切知道如何表达这一点,但我本质上希望根据其他数据之前的列值将数据分组为 Excel 图的系列。例如: size weight apple 3 35 orange 4
我正在为出版物创建图表并希望它们具有相同的字体大小。 当我用多图创建图形时,字体大小会减小,即使我没有更改tiff() 分辨率或pointsize 参数。我根据最终适合的地 block 数量增加了图形
我用 glm::perspective(80.0f, 4.0f/3.0f, 1.0f, 120.0f);并乘以 glm::mat4 view = glm::lookAt( glm::vec3(
我在 Shiny UI 中有一个情节。如果我更改任何输入参数并且通过 react 性图将会改变。但是让我们考虑以下情况:- Shiny UI 中的绘图可以说股票的日内价格变动。为此,您查询一些实时数据
我对 R 有点陌生。我在以下两个线程中跟踪并实现了结果: http://tolstoy.newcastle.edu.au/R/e17/help/12/03/7984.html http://lukem
我想在 WPF 控件中使用 GDI+ 绘图。 最佳答案 有多种方法可以做到这一点,最简单的方法是锁定您使用 GDI 操作的位图,获取像素缓冲区(从锁定中获取的 BitmapData 中的 Scan0
如何在以下取自其网站的绘图示例中隐藏颜色条? df % layout(title = '2014 Global GDPSource:CIA World Factbook',
我有两列数据,X 和 Y,每个条目在两个向量的小数点后都有 4 位数据。 当我使用 plot(x,y) 绘制简单图时,轴上显示的数据精确到小数点后两位。如何在两个轴上将其更改为小数点后 4 位精度?
我目前正在使用 Canvas 处理 JavaFX-Drawing-Application。在 GraphicsContext 的帮助下,我使用 beginPath() 和 lineTo() 方法绘制线
如果这个问题已经得到解答,但我无法找到我需要的东西,我提前道歉。我想从名为 data1.dat、data2.dat 的文件中绘制一些结果......我设法通过循环导入数据,但我无法使用循环绘制结果。仅
我的 pandas 数据框中有一个功能,可以(可能)具有多个级别。我可以使用以下方法获得独特的级别: reasons = df["Reason"].unique() 我可以通过执行以下操作在单个图表上
我在 Ubuntu 14 和 Windows 7(均为 64 位)中用 Python 绘制结果时遇到问题。作为一个简单的比较,我做了: from tvb.simulator.lab import *
以下代码相当简单 - 它用随机选择的像素填充设计表面 - 没什么特别的(暂时忽略第二种方法中的 XXXXXXX)。 private void PaintBackground() { Rando
我正在尝试制作一个绘制函数图形的 swing 应用程序(现在很简单,例如 x+2)但我在根据屏幕坐标制作我的点的数学坐标时遇到问题。我希望它在我的图表中简单地画一条从 P1(0,1) 到 P2(1,2
编辑 4:问题的新格式 背景:我有一个扩展 JFrame 的类 Window,在 JFrame 中我有一个 Canvas 。我向 Canvas 添加自定义对象。这个对象的唯一目的(为了争论)是在 Ca
我需要为即将到来的锦标赛标记阶梯,但我找不到任何方法来语义标记它。到目前为止我看到的唯一方法是 mark it up as a table ,我想不惜一切代价避免这种情况。 有什么想法吗? 最佳答案
我目前正在为一个小型 uC 项目编写 UI。在计算垂直线的位置时遇到一些问题。这个想法是将红线沿 x 轴移动到矩形的末端。 使用无限旋转编码器递增的值,范围为 0 到 800,增量为 1。矩形的左侧是
我正在尝试绘制光分布图。我想准确地执行此问题的第一步所要求的:Statistical analysis on Bell shaped (Gaussian) curve . 现在我有一组值。我希望数组元
我是一名优秀的程序员,十分优秀!