matplotlib

Documentation links are:

There are two methods to interface matplotlib:

  • The pyplot API, that has a subsection of the examples and a tutorial. Details are shown in a separate page. The documentation says:

    pyplot is mainly intended for interactive plots and simple cases of programmatic plot generation

The main parts of the object oriented API mentioned are the axes and figure documentation references.

  • The object-oriented API. This is described further down on the API page

Simple bar plotting example that colours bars in two colours.

This is based on the example from the documentation ‘Scores by group and gender’:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

labels = [    "G1", "G2", "G3", "G4",
              "G5", "G6", "G7", "G8"]
controls = [ False, True, False, True,
             True, True, False, False]
no_members = [30, 25, 22, 16, 15, 12, 5, 3]

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x, no_members, width)

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('No of members')
ax.set_title('Head Count')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.legend()


def autolabel(rects):
    """Attach a text label above each bar in *rects*, displaying its height."""
    count = 0
    for rect in rects:
        if controls[count]:
            rect.set_color('r')
        height = rect.get_height()
        ax.annotate('{}'.format(height),
                    xy=(rect.get_x() + rect.get_width() / 2, height),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')
        count = count + 1


autolabel(rects1)

fig.tight_layout()

plt.show()