Skip to content

Instantly share code, notes, and snippets.

@dmyersturnbull
Last active November 21, 2016 23:18
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 dmyersturnbull/dce80c1bccf2bf87e645da90246a920a to your computer and use it in GitHub Desktop.
Save dmyersturnbull/dce80c1bccf2bf87e645da90246a920a to your computer and use it in GitHub Desktop.
Colored barplot in Matplotlib that makes sense and doesn't look terrible.
# Douglas Myers-Turnbull wrote this for the Kokel Lab, which has released it under the Apache Software License, Version 2.0
# See the license file here: https://gist.github.com/dmyersturnbull/bfa1c3371e7449db553aaa1e7cd3cac1
# The list of copyright owners is unknown
import matplotlib.pyplot as plt
import numpy as np
from typing import Optional, Tuple
def colored_barplot(x: np.ndarray, y: np.ndarray, colors: np.ndarray, y_ticks: Optional[np.ndarray]=None, fig_size: Tuple[float, float]=(10.0, 10.0), label_rotation: float=75):
index = np.arange(0, len(x))
fig = plt.figure()
fig.set_size_inches(fig_size)
ax = fig.add_subplot(111)
plot = ax.bar(index, y, color=colors, align='center')
ax.set_xticks(index)
ax.autoscale() # otherwise there's whitespace on the rhs
if y_ticks is not None:
ax.set_yticks(y_ticks)
ax.set_xticklabels(x, rotation=label_rotation)
return plot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment