Changing rcParams for Better Data Science Plots | Matplotlib Tutorial

Dave Ebbelaar · Beginner ·🛠️ AI Tools & Apps ·3y ago

Key Takeaways

This video teaches how to configure Matplotlib rcParams for better data science plots

Full Transcript

everyone welcome to a new video my name is dave and my goal is to help you level up as a data scientist and in today's video i want to show you how you can basically automatically customize your matlob lib figures so you can create better figures for your data science projects yesterday i was working on some reports i was struggling with something um but i solved the problem and i thought yeah let's make a video about it to show you how i work with matloblip and how i use it so yeah hopefully you will learn something from this so what i will do i will just show you uh some basic examples so let's hope into vs code so i've explained earlier in another video which i will link in the description that i use vs code for my data science projects and in that video you will find how i've set this up and also in another video i explained how i structure my data science project so this project will also follow that structure so that's what we're looking at right now and uh i just want to give you uh some examples over here so i will start up an interactive python session here let's first of all import some data because that is typically what a data science project starts with this is a data set from a bump this is a open source data set it has about 51 sensors in here and also machine status what the data looks like is pretty irrelevant for what i'm going to show you just know that this is the data set that we're using the thing that you typically do when you load data set is you want to explore it so you maybe run the describe or the info so for example let me take this data variable over here and we do the info so we can see okay what's in it are there any nulls of course describe the data set so we get some idea about the standard statistical properties of this data set to me i'm a very visual person so one of the first things that i like to do especially when it comes to sensor data is just make a plot because that just gives you a much better idea of what the data looks like and how it's distributed so this was an example of plotting an individual column but what i often do is i loop through a data set so i here i made a selection of the first five columns and we can plot the data over here but now the thing is i usually like to tweak my matte plot lip figures a bit to make them a bit larger exactly especially when i'm working with time series data for example i don't like the images to be square so i would like to stretch them a little so what you can then do of course is you can put in the parameter fixed size for example and we can tweak it to be another aspect ratio for example if we do it like this and this already gives me a much better idea of the trends within the data because it's not so squeezed together especially when you're working with larger data sets over a longer period of time it can be very helpful to plot uh the data like this but then there's this problem that every time you want to tweak your matplotlib figures you have to input these parameters like fixed size or line width line color legend labels etc and that just gets really annoying over time now when you're trying to make figures for report for example where they have to be really on point and specific then of course it's fine to type it all out and create the script and make the reports but just for exploratory data analysis that you do all the time for every project for every data set you just don't want to mess these with these parameters all the time you just want to have a look at the data and then continue to uh to the next step so that can get really annoying so uh luckily there is a solution to that and that is what i will now show you so how this works is you can set this up by customizing the multiple clip with style sheets and rc params so on this web page over here there's a detailed explanation about how this works and i will link this in the description there's this list of rc params that you can set at the script level and after setting these parameters all the plots afterwards will use the parameters that you've specified so this is very convenient i will now show you why so how this works is i have another file over here basically the same file uh that we just had but now with the rc params included so let me just run everything and basically what you can see here here is i've set parameters for fixed size face color grids colors line width etc and now when we import the data again and we plot our column you can see that we get a nice plot using the ggplot theme it's nice and white it also has a grid which has a nice light grey color so basically by just running the commands over here we get this plot and so now instead of if i go back to the other file well for basically for every plot that we want to do we have to include this fixed size and potentially even other parameters now we can just set it at the top of our script and then we're good to go same for the loop so if i now loop through all the data we get some really nice plots over here and now to take this one step further to make it even better what we can do is we can save these settings and put them into a separate file so that's what i've done over here if i go back to my project structure within the source folder i've added a utility folder and in there is a file called plotsettings.pi in which i've copied and pasted all these settings and what we can now do is we can use this file and import this within our other files so let me give you another example so here is the same thing we import the data and we make a plot but now instead of defining all the parameters in the script itself we import it using this statement over here so now in order for this to work i import a library called system and i append the parent directory because the way this project is set up is so we're in the data folder and then we have to go up one level to the source and then import it from utility so we've basically turned it into a module so that is how this works so now if i run this it will import our plot settings and by doing so it will automatically run the lines within this file as well because there is no function or anything defined so whenever you run this import statement everything in here will run so now within this file mutterlib will use the settings so let me give you the example one more time we we load the data and then create a plot loop through the data and as you can see all the plots look really nice following the settings that we've specified over here and now if you want to change some parameters that's as easy as adjusting this file so for example if we want to change the aspect ratio and we don't want to grid this time save this file run this again as you can see we have this different aspect ratio and no grid this time now there are two things to keep in mind and that is what i was struggling with yesterday so first of all in order for this to work properly make sure that you have the following extension installed so that is the jupiter notebook renderers if you're using vs code so within vs code make sure this is installed and enabled so that's the first thing and another thing that you should keep in mind is that there are some problems bugs i think in newer versions of matplotlib where this doesn't work properly with fierce code and i if this is what i was struggling with yesterday so i am currently on let me check multiple blip 3.5.1 and i think i was on 3.5.2 so that is the newest version but in the newest version you get this strange behavior where you basically have to run all these settings twice so whenever you import them and then do your plots they won't work but then when you import them again so you run the lines again then they work but this is specifically within an interactive python session might also be in a jupyter notebook not sure but it was working when i was running it just as a python file so uh coming up here and then uh run as by so for example doing it like this so that was working properly oh now i get all the plots over here so this run python file so it would work like that with the newest version of matplotlib but not within the interactive python session so that is something to keep in mind and that is something that was driving me crazy yesterday and then i i found post somewhere on github where somebody was mentioning it that it it works fine in a 3.5.1 so that is the version of multiple lib that i'm currently on if you want to try this out yourself i've created a github repository for this project which i will link in the description you can clone it test it out and of course you are not limited to the parameters that i've specified in this file over here as mentioned there is a huge list as you can see of all the parameters that you can set so get creative make something that you like and basically the main goal of course is to create a style that works for you and that makes it as easy as pos as possible for you to interpret the data so that's basically what this is all about of course you can also use it to create report templates so that's another way to use this so basically make a nice set of parameters that you can use for reports and then every time you create your figures you basically make sure that they all look the same so they will look cohesive when you put them in a presentation or a final report of a project for example so that's what i wanted to show you in today's video i think this is a huge time saver something i struggled with i came up with this method to put it in plot settings yeah i hope this will help you out i hope you find it interesting as always if this video was helpful to you i would really appreciate it if you like this video and subscribe to the channel i'll be making more videos related to python data science and machine learning so if that's something you're interested in you should definitely subscribe see you next time [Music]

