Numbers as a picture
A table full of numbers has to be read; a chart you see at a glance. matplotlib is the library Python has drawn with for over twenty years – pandas from lesson 69 draws its charts with it too. We reach it through pycall, like pandas and NumPy. A week of temperatures, as a line:
require "pycall"
plt = PyCall.import_module("matplotlib.pyplot")
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
temps = [12.5, 15.0, 9.5, 21.0, 18.5, 23.0, 19.5]
plt.plot(days, temps, marker: "o")
plt.title("A week of weather")
plt.showplt is pyplot, matplotlib's drawing board. plot draws a line through the points – Ruby arrays go straight in, and marker: "o" is a keyword argument that puts a dot on every value. plt.show shows the picture: here below the cell, on your computer in a window of its own.
matplotlib is big, so the first import takes a few seconds. The page starts it as soon as you open the lesson; after that, every chart is there in a moment.
After show the board is empty again. A bar chart, with a colour and a label on the y axis:
ice_creams = [40, 55, 20, 90, 70, 120, 85]
plt.bar(days, ice_creams, color: "orange")
plt.ylabel("ice creams sold")
plt.showSaturday, the warmest day, sold the most ice creams – you see it at once.
plt.plot and plt.bar always draw into the current picture. As soon as there is more than one chart, it is better to say where: plt.subplots returns two things, the whole picture (the figure) and the drawing area in it (the axes). In Python that is a tuple, and pycall turns it into a Ruby array – so Ruby's multiple assignment hands them to fig and ax:
fig, ax = plt.subplots(figsize: [6, 3])
ax.plot(days, temps, color: "tomato", marker: "o")
ax.set_title("Temperature")
ax.set_ylabel("°C")
plt.showThe drawing area has the same tools, only the labels have set_ in front: set_title, set_ylabel. figsize: [6, 3] is the size in inches. What it would look like in Python:
| Python | Ruby with pycall |
|---|---|
fig, ax = plt.subplots() | fig, ax = plt.subplots |
plt.plot(x, y, marker="o") | plt.plot(x, y, marker: "o") |
figsize=(6, 3) | figsize: [6, 3] |
ax.legend() | ax.legend |
With NumPy from lesson 71 we get curves: linspace makes 100 points between 0 and 2π, two plots land on the same area, and label: names each line for the legend:
np = PyCall.import_module("numpy")
x = np.linspace(0, 2 * np.pi, 100)
fig, ax = plt.subplots(figsize: [6, 3])
ax.plot(x, np.sin(x), label: "sin")
ax.plot(x, np.cos(x), label: "cos")
ax.legend
plt.showTwo charts side by side: plt.subplots(1, 2) makes one row with two areas. The second thing it returns is then a NumPy array holding both. In Python you would write fig, (left, right) = …; with pycall the array stays a Python object, so you take the areas out with axes[0] and axes[1].
And plt.savefig writes the picture to a file – before show, which empties the board. You can download it below the cell:
fig, axes = plt.subplots(1, 2, figsize: [9, 3])
left = axes[0]
right = axes[1]
left.plot(days, temps, color: "tomato")
left.set_title("Temperature")
right.scatter(temps, ice_creams)
right.set_title("Ice creams by temperature")
plt.savefig("weather.png")
plt.showOn the right, a scatter plot: the warmer, the more ice creams – exactly what scikit-learn learns as a straight line in the next lesson.
pip install matplotlib and gem install pycall, and the code of this lesson runs unchanged – plt.show opens a window, savefig writes the file next to your program. matplotlib has a free licence of its own, modelled on Python's; the fonts it brings along (DejaVu, STIX) are free as well.
Fox hunt and show it with plt.show.require "pycall"
plt = PyCall.import_module("matplotlib.pyplot")
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
mice = [3, 5, 2, 6, 4, 7, 1]
# a bar chart titled "Fox hunt", then plt.show