【问题标题】:How do you change the plot background color in a tkinter application?如何在 tkinter 应用程序中更改绘图背景颜色?
【发布时间】:2020-08-05 18:40:41
【问题描述】:

我在 tkinter 应用程序中有一个绘图,我正在尝试使用 tkinter colors 更改绘图的背景颜色。我找到了How to change plot background color? 但是,我收到了这个错误:

ValueError: Invalid RGBA argument: 'AntiqueWhite2'

...表示我可能只能使用他们的颜色子集?有谁知道如何在这里使用 tkinter 颜色?

这是我目前使用的(没有颜色变化):

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)

【问题讨论】:

    标签: python tkinter plot colors background


    【解决方案1】:
    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()
    

    这是一个将背景更改为黑色并更改标签、网格、刻度标签、...颜色的示例。

    【讨论】:

      猜你喜欢
      • 2012-12-14
      • 2011-03-26
      • 2011-02-14
      • 2018-07-25
      • 1970-01-01
      • 2016-09-10
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多