NumPy unique: Values, Counts and Unique Rows

NumPy unique: the Command Prompt showing unique values with their counts and the most common value

np.unique() returns the distinct values of a NumPy array, sorted, with duplicates removed. Add return_counts=True and you get how often each one appeared: The sorting is not optional, which is the first thing that surprises people coming from pandas. The axis argument is the second. Runs below are on NumPy 2.5.3, pandas 3.0.6, Python 3.12.5. … Read more >>

Set xticks Range and Interval in Matplotlib

Set xticks Range in Matplotlib

I have spent over a decade building data visualizations in Python, and if there is one thing I’ve learned, it’s that default axis ticks are rarely perfect. Often, Matplotlib tries to be helpful by guessing where your ticks should go, but it often ends up cluttering the x-axis or skipping vital data points. In this … Read more >>

Matplotlib Boxplot: Set X-Axis Tick Labels

Matplotlib Boxplot Set X-Axis Tick Labels

In my decade of working as a Python developer, I have spent countless hours staring at data visualizations. One of the most common challenges I see beginners face is making their charts readable for a non-technical audience. When I first started building Python data models, I often struggled with messy axis labels. My boxplots would … Read more >>

Matplotlib set_xticklabels: Custom X-Axis Labels

Matplotlib set_xticklabels: a bar chart with numeric labels, month names, and month names rotated and right-aligned

set_xticklabels replaces the text under each x-axis tick in Matplotlib. It changes the words, not where the ticks sit, so the positions have to be settled first: Skip that first line and Matplotlib warns you, because it can no longer promise the labels line up with the data. There’s a newer one-call form too, further … Read more >>

Python Percent Symbol (%)

percentage symbol in python

In my decade of building Python applications, I’ve found that few symbols are as versatile as the percent sign (%). While most beginners think it’s just for calculating remainders, it actually wears many hats in a professional codebase. I remember early in my career when I had to format complex financial reports for a New … Read more >>

Matplotlib fill_between: Shading Between Two Curves

Matplotlib fill_between: three sine plots showing a basic fill, conditional shading with gaps, and the same with interpolate set to True

fill_between shades the area between two curves in Matplotlib. Give it the x values and one y series and it fills down to zero; give it two and it fills the gap between them. The two arguments that cause trouble are where=, for shading only part of the range, and interpolate=True, which you almost always … Read more >>

NumPy Factorial: Why np.factorial Fails and What to Use

NumPy factorial: the Command Prompt showing that np.factorial does not exist and raises an AttributeError

There is no NumPy factorial function. np.factorial() doesn’t exist and never has, which is why you’re here after an AttributeError. Use math.factorial for a single integer and scipy.special.factorial for a whole array. The rest of this page is the detail that decides between them. Versions used below: NumPy 2.5.3, SciPy 1.18.1, Python 3.12.5. Why does … Read more >>

Python Program to Find the Area of a Rectangle

Python program to find the area of a rectangle: the Command Prompt showing the length, width, area and perimeter printed

The area of a rectangle in Python is length * width. That’s the whole calculation, and here it is as a program you can run: What people usually need on top of that is the version that asks the user for the numbers, and one that survives being given nonsense. Both are below. Each program … Read more >>

Matplotlib set_xticks: Tick Positions and Labels

Matplotlib set_xticks: the same sine curve with default ticks, custom positions, and positions with pi labels

ax.set_xticks() decides where the ticks sit on a Matplotlib x-axis. You pass positions in data coordinates, and optionally the labels to go with them: Positions first. That’s the detail behind most of the trouble people have with it, because setting labels without positions is a different and less reliable thing. Figures and output below are … Read more >>