- As part of the update to matplotlib 2.0 the edges on bar plots are turned off by default. However, this behavior can be globally changed with
rcParams.
plt.rcParams["patch.force_edgecolor"] = True
- Alternatively, set the
edgecolor / ec parameter in the plot call, and potentially increase the linewidth / lw parameter.
- Tested in
python 3.11.4, pandas 2.0.3, matplotlib 3.7.1, seaborn 0.12.2
Imports and Sample Data
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
np.random.seed(2023) # ensures the data is repeatable
x = np.random.randn(100)
df = pd.DataFrame(data=x, columns=['values'])
# precalculate the histogram values to plot a bar plot
counts, bins = np.histogram(x)
pandas
- Uses matplotlib as the default plotting backend.
ax = df.plot(kind='hist', ec="k")
matplotlib
.hist
plt.hist(x, ec="k")
.bar
fig, ax = plt.subplots()
ax.bar(x=range(len(counts)), height=counts, width=1, ec="k")
# sets the bin values at the bar edges
_ = ax.set_xticks(ticks=np.arange(0, len(bins)) - 0.5, labels=bins.round(2))
seaborn
- Is a high-level API for matplotlib.
- Figure-level vs. axes-level functions
ec="k" is the default setting. However, ec can be used to set a different color or None.
.histplot
ax = sns.histplot(data=df, x='values')
.displot
g = sns.displot(data=df, kind='hist', x='values')
.distplot
- Replaced by
histplot and displot.
- Set the edgecolor when creating a
distplot, using the hist_kws argument.
ax = sns.distplot(x, hist_kws=dict(edgecolor="k", linewidth=2))
Example Plot
