简体   繁体   English

如何更改 tkinter 应用程序中的 plot 背景颜色?

[英]How do you change the plot background color in a tkinter application?

I have a plot in a tkinter application and I am trying to change the background color of the plot using tkinter colors . I have a plot in a tkinter application and I am trying to change the background color of the plot using tkinter colors . I have found How to change plot background color?我发现如何更改 plot 背景颜色? however, I got this error:但是,我收到了这个错误:

ValueError: Invalid RGBA argument: 'AntiqueWhite2'

...indicating I may only be able to use their subset of colors? ...表明我可能只能使用他们的 colors 子集? Does anyone know how to use the tkinter colors here?有人知道如何在这里使用 tkinter colors 吗?

Here is what I am currently using (without color change):这是我目前使用的(没有颜色变化):

self.fig = Figure(figsize=(5, 4), dpi=100)
self.canvas = FigureCanvasTkAgg(self.fig, master=self.master)  # A tk.DrawingArea
self.canvas.draw()
self.ax = self.fig.add_subplot(111)
import tkinter
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure

import numpy as np


root = tkinter.Tk()
root.geometry("770x633+745+171")
Frame1 = tkinter.Frame(root)
Frame1.place(relx=0.1, rely=0.1, relheight=0.3, relwidth=0.5)
root.wm_title("Embedding in Tk")
t = np.arange(0, 3, .01)


fig = Figure(figsize=(5, 3), dpi=80,facecolor = 'k')

axes1 =fig.add_subplot(111)
# axes1.axis('tight')
axes1.autoscale(enable=True, axis='y', tight=True)
axes1.plot(t, 2 * np.sin(2 * np.pi * t))
# axes1.set_axis_bgcolor('k')
axes1.set_facecolor('k')
axes1.grid(color = 'w')

for label in axes1.xaxis.get_ticklabels():
            # label is a Text instance
    label.set_color('w')
for label in axes1.yaxis.get_ticklabels():
    # label is a Text instance
    label.set_color('w')
    # label.set_rotation(45)
    # label.set_fontsize(1)
for line in axes1.yaxis.get_ticklines():
    # line is a Line2D instance
    line.set_color('w')
for line in axes1.xaxis.get_ticklines():
    # line is a Line2D instance
    line.set_color('w')
    # line.set_markersize(25)
    # line.set_markeredgewidth(3)
for line in axes1.xaxis.get_gridlines():
    line.set_color('w')

for line in axes1.yaxis.get_gridlines():
    line.set_color('w')
    line.set_markeredgewidth(8)
axes1.yaxis.grid(color='w',linewidth=1)
# axes1.set_xmargin(0.9)
axes1.set_xlabel("Time(ns)")
axes1.xaxis.label.set_color('w')
axes1.set_ylabel("Amplitude(mV)")
axes1.yaxis.label.set_color('w')
axes1.xaxis.grid(color='w',linewidth=1)
axes1.spines['bottom'].set_color('white')
axes1.spines['top'].set_color('white')
axes1.spines['left'].set_color('white')
axes1.spines['right'].set_color('white')

canvas = FigureCanvasTkAgg(fig, master=Frame1)  # A tk.DrawingArea.
canvas.get_tk_widget().configure(bg='black')
canvas.get_tk_widget().grid(row=1,column=0)
canvas.get_tk_widget().grid(row=1,column=0)
# canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)
canvas.draw()

toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)


def on_key_press(event):
    print("you pressed {}".format(event.key))
    key_press_handler(event, canvas, toolbar)


canvas.mpl_connect("key_press_event", on_key_press)

fig.tight_layout()

def _quit():
    root.quit()     # stops mainloop
    root.destroy()  # this is necessary on Windows to prevent
                    # Fatal Python Error: PyEval_RestoreThread: NULL tstate


button = tkinter.Button(master=root, text="Quit", command=_quit)
button.pack(side=tkinter.BOTTOM)

tkinter.mainloop()
# plt.tight_layout()

This is an example for change the background as black and change the label, grid, tick labels, ... color.这是将背景更改为黑色并更改 label、网格、刻度标签...颜色的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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