Skip to content

Instantly share code, notes, and snippets.

@shamilnabiyev
Last active September 21, 2022 08:52
Show Gist options
  • Save shamilnabiyev/4925276c74b87efdddc7afea75b59e68 to your computer and use it in GitHub Desktop.
Save shamilnabiyev/4925276c74b87efdddc7afea75b59e68 to your computer and use it in GitHub Desktop.
Matplotlib / seaborn scatter plot. Colors on condition. Custom color palette.
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 5))

a = pd.Series(np.random.randint(60, 180, 25))
b = pd.Series(np.random.randint(55, 160, 25))

x_min = min(min(a), min(b))
y_max = max(max(a), max(b))

sns.scatterplot(a, b, ax=ax1)
ax1.plot([x_min, y_max], [x_min, y_max], ":", color="grey")
ax1.set_title("Model training runtime (Experiment #2)", size=16)
ax1.set_xlabel("User-defined runtime (sec.)", size=14)
ax1.set_ylabel("Actual runtime (sec.)", size=14)

data=pd.DataFrame({"a":a, "diff":(b-a), "cond":((b-a) <= 0) * 1})
sns.scatterplot(x="a", y="diff", data=data, ax=ax2, hue="cond", 
                palette={0: "tab:orange", 1: "tab:green"}, legend = False)
ax2.axhline(y=0, xmin=a.index.min(), xmax=a.index.max(), linestyle=":", color="grey")
ax2.set_title("Runtime difference in seconds (lower is better)", size=16)
ax2.set_ylabel("Runtime difference (sec.)", size=14)
ax2.set_xlabel("User-defined runtime (sec.)", size=14)

plt.show()

Output:

image

@shamilnabiyev
Copy link
Author

Create multiple boxplots from a pandas dataframe and change the font size of axis labels and ticks.

dfx = pd.DataFrame({
    "dataset": ["#1", "#2", "#3"] * 40,
    "test_accuracy": np.random.rand(120)
})

fig, ax = plt.subplots(1, 1, figsize=(12, 4))

boxplot = dfx[["dataset", "test_accuracy"]].boxplot(by="dataset", ax=ax, grid=False)

fig.suptitle("")
ax.set_title("Boxplot of test accuracy for each dataset", fontsize=16)
ax.set_xlabel("Dataset", fontsize=14)
ax.set_ylabel("Test accuracy", fontsize=14)

ax.xaxis.set_tick_params(labelsize=12)
ax.yaxis.set_tick_params(labelsize=12)
plt.show()

Output:

multiple-boxplots

@shamilnabiyev
Copy link
Author

Seaborn barplots with error error bars (std, CI etc.) seaborn.barplot

Examples:

df = sns.load_dataset("penguins")
sns.barplot(data=df, x="island", y="body_mass_g", errorbar="sd")

image

sns.barplot(
    data=df, x="body_mass_g", y="island",
    errorbar=("pi", 50), capsize=.4, errcolor=".5",
    linewidth=3, edgecolor=".5", facecolor=(0, 0, 0, 0),
)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment