简体   繁体   中英

How to change background colour of Figure object in Tkinter

How do I change change the colour of the surrounding white area for a 3D matplotlib Figure object embedded in Tkinter?

在此输入图像描述

Below is a simplified version. I tried to modify the bg in root , Frame and the canvas object but none reflected any changes.

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()

This is what you need:

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

It was just the color of the Figure that needed to be changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM