如何在Tkinter中更改Figure对象的背景颜色

时间:2019-04-17 07:46:06

标签: python matplotlib tkinter

如何更改Tkinter中嵌入的3D matplotlib Figure对象的周围白色区域的颜色?

enter image description here

下面是简化版本。我试图修改rootFramecanvas对象中的bg,但是没有一个反映任何变化。

from tkinter import *
import matplotlib
import matplotlib.pyplot as plt
plt.style.use('ggplot')
from datetime import datetime
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.dates as dates

class PlotGraph(Frame):
    def __init__(self, parent, **kwargs):
        Frame.__init__(self, parent, **kwargs)
        self.config(bg="black")
        self.f = Figure(figsize=(8, 4), dpi=100)
        self.canvas = FigureCanvasTkAgg(self.f, self)
        self.canvas.get_tk_widget().configure(background='black')
        self.canvas.get_tk_widget().pack(fill=BOTH, expand=True)
        self.canvas._tkcanvas.config(bg="black")
        self.ax = self.f.add_subplot(111, projection="3d")
        x = ['2019-04-17', '2019-04-18', '2019-04-19', '2019-04-20', '2019-04-21']
        y = [3, 1, 3, 0, 1]
        some_dates = [dates.date2num(datetime.strptime(i,"%Y-%m-%d")) for i in x]
        weekday = [datetime.strptime(i,"%Y-%m-%d").weekday() for i in x]
        self.ax.plot(some_dates,weekday,y,marker="o")        

root = Tk()
root.config(bg="black")
fig = PlotGraph(root)
fig.pack()

root.mainloop()

1 个答案:

答案 0 :(得分:0)

这就是您需要的:

self.f.patch.set_facecolor('black')

只是需要更改图形的颜色。