1

So this is a simplified version, but as you can see I create a graph with matplotlib and display it. I display it with 'gray21' which has worked for Tkinter objects, but its not working here. The color 'white' is the only one I have found that works. How can I get a variety of colors? Can I use RGB or some form of exact color specification, because I have a color in mind (R:70 G:70 B:70). What forms of colors does .patch.set_facecolor() take?

import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from Tkinter import *


root=Tk()
root.geometry('1000x700')
root.configure(bg="gray21")
root.title("My Graph")


one_day_fig=plt.figure()

one_day_fig.patch.set_facecolor('gray21')
plt.plot([1,2,3], [2,4,6])
my_canvas = FigureCanvasTkAgg(one_day_fig,master=root)
plot_widget = my_canvas.get_tk_widget()
plot_widget.place(x=50, y=50)


root.mainloop()
6
  • 1
    matplotlib.org/users/colors.html Commented Jun 3, 2017 at 23:57
  • @Goyo so it said that RGB takes 3 numbers, so if my RGB value was 70, 70, 70, wouldn't I do one_day_fig.patch.set_facecolor(.7, .7, .7) ? This however didn't work. What am I missing? Commented Jun 4, 2017 at 0:46
  • 1
    Did you read the error message? Also what is said is "an RGB or RGBA tuple of float values". Commented Jun 4, 2017 at 0:52
  • @Goyo yes, it says: TypeError: set_facecolor() takes exactly 2 arguments (4 given) This is odd because I gave 3 values, not 4. And what does RGB or RGBA tuple of floats mean? Commented Jun 4, 2017 at 0:58
  • @Goyo sorry autocorrect. Ok so what would my parameters be if my wanted color is R:70 G:70 B:70 ? Would it be one_day_fig.patch.set_facecolor([.7, .7, .7])? Because I have tried that Commented Jun 4, 2017 at 1:04

1 Answer 1

1

RGB values can be specified as 3-tuples of integers between 0 and 255 or as 3-tuples of floats between 0 and 1.

The tuple (70,70,70) would thus correspond to (70/255., 70/255., 70/255.).

This can be used as a color specification to matplotlib.

figure.set_facecolor((70/255., 70/255., 70/255.))
Sign up to request clarification or add additional context in comments.

2 Comments

Ah thank you! Would you happen to know how to change the font colors of my x and y axis?
This is not a chat here. If you have a question, ask a question. But make sure the question hasn't been asked already. As you can imagine you're not the first one that wants to change some color in the plot, so possibly this is the question you're after?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.