我正在努力解决以下问题.我需要生成由一组图表组成的报告.除了一个之外,所有这些图表都是使用Matplotlib默认后端(TkAgg)制作的.需要使用开罗后端制作一张图表,原因是我正在绘制igraph图,并且只能使用Cairo绘制.
问题是我不能动态改变后端,例如以下方法不起作用:(
matplotlib.pyplot.switch_backend('cairo.png')
我知道switch_backend功能是实验性的)
我也尝试过,matplotlib.use("cairo.png")但这会导致导入问题,因为matplotlib.use("cairo.png")语句应该在导入之前出现matplotlib.pyplot.但是在剧本的生命历程中我需要两个不同的后端.
所以我的问题是有人有一个代码片段,显示如何在Matplotlib中切换后端?
非常感谢!
更新:我编写了一个加载matplotlib的片段,显示默认后端,卸载matplotlib,重新加载它并更改后端:
import matplotlib
import matplotlib.pyplot as plt
import sys
print matplotlib.pyplot.get_backend()
modules = []
for module in sys.modules:
if module.startswith('matplotlib'):
modules.append(module)
for module in modules:
sys.modules.pop(module)
import matplotlib
matplotlib.use("cairo.png")
import matplotlib.pyplot as plt
print matplotlib.pyplot.get_backend()
Run Code Online (Sandbox Code Playgroud)
但这真的是这样做的吗?
更新2:我昨天有一些严重的大脑冻结......简单而明显的解决方案是将开罗后端用于所有图表而不是切换后端:)
更新3:实际上,它仍然是一个问题所以谁知道如何动态切换matplotlib后端....请发布你的答案.
我正在构建一个将matplotlib图形嵌入GUI的应用程序.问题是,一旦我将matplotlib中的任何内容添加到我的代码中,我的应用程序就会崩溃(导入除外,这些工作正常).在我的课,会出现问题Solver_App的tk.Tk.__init__(self, *args, **kwargs).当我运行代码时,我收到一个巨大的错误,应用程序崩溃.这是我的一些代码:
import tkinter as tk
from tkinter import ttk
import matplotlib
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
# Setting up figures for integration in GUI:
fig_3D = plt.figure()
fig_2D = plt.figure()
a_3D = fig_3D.add_subplot(111, projection="3d")
a_2D = fig_2D.add_subplot(111)
a_3D.plot_wireframe([1, 2, 3, 4, 5], [1, 3, 7, 6, 4], [1, 2, 3, 4, 5], color="blue")
class Solver_App(tk.Tk, ttk.Frame):
def __init__(self, *args, **kwargs): …Run Code Online (Sandbox Code Playgroud)