Original Description

In this video, I will show you how to configure the Matplotlib rcParams and save them into a file to save time when creating plots for your data science projects. This is really convenient for changing common style settings like colors, size of figures, and line styles. Timestamps - 00:00 Introduction & Overview - 01:54 Plotting Pandas Series - 02:56 Customizing Matplotlib with Style Sheets and rcParams - 05:27 Importing Style Settings from a File - 07:16 Requirements - 09:02 General Tips Link to project - https://github.com/daveebbelaar/matplotlib-tutorials Matplotlib documentation - https://matplotlib.org/stable/tutorials/introductory/customizing.html - https://matplotlib.org/stable/api/matplotlib_configuration_api.html#matplotlib.rcParams How to Set up VS Code for Data Science - https://youtu.be/zulGMYg0v6U The Best Way to Organize Your Data Science Projects - https://youtu.be/MaIfDPuSlw8 Let's Connect - Instagram | https://instagram.com/daveebbelaar - LinkedIn | https://linkedin.com/in/daveebbelaar - Twitter | https://twitter.com/daveebbelaar
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Dave Ebbelaar · Dave Ebbelaar · 9 of 60

1 How to Install Homebrew on Mac (Getting Started)
How to Install Homebrew on Mac (Getting Started)
Dave Ebbelaar
2 How to Install Python on Mac (Homebrew)
How to Install Python on Mac (Homebrew)
Dave Ebbelaar
3 How to Install Anaconda on Mac (Getting Started)
How to Install Anaconda on Mac (Getting Started)
Dave Ebbelaar
4 How to Set up VS Code for Data Science & AI
How to Set up VS Code for Data Science & AI
Dave Ebbelaar
5 How to Use Git in VS Code for Data Science
How to Use Git in VS Code for Data Science
Dave Ebbelaar
6 Data Science Desk Setup to Maximize Productivity
Data Science Desk Setup to Maximize Productivity
Dave Ebbelaar
7 THIS Is How I Write Clean Data Science Code EVERY TIME
THIS Is How I Write Clean Data Science Code EVERY TIME
Dave Ebbelaar
8 Data Science Tutorial - Project Structure
Data Science Tutorial - Project Structure
Dave Ebbelaar
Changing rcParams for Better Data Science Plots | Matplotlib Tutorial
Changing rcParams for Better Data Science Plots | Matplotlib Tutorial
Dave Ebbelaar
10 How to Read Excel Files with Python (Pandas Tutorial)
How to Read Excel Files with Python (Pandas Tutorial)
Dave Ebbelaar
11 My Data Science Journey (Zero to Freelance)
My Data Science Journey (Zero to Freelance)
Dave Ebbelaar
12 How I Automate Data Visualization in Python
How I Automate Data Visualization in Python
Dave Ebbelaar
13 16 Apps I Use Daily as a Data Scientist
16 Apps I Use Daily as a Data Scientist
Dave Ebbelaar
14 How to Manage Conda Environments for Data Science
How to Manage Conda Environments for Data Science
Dave Ebbelaar
15 How to Export Machine Learning Models in Python
How to Export Machine Learning Models in Python
Dave Ebbelaar
16 VS Code Speed Hack for Data Science
VS Code Speed Hack for Data Science
Dave Ebbelaar
17 17 VS Code Tips That Will Change Your Data Science Workflow
17 VS Code Tips That Will Change Your Data Science Workflow
Dave Ebbelaar
18 How to Predict the Future with Python (Forecasting Tutorial)
How to Predict the Future with Python (Forecasting Tutorial)
Dave Ebbelaar
19 How to Use Python Environment Variables
How to Use Python Environment Variables
Dave Ebbelaar
20 7 Data Science Tips for Beginners in 2023
7 Data Science Tips for Beginners in 2023
Dave Ebbelaar
21 How to Effectively Use the Data Science Lifecycle
How to Effectively Use the Data Science Lifecycle
Dave Ebbelaar
22 Full Machine Learning Project — Coding a Fitness Tracker with Python (Part 1)
Full Machine Learning Project — Coding a Fitness Tracker with Python (Part 1)
Dave Ebbelaar
23 Full Machine Learning Project — Processing Raw Data (Part 2)
Full Machine Learning Project — Processing Raw Data (Part 2)
Dave Ebbelaar
24 Full Machine Learning Project — Data Visualization with Matplotlib (Part 3)
Full Machine Learning Project — Data Visualization with Matplotlib (Part 3)
Dave Ebbelaar
25 This Will Change Data Science as We Know It (ChatGPT)
This Will Change Data Science as We Know It (ChatGPT)
Dave Ebbelaar
26 Full Machine Learning Project — Detecting Outliers in Sensor Data (Part 4)
Full Machine Learning Project — Detecting Outliers in Sensor Data (Part 4)
Dave Ebbelaar
27 Full Machine Learning Project — Low-pass Filter & Principal Component Analysis (Part 5a)
Full Machine Learning Project — Low-pass Filter & Principal Component Analysis (Part 5a)
Dave Ebbelaar
28 Full Machine Learning Project — Fourier Transformation & Clustering (Part 5b)
Full Machine Learning Project — Fourier Transformation & Clustering (Part 5b)
Dave Ebbelaar
29 Full Machine Learning Project — Predictive Modelling (Part 6)
Full Machine Learning Project — Predictive Modelling (Part 6)
Dave Ebbelaar
30 Automate Machine Learning with ChatGPT
Automate Machine Learning with ChatGPT
Dave Ebbelaar
31 Scraping Web Datasets for Data Science Projects
Scraping Web Datasets for Data Science Projects
Dave Ebbelaar
32 Full Machine Learning Project — Counting Repetitions (Part 7)
Full Machine Learning Project — Counting Repetitions (Part 7)
Dave Ebbelaar
33 How to Use GitHub Copilot for Data Science (Python + VS Code)
How to Use GitHub Copilot for Data Science (Python + VS Code)
Dave Ebbelaar
34 Every Beginner Data Scientist Should Understand This
Every Beginner Data Scientist Should Understand This
Dave Ebbelaar
35 Revealing My New AI-Powered Data Science Workflow
Revealing My New AI-Powered Data Science Workflow
Dave Ebbelaar
36 Auto-GPT Tutorial - Create Your Personal AI Assistant 🦾
Auto-GPT Tutorial - Create Your Personal AI Assistant 🦾
Dave Ebbelaar
37 Build Your Own Auto-GPT Apps with LangChain (Python Tutorial)
Build Your Own Auto-GPT Apps with LangChain (Python Tutorial)
Dave Ebbelaar
38 Building Slack AI Assistants with Python & LangChain
Building Slack AI Assistants with Python & LangChain
Dave Ebbelaar
39 ChatGPT Code Interpreter - Goodbye Data Analysts?
ChatGPT Code Interpreter - Goodbye Data Analysts?
Dave Ebbelaar
40 How to Deploy AI Apps to the Cloud with Flask & Azure
How to Deploy AI Apps to the Cloud with Flask & Azure
Dave Ebbelaar
41 How to Build an AI Document Chatbot in 10 Minutes
How to Build an AI Document Chatbot in 10 Minutes
Dave Ebbelaar
42 Is Falcon LLM the OpenAI Alternative? An Experimental Setup with LangChain
Is Falcon LLM the OpenAI Alternative? An Experimental Setup with LangChain
Dave Ebbelaar
43 GPT Engineer... Generate an entire codebase with one prompt
GPT Engineer... Generate an entire codebase with one prompt
Dave Ebbelaar
44 Pandas DataFrame Agent... the future of data analysis?
Pandas DataFrame Agent... the future of data analysis?
Dave Ebbelaar
45 OpenAI Function Calling - Full Beginner Tutorial
OpenAI Function Calling - Full Beginner Tutorial
Dave Ebbelaar
46 How to use ChatGPT's new “Code Interpreter” feature
How to use ChatGPT's new “Code Interpreter” feature
Dave Ebbelaar
47 LangChain just launched their new "LangSmith" platform
LangChain just launched their new "LangSmith" platform
Dave Ebbelaar
48 How I'd Learn AI (if I could start over)
How I'd Learn AI (if I could start over)
Dave Ebbelaar
49 I Used AI To Scrape The Web & Write PDF Reports
I Used AI To Scrape The Web & Write PDF Reports
Dave Ebbelaar
50 LangSmith Tutorial - LLM Evaluation for Beginners
LangSmith Tutorial - LLM Evaluation for Beginners
Dave Ebbelaar
51 7 Lessons for New AI Engineers - Beginner’s Guide
7 Lessons for New AI Engineers - Beginner’s Guide
Dave Ebbelaar
52 The Rise of the "New-Age" Machine Learning Engineer
The Rise of the "New-Age" Machine Learning Engineer
Dave Ebbelaar
53 OpenAI Assistants Tutorial for Beginners
OpenAI Assistants Tutorial for Beginners
Dave Ebbelaar
54 How To Connect OpenAI To WhatsApp (Python Tutorial)
How To Connect OpenAI To WhatsApp (Python Tutorial)
Dave Ebbelaar
55 How to Build Chatbot Interfaces with Python
How to Build Chatbot Interfaces with Python
Dave Ebbelaar
56 PostgreSQL as VectorDB - Beginner Tutorial
PostgreSQL as VectorDB - Beginner Tutorial
Dave Ebbelaar
57 My MacBook Setup (as a coder & business owner)
My MacBook Setup (as a coder & business owner)
Dave Ebbelaar
58 Easiest Way to Connect AI Chatbots to WhatsApp
Easiest Way to Connect AI Chatbots to WhatsApp
Dave Ebbelaar
59 ClickUp Tutorial - What Is ClickUp Brain? 🧠
ClickUp Tutorial - What Is ClickUp Brain? 🧠
Dave Ebbelaar
60 My Development Workflow for Data & AI Projects
My Development Workflow for Data & AI Projects
Dave Ebbelaar

Related Reads

Up next
Vidx.ai Doesn't Work Like They Claim—Here's Why
DroidCrunch
Watch →