Change Background Color of Matplotlib Subplot Based on Value

Change Background Color of Subplots According to Values

Recently, while working on a Python data visualization project, I wanted to highlight how different metrics performed across multiple plots. Each subplot represented a different region in the United States, and I wanted the background color of each subplot to reflect its performance level, green for good, yellow for average, and red for poor. In … Read more >>

Save a Matplotlib Plot as a Transparent PNG in Python

Save Matplotlib Plot as Transparent PNG in Python

Working on a Python data visualization project, I needed to save a Matplotlib plot with a transparent background. I was preparing a presentation for a U.S.-based client, and the slides had a dark theme. When I added my regular Matplotlib chart, it looked odd with a white background. That’s when I realized, Matplotlib actually makes … Read more >>

Build a Simple React Tree View Component

Build React Tree View Component

As a developer with over a decade of experience in web technologies, I often turn to React when I need to build interactive front-end experiences. Recently, I needed a Tree View component for a dashboard project, one that could display hierarchical data, such as folders and files, in a clean, collapsible way. If you’ve ever … Read more >>

How to Change Label Text in Tkinter

Change Label Text in Tkinter

Changing the text of a label is one of the most common tasks you will face when building a desktop application with Python. I remember when I first started using Tkinter; I thought I had to destroy the label and create a new one every time the data changed. It took me a while to … Read more >>

How to Change the Background Color in Matplotlib

Change the background color in Matplotlib: a line chart with a light blue figure background and a cream axes background

To change the background color in Matplotlib, set the facecolor: ax.set_facecolor(“lightyellow”) colours the plot area inside the axes, and fig.set_facecolor(“lightblue”) (or plt.figure(facecolor=…)) colours the figure around it. To change the default background color for every plot, set plt.rcParams[“axes.facecolor”] and plt.rcParams[“figure.facecolor”]. This guide covers all of these, plus dark mode with dark_background, transparent PNGs, subplots, the … Read more >>

How to Find the First Number in a String in Python

Find the first number in a string in Python with re.search and the d+ pattern

To find the first number in a string in Python, use re.search(r”\d+”, text). It returns the first run of digits, or None if the string has no digits, so always check the match before calling .group(). Below: a reusable function, versions without regex, how to handle negative numbers, decimals and thousands separators, and a few … Read more >>

How to Get the First and Last Digit of a Number in Python

Get the first and last digit of a number in Python: Command Prompt output of a function tested on positive, negative and large numbers

To get the last digit of a number in Python, use the modulo operator: number % 10. To get the first digit of a number in Python, convert it to a string and take the first character: int(str(number)[0]). Both work for any size of integer; use abs() first if the number can be negative. This … Read more >>

Save Matplotlib Table as PDF in Python

Save Matplotlib Table as PDF in Python

Recently, while working on a Python data visualization project for a US-based retail analytics company, I needed to export a summary table created with Matplotlib into a clean, printable PDF format. At first, I assumed there would be a direct function in Matplotlib to save tables as PDFs, but I quickly realized that the process … Read more >>

Save Multiple Pages to a PDF in Matplotlib

Save Multiple Pages to PDF in Matplotlib

Working on a Python data visualization project where I needed to generate multiple charts and share them with my team in a single, printable PDF file. I tried saving each Matplotlib plot separately, but managing and sending multiple files quickly became messy. That’s when I discovered a simple and powerful way to save multiple Matplotlib … Read more >>

Matplotlib Axis Label Font Size

Matplotlib Axis Label Font Size

When I first started building data visualizations in Python, I often found myself squinting at the screen. The default plots were great, but the axis labels were almost always too small to read once I put them into a presentation. It took me some time to realize that Matplotlib gives you total control over typography. … Read more >>