我创建了Django视图'graph',目的是显示从matplotlib.pyplot模块创建的图像。我在 views.py 中导入的脚本 data_analysis.py 中编写了我的函数 plot_bubbles (返回一个matplotlib.figure.Figure对象)。脚本。
Tkinter只能在主线程上运行,我的网页第一次请求它时按预期工作但刷新或再次请求时不显示图像。我的理解是,当再次请求视图时,Django在新线程上运行代码。
我尝试使用Queue类,并使用返回我的数字的函数填充它,如本回答Execute Python function in Main thread from call in Dummy thread中所述。这是我的观点脚本。
from django.http import HttpResponse
from . import data_analysis
import Queue
import threading
q = Queue.Queue()
def graph(request):
parties = ["Conservative Party", "Labour Party", "Green Party", "UKIP"]
def from_other_thread(graph_function):
q.put(graph_function)
def main_thread_execute():
callback = q.get()
fig = callback
return fig
def grapher(arguments, area_variable):
data_analysis.plt.close('all')
from_other_thread(data_analysis.plot_bubbles(arguments, area_variable))
t = threading.Thread(target = grapher, args=(parties, data_analysis.all_data['2015 Pay']))
t.start()
t.join()
fig = main_thread_execute()
response = HttpResponse(content_type='image/png')
fig.savefig(response, format='png')
return response
目的是在主线程中运行该函数,以便Tkinter可以实际工作并创建图像我希望每次请求url时都会创建图像,因为我会让用户选择他的变量想要通过表单进行可视化,将它们作为plot_bubbles函数的参数传递。
我是django的初学者,从未在我的代码中使用多线程,感谢您阅读本文。非常感谢您对解决方案的任何解释。
不一定需要线程化。问题源于我的data_analysis脚本生成绘图的方式。特别是代码调用方法matplotlib.pyplot.subplots http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplots生成一个带有4个轴对象的图形,如此处
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2,2, figsize(15,12))
这似乎导致Tkinter无法在主循环上运行,我还没有完全理解其中的原因。这就是代码现在的样子
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
from pandas import Series, DataFrame
from pylab import figure, axes, plot, title, subplots
import statsmodels.api as sm
from sqlalchemy import create_engine
from matplotlib.backends.backend_agg import FigureCanvasAgg
import matplotlib
# Load data from database into dataframe
engine = create_engine("postgresql://user_name:password@localhost:5432/brexit")
all_data = pd.read_sql('''SELECT * FROM records;''', engine, index_col='Borough')
# Bubble Plot function creation
colors = np.random.rand(len(all_data.index))
area = []
def plot_bubbles(arguments, area_variable, space=0):
ah = iter(arguments)
eh = iter(arguments)
ih = iter(arguments)
kh = iter(arguments)
th = iter(arguments)
zh = iter(arguments)
mh = iter(arguments)
fig = figure(figsize=(30, 25))
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)
collection = [ax1, ax2, ax3, ax4]
for x in area_variable:
#want the bubbles to have an average area of 40, add a factor to increase the variability in size
factor = ((x-area_variable.mean())**2/400)
area.append(factor*x*(40/area_variable.mean()))
for ax in collection:
orient = all_data[ah.next()]
ax.set_ylabel('Leave %')
ax.set_xlim([max(0, all_data[zh.next()].min()-all_data[mh.next()].min()/3),
all_data[ih.next()].max()+all_data[th.next()].max()/7])
results = sm.OLS(all_data['Leave votes'], sm.add_constant(orient)).fit()
X_plot = np.linspace(orient.min()-0.05, orient.max(), 100)
ax.plot(X_plot, X_plot*results.params[1] + results.params[0], )
for label, ori, leave in zip(all_data.index, orient, all_data['Leave votes']):
ax.annotate(label, xy=(ori, leave), xytext=(ori, leave+0.05),
arrowprops={'facecolor':'black', 'connectionstyle':'arc3,rad=0.3', 'arrowstyle':'simple'})
ax.scatter(orient, all_data['Leave votes'], s=area, c=colors, alpha=0.6)
ax.set_title(kh.next())
fig.subplots_adjust(hspace=space, wspace=space)
return fig
随着图形和轴的创建方式的改变,问题得以解决。如果有人解释为什么会出现这种情况,那将会很有趣。