Skip to content

Instantly share code, notes, and snippets.

@mgbckr
Last active February 2, 2020 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgbckr/993b395fb1a754582e7540720f6d0a5f to your computer and use it in GitHub Desktop.
Save mgbckr/993b395fb1a754582e7540720f6d0a5f to your computer and use it in GitHub Desktop.
Matplotlib subplots legend bottom center; does not work in Jupyter but the output is fine
import matplotlib.pyplot as plt
import matplotlib.lines
# matplotlib.rcParams["legend.frameon"] = False # need to set this once to enable styling??? WTF???
titles = ["title 1", "title 2", "title 3", "title 4"]
colors = ['#ffd600', '#f44336', '#43a047', '#1e88e5', '#ab47bc', '#3f51b5', '#f57c00']
# init figure / plot
fig, axes = plt.subplots(1, len(titles), figsize=(5*len(titles) - 3, 5))
for i_t, ax in enumerate(axes):
# ax.axis("off")
ax.set_title(titles[i_t], fontsize=22)
# legend elements
elements = [
matplotlib.lines.Line2D(
[0],[0],
marker='o',
color="w",
label=m,
markerfacecolor=c,
markersize=18,
linestyle = 'None')
for m,c in zip(
[f"entry {i}" for i in range(7)],
colors)]
# legend
legend = fig.legend(
handles=elements,
handlelength=0.5,
fancybox=True,
framealpha=.8,
shadow=False,
borderpad=0.4,
frameon=True,
bbox_to_anchor=(0, -0.05, 1, 1),
loc='lower center',
fontsize=22,
ncol=4)
legend.get_frame().set_linewidth(2)
plt.subplots_adjust(left=0.05, right=0.95, bottom=0.25, wspace=.15)
# save figure
plt.savefig("../out/test.png")
@mgbckr
Copy link
Author

mgbckr commented Feb 2, 2020

pregnancy_multiomics_correlation_network_test

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