How to create a grouped bar plot

Pandas will show grouped bars by columns. Entries in each row but different columns will constitute a group in the resulting plot. Hence you need to “reshape” your dataframe to have the “group” as columns. In this case you can pivot like df.pivot(“column”, “group”, “val”) producing group g1 g2 column c1 10 8 c2 12 … Read more

How to add edge color to a histogram

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, … Read more

How to format axis tick labels from number to thousands or Millions (125,436 to 125.4K)

IIUC you can format the xticks and set these: In[60]: #generate some psuedo data df = pd.DataFrame({‘num’:[50000, 75000, 100000, 125000], ‘Rent/Sqft’:np.random.randn(4), ‘Region’:list(‘abcd’)}) df Out[60]: num Rent/Sqft Region 0 50000 0.109196 a 1 75000 0.566553 b 2 100000 -0.274064 c 3 125000 -0.636492 d In[61]: import matplotlib.pyplot as plt import matplotlib.ticker as ticker import seaborn as … Read more

Avoiding overlapping datapoints in a “scatter/dot/beeswarm” plot

Extending the answer by @user2467675, here’s how I did it: def rand_jitter(arr): stdev = .01 * (max(arr) – min(arr)) return arr + np.random.randn(len(arr)) * stdev def jitter(x, y, s=20, c=”b”, marker=”o”, cmap=None, norm=None, vmin=None, vmax=None, alpha=None, linewidths=None, verts=None, hold=None, **kwargs): return scatter(rand_jitter(x), rand_jitter(y), s=s, c=c, marker=marker, cmap=cmap, norm=norm, vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths, **kwargs) The stdev … Read more

3D scatterplots with hue colormap and legend

The color palette from Seaborn can be turned into a Matplotlib color map from an instance of a ListedColorMap class initialized with the list of colors in the Seaborn palette with the as_hex() method (as proposed in this original answer). From the Matplotlib documentation, you can generate a legend from a scatter plot with getting … Read more

Extracting clusters from seaborn clustermap

While using result.linkage.dendrogram_col or result.linkage.dendrogram_row will currently work, it seems to be an implementation detail. The safest route is to first compute the linkages explicitly and pass them to the clustermap function, which has row_linkage and col_linkage parameters just for that. Replacing the last line in your example (result = …) with the following code … Read more

Seaborn ValueError: zero-size array to reduction operation minimum which has no identity

This issue seems to be resolved for matplotlib==3.3.2. seaborn: Scatterplot fails with matplotlib==3.3.1 #2194 With matplotlib version 3.3.1 A workaround is to send a list to hue, by using .tolist() Use hue=tips.time.tolist(). The normal behavior adds a title to the legend, but sending a list to hue does not add the legend title. The legend … Read more

Add minor gridlines

Wound up combining CT Zhu’s answer with tcaswell’s hint: import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt import seaborn as sbn x = np.linspace(0, 2 * np.pi, 100) y = np.sin(x) fig, ax = plt.subplots(1, 1) ax.scatter(x, y) ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) ax.grid(b=True, which=”major”, color=”w”, linewidth=1.0) ax.grid(b=True, which=”minor”, color=”w”, linewidth=0.5)

Show confidence limits and prediction limits in scatter plot

Here’s what I put together. I tried to closely emulate your screenshot. Given import numpy as np import scipy as sp import scipy.stats as stats import matplotlib.pyplot as plt %matplotlib inline # Raw Data heights = np.array([50,52,53,54,58,60,62,64,66,67,68,70,72,74,76,55,50,45,65]) weights = np.array([25,50,55,75,80,85,50,65,85,55,45,45,50,75,95,65,50,40,45]) Two detailed options to plot confidence intervals: def plot_ci_manual(t, s_err, n, x, x2, y2, ax=None): … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)