Change background color in Matplotlib

0

I am generating some graphics in a desktop application made with Tkinter. The problem I have is that it generates very simple graphics, with a white background, and I am finding it hard to find information on the Internet about how I can play with the background color, the font of the title, add some description if possible. Let's see if anyone can help me change the background color, even if it is. Greetings, and thank you.

import tkinter as tk
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
import matplotlib.pyplot as plt

class Application(ttk.Frame):
   def __init__(self, main_window):
       super().__init__(main_window)
    main_window.geometry("600x600")

    plt.figure('3')  # Crea una ventana titulada '3'
    self.fig, self.ax = plt.subplots()
    self.ax.plot(np.random.randn(150), np.random.randn(150), 'o')
    self.ax.set_title('Ejemplo grafico de puntos')

    self.canvas = FigureCanvasTkAgg(self.fig, master=main_window)
    self.canvas.draw()
    self.canvas.get_tk_widget().pack()


if __name__ == "__main__":
   main_window = tk.Tk()
   app = Application(main_window)
   app.mainloop()
    
asked by Alfredo Lopez Rodes 18.09.2018 в 12:06
source

1 answer

0

The Tkinter Doc

From what I see, it seems that you create a Canvas .

In the documentation, there is a section of "standard attributes"

link

In it they say:

  

Each widget has a series of options that affect its behavior or its appearance, such as fonts, colors, text sizes, labels ...

     

You can specify the desired options, when you call the widget's constructor, specifying the properties you want as arguments: text='PANIC!' or height=20.

One of those options is the colors

It clearly says that you have to use color codes ( #000 for example)

So, in your case, if you're creating a canvas, you should go to the documentation of the Canvas And it shows that the first option is the background:

So, looking at how the constructor is used, it should be:

 w = tk.Canvas(parent, option=value, ...)

Therefore, where do you do:

self.canvas = FigureCanvasTkAgg(self.fig, master=main_window)

You should do

self.canvas = FigureCanvasTkAgg(self.fig, master=main_window, bg='#000')

EDIT - Coloring embedded MathPlotLib

When the error commented by the user occurs, it seems that it is embedded mathplotlib.

It is therefore necessary to change the colors of the mathplotlib canvas, including the order:

self.ax.set_facecolor('tab:red')

Where the color will be among the MathPlotLib colors

EDIT

  

The user comments that only the color of the MathPlot is changed. For   change the back canvas, so we will call the config of the main window, which is where the canvas seems to have "nested"

 main_window.config(bg='#000')
    
answered by 18.09.2018 в 12:55