Raincloud plots are a great way to visualise your data. Instead of relying on boxplots, violin plots and other data distribution methods that can skew the data, you can using a single raincloud plot.
A raincloud plot stacks three views of a distribution in one: a half-violin for the shape, which forms the cloud, the raw points jittered underneath to form the rain, and a box plot to show the distribution summary. These charts provide a quick way to read beyond what a simple boxplot can provide. I have covered this in the past, that a boxplot simply generates a summary of several key statistics of the data and can be misleading.
For my past articles on the Raincloud plot, you can check out the links below.
From Boxplot to Raincloud: Building a Better Distribution Plot in Python
Raincloud Plots with PtitPrince: See What Your Data Is Really Doing
After building a raincloud plot, the next questions to ask are:
How to I customise it to suit my needs?
Can I split the plot by a second variable?
How do I draw attention to a specific group?
How can I show trends across multiple wells in a field?
Within this article, we are going to look at 6 ways we can customise a raincloud plot that is generated through ptitprince (a library that makes plotting raincloud plots easier)
The dataset and a baseline chart
For this walkthrough, we are going to reuse the synthetic porosity dataset from the last article. This contains four illustrative formations with deliberately different distribution shapes, two roughly normal, one bimodal, one skewed. The names and values are made up rather than real names.
Whilst this example and walkthrough is focused on well log data, it can easily be applied to any dataset.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import ptitprince as pt
rng = np.random.default_rng(42)
formations = {
“Vardan”: rng.normal(0.21, 0.025, 40),
“Kelvik”: rng.normal(0.15, 0.02, 40),
“Stenby”: np.concatenate([rng.normal(0.10, 0.015, 20),
rng.normal(0.18, 0.015, 20)]),
“Morvik”: rng.beta(2, 5, 40) * 0.25,
}
df = pd.DataFrame(
[(name, val) for name, vals in formations.items() for val in vals],
columns=[”Formation”, “Porosity”],
)
def tidy(ax):
“”“Shared styling so each example stays focused on one change.”“”
ax.set_xlabel(”Porosity (v/v)”, fontsize=12)
ax.set_ylabel(”“)
ax.spines[[”top”, “right”]].set_visible(False)
ax.xaxis.grid(True, linestyle=”--”, alpha=0.4)
ax.set_axisbelow(True)
np.random.seed(42)
f, ax = plt.subplots(figsize=(10, 7))
pt.RainCloud(x=”Formation”, y=”Porosity”, data=df, ax=ax,
orient=”h”, palette=”colorblind”, alpha=0.65,
width_viol=0.7, width_box=0.25,
point_size=3, jitter=0.08, move=0.2)
tidy(ax)
ax.set_title(”Baseline Raincloud”, fontsize=14, fontweight=”bold”,
loc=”left”, pad=15)
plt.show()When we run the above code, we get the following baseline image of our data. One thing to note, the jitter and point position for the plot’s rain can change each tome the plot is run, by calling np.random.seed(42) before plotting we can fix these in place.
Baseline chart for the raincloud using ptitprince.
1. Split each cloud by a second variable
A raincloud is setup to compare one property across different groups. However, there are times when we may want to see how a second category or property compares within each group.
For example, if we have done a Machine Learning prediction, we may want to visualise how the actual data compares with the predicted data.
In the example below we are comparing measured porosity with predicted porosity.
# We will deliberately modify the porosity for plotting purposes.
pred = df.groupby(”Formation”)[”Porosity”].transform(
lambda s: s.mean() + 0.6 * (s - s.mean()))
pred = pred + rng.normal(0, 0.008, len(pred))
df_comp = pd.concat([
df.assign(Source=”Core”),
df.assign(Porosity=pred, Source=”Predicted”),
])
np.random.seed(42)
f, ax = plt.subplots(figsize=(10, 8))
pt.RainCloud(x=”Formation”, y=”Porosity”, hue=”Source”, data=df_comp, ax=ax,
orient=”h”, dodge=True, alpha=0.6,
hue_order=[”Core”, “Predicted”],
palette=[”#0173b2”, “#de8f05”],
width_viol=0.6, width_box=0.18,
point_size=2.5, jitter=0.06, move=0.2,
box_showfliers=False)
handles, labels = ax.get_legend_handles_labels()
by_label = dict(zip(labels, handles))
ax.legend(by_label.values(), by_label.keys(), title=”Porosity Source”,
loc=”lower right”, frameon=False)
tidy(ax)
ax.set_title(”The Model Compresses Every Distribution”,
fontsize=14, fontweight=”bold”, loc=”left”, pad=15)
plt.show()If we were to look at just the boxplots above, we would see that the means are fairly similar between actual and predicted for the first three groups, but slightly off on the last group.
By adjusting three settings we can make the paired category layout work.
dodge=True places each pair side by side rather than on top of each other.
hue_order with an explicit two-colour palette pins which category gets which colour, so core is always blue and predicted always orange regardless of the order the data arrives in.
box_showfliers=False keeps the boxplot layer from drawing outlier markers that duplicate points already visible in the rain.
By doing this with more than two or three categories across different groups, the plot may start to get too busy and becomes difficult to read.
2. Highlight one group
When working with charts we often want to amke a point to the reader about one particular group. One way we can do this is by dulling down everything apart form the group we want to highlight.
This allows the reader’s eye to land where you want it without a caption telling them where to look.
For this example, we can pass in an ordered list of colours to the palette keyword and matching the group order on the axis.
highlight = [”#d0d0d0”, “#d0d0d0”, “#c44e52”, “#d0d0d0”]
np.random.seed(42)
f, ax = plt.subplots(figsize=(10, 7))
pt.RainCloud(x=”Formation”, y=”Porosity”, data=df, ax=ax,
orient=”h”, palette=highlight, alpha=0.75,
width_viol=0.7, width_box=0.25,
point_size=3, jitter=0.08, move=0.2)
tidy(ax)
ax.set_title(”Stenby Shows Two Distinct Porosity Populations”,
fontsize=14, fontweight=”bold”, loc=”left”, pad=15)
plt.show()3. Connect the means
If we are comparing multiple groups of data that have some form of sequence, then we may want to draw a line between the means to help illustrate how they differ.
This is often the case when working with geological data, where we may have low porosity zones and high porosity zones one after another. In this case, we may want to see that trend when comparing different wells with the same geological formations.
Setting pointplot=True adds a mean marker to each group and connects them with a line, turning the raincloud into a distribution-aware trend chart.
np.random.seed(42)
f, ax = plt.subplots(figsize=(10, 7))
pt.RainCloud(x=”Formation”, y=”Porosity”, data=df, ax=ax,
orient=”h”, palette=”colorblind”, alpha=0.65,
width_viol=0.7, width_box=0.25,
point_size=3, jitter=0.08, move=0.2,
pointplot=True, linecolor=”grey”)
tidy(ax)
ax.set_title(”Mean Porosity Trend Across Formations”,
fontsize=14, fontweight=”bold”, loc=”left”, pad=15)
plt.show()Use this one with a little care, as the connecting line may cause the reader to imply the group order is meaningful.
4. Tune the cloud’s smoothing and range
The cloud is a kernel density estimate, and two arguments control how it’s drawn:
bw sets the smoothing bandwidth: larger values give smoother, vaguer shapes; smaller values track the data more closely and eventually track noise.
cut=0 stops the cloud extending past the actual data range, which for a bounded property like porosity prevents the estimate implying impossible values, e.g. negative porosity values.
f, axes = plt.subplots(1, 2, figsize=(14, 6), sharey=True)
for ax, bw, label in zip(axes, [0.5, 0.15],
[”bw=0.5 (smoother)”, “bw=0.15 (more detail)”]):
np.random.seed(42)
pt.RainCloud(x=”Formation”, y=”Porosity”, data=df, ax=ax,
orient=”h”, palette=”colorblind”, alpha=0.65,
width_viol=0.7, width_box=0.25,
point_size=2.5, jitter=0.08, move=0.2,
bw=bw, cut=0)
tidy(ax)
ax.set_title(label, fontsize=13, loc=”left”, pad=10)
plt.tight_layout()
plt.show()Because we have set the seed to a fixed value before each panel, the rain is identical on both sides, and the differences you can see on the cloud is related to the differences in the smoothing applied.
Looking at Stenby in both panels, we can see that the heavier smoothing its two populations blur towards a single broad hump; at the lighter setting they separate clearly. This is the same data, but we can draw two very different conclusions from it, especially if we just relied on the kde alone.
5. Style the rain
The rain layer can be thought of as the honest layer as it shows the real data points, something that is often hidden by using boxplots
Three arguments control its geometry (point_size, jitter, move), and because ptitprince forwards anything prefixed with rain_ to the underlying strip plot, you can also set edge colours and per-layer transparency.
By adding a thin dark outline we can keep light-coloured points visible when the figure is printed or displayed on a larger screen.
np.random.seed(42)
f, ax = plt.subplots(figsize=(10, 7))
pt.RainCloud(x=”Formation”, y=”Porosity”, data=df, ax=ax,
orient=”h”, palette=”colorblind”, alpha=0.65,
width_viol=0.7, width_box=0.25,
point_size=4, jitter=0.1, move=0.25,
rain_edgecolor=”black”, rain_linewidth=0.3, rain_alpha=0.8)
tidy(ax)
ax.set_title(”Outlined Rain Points for Print”,
fontsize=14, fontweight=”bold”, loc=”left”, pad=15)
plt.show()We can now see the rain points stand out a little better, however, if we have a lot of data this can become noisy.
The same prefix trick works for the other layers: cloud_ arguments go to the half-violin and box_ arguments go to the boxplot.
6. Go vertical when the groups form a sequence
All of the charts we have looked at so far have been horizontal, which is a good way to deal with a small number of categories. However, if we have many and want to view changes across an ordered sequence such as a traverse across a field, then we can rotate the plots 90 degrees and view them like a transect.
This can also be applied to time series data, where you may have a series of measurements every 30 days and you want to see how the properties change with time.
rng2 = np.random.default_rng(7)
wells = {f”W-{i:02d}”: rng2.normal(mu, 0.02, 40)
for i, mu in enumerate(
[0.22, 0.21, 0.19, 0.16, 0.15, 0.12, 0.11, 0.09], start=1)}
df_wells = pd.DataFrame(
[(name, val) for name, vals in wells.items() for val in vals],
columns=[”Well”, “Porosity”],
)
np.random.seed(42)
f, ax = plt.subplots(figsize=(14, 6))
pt.RainCloud(x=”Well”, y=”Porosity”, data=df_wells, ax=ax,
orient=”v”, palette=”crest”, alpha=0.65,
width_viol=0.6, width_box=0.2,
point_size=2.5, jitter=0.06, move=0.2,
box_showfliers=False)
ax.set_ylabel(”Porosity (v/v)”, fontsize=12)
ax.set_xlabel(”“)
ax.set_title(”Porosity Decline Along the Field Traverse”,
fontsize=14, fontweight=”bold”, loc=”left”, pad=15)
ax.spines[[”top”, “right”]].set_visible(False)
ax.yaxis.grid(True, linestyle=”--”, alpha=0.4)
ax.set_axisbelow(True)
plt.tight_layout()
plt.show()Three adjustments make the vertical version work across eight wells.
The figure is wide rather than square which gives each well a usable column.
The components are slimmed down (width_viol, width_box, point_size, jitter all reduced) so neighbouring wells don’t overlap and collide with each other.
The grid also moves with the orientation: it’s the value axis that needs gridlines, which is now the y axis.
If you keep a helper function like tidy() across projects, it’s worth adding an orientation argument to help with this.
Summary
Raincloud plots are fast becoming one of my favourite plotting methods. Rather than just relying on individual plots, we can combine the power of three into one useful visual.
The six examples above each solve different problems, but can make or break the chart and the message you are trying to convey. In particular ensuring you have correctly set the bandwidth can help reduce the reader drawing incorrect conclusions from your visualisation.
Every customisation here assumes the raincloud was the right chart to start with, and for distribution comparisons it often is. But it sits alongside ridgeline plots, bump charts and beeswarm plots as visualisation types that rarely get reached for despite regularly beating the default choice. Over on Substack I’ve put together a piece covering all three, with before-and-after examples using well log data.










Nice work, Andy. Clear examples of the power of the plot combination. Seems a very useful tool. Thanks for the article.