Matplotlib with a dark background
The Matplotlib package for the Python language is pretty powerful, but sometimes it can be a bit frustrating to use. Some things simply aren't intuitive or obvious and the documentation can be a bit obtuse at times.
This time, I was trying to get Matplotlib to plot in negative -- not the usual black-on-white, by white-on-black. This can be useful for websites... especially those when you want to plot data on the same page as faint aurora images. While I found changing the axis face colour pretty quickly, the borders remained stubbornly white. I did figure it out in the end, but it took a while. Here's how to do it.
This results in a plot that looks like this:
The axes face (the region within the box) and the figure face (the rest of the "page") are thus black and all the labels, etc. are set to white.
Actually, you can control all the colours of the plot, including the axes and figure face colours. To do this, you need to set the relevant rcParams. There are actually lots of these and they can be used to control most of the aspects of the way Matplotlib plots.
For example, I'll add two more lines to the previous example, which gives:
Resulting in this plot:
Important: note that the rcParam to change is "savefig.facecolor", not "figure.facecolor"!
This time, I was trying to get Matplotlib to plot in negative -- not the usual black-on-white, by white-on-black. This can be useful for websites... especially those when you want to plot data on the same page as faint aurora images. While I found changing the axis face colour pretty quickly, the borders remained stubbornly white. I did figure it out in the end, but it took a while. Here's how to do it.
#!/usr/bin/env python import matplotlib.pyplot as plt # Create some data x = [0.0, 1.0, 2.0, 3.0] y = [2.0, 0.0, 1.0, 7.0] # Create a new figure, and set the style fig=plt.figure() plt.style.use('dark_background')
# Plot the data and set the labels. plt.plot(x,y,color='r') plt.xlabel("x") plt.ylabel("y") # Save the figure to a file plt.savefig("dark.png")
This results in a plot that looks like this:
The axes face (the region within the box) and the figure face (the rest of the "page") are thus black and all the labels, etc. are set to white.
Actually, you can control all the colours of the plot, including the axes and figure face colours. To do this, you need to set the relevant rcParams. There are actually lots of these and they can be used to control most of the aspects of the way Matplotlib plots.
For example, I'll add two more lines to the previous example, which gives:
#!/usr/bin/env python import matplotlib.pyplot as plt # Create some data x = [0.0, 1.0, 2.0, 3.0] y = [2.0, 0.0, 1.0, 7.0] # Create a new figure, and set the style fig=plt.figure() plt.style.use('dark_background') plt.rcParams['axes.facecolor'] = 'g' plt.rcParams['savefig.facecolor'] = 'b' # Plot the data and set the labels. plt.plot(x,y,color='r') plt.xlabel("x") plt.ylabel("y") # Save the figure to a file plt.savefig("face.png")
Resulting in this plot:
Important: note that the rcParam to change is "savefig.facecolor", not "figure.facecolor"!
Great tip. It worth mentioning that to go back to the default style sheet you may use: plt.style.use('default').
ReplyDeleteTHANKSSS
ReplyDeleteDoing animations for space physics in matplotlib is SO much nicer with black background and no axis :3 thumbs up
ReplyDeleteLearn python and earn money from python.
ReplyDeletepython course london
Big data is a term that describes the large volume of data – both structured and unstructured – that inundates a business on a day-to-day basis. big data projects for students But it’s not the amount of data that’s important.Project Center in Chennai
DeletePython Training in Chennai
Python Training in Chennai
The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai
It’s really amazing to see this blog and its related to what I expected.
ReplyDeleteRobotics Process Automation Training in Chennai
App Development Course in Chennai
Angular 4 Training in Chennai
.Net training in chennai
Ethical Hacking Training in Chennai
Digital Marketing Training Institute in Chennai
This blog is very knowledgeable. Thank you for writing this blog. Dreamsoft Consultancy is one of the Leading Company in India who Provide Genuine Experience Certificate in Pune. So Contact here and Get all Details. Contact Us with Get all Details- 9599119376 or check Our Website- https://experiencecertificates.com/experience-certificate-provider-in-Gurgaon.html
ReplyDeleteThis blog is very knowledgeable. Thank you for writing this blog. Dreamsoft Consultancy is one of the Leading Company in India who Provide Genuine Experience Certificate in Pune. So Contact here and Get all Details. Contact Us with Get all Details- 9599119376 or check Our Website- https://experiencecertificates.com/experience-certificate-provider-in-mumbai.html
ReplyDeleteNice post. I understand some thing more challenging on diverse blogs everyday. It will always be stimulating to read content from other writers and use a little something from their website. I’d would prefer to apply certain together with the content in my small weblog whether you do not mind. Natually I’ll provide a link on your own web blog. Many thanks for sharing. white house market
ReplyDelete
ReplyDeleteGone through your blog it is very knowledgeable and have very interesting fact. Dreamsoft is the 20 years old consultancy providing fake experience certificate in Noida To get fake experience certificate in Noida you can call at 9599119376 or can the visit https://experiencecertificates.com/experience-certificate-provider-in-Noida.html
ReplyDeleteGood to hear you views about I totally agree to your views. For filling the gap in the courier Dreamsoft will provide the Genuine experience certificate in Delhi, so here is your turn to grab the opportunity and to take your experience certificate in Delhi. The one who is interested for the experience certificate in Delhi may contact at 9599119376 or can visit our website at https://experiencecertificates.com/experience-certificate-provider-in-delhi.html