A first plot

plot a line, a bar chart, and why plt.show() is required.

A plot is data plus a figure. plt.plot draws a line; plt.bar draws bars. The last line is always plt.show().

Goal

Draw a line and a bar chart, and see both in the Plot panel.

Line

xs = [0, 1, 2, 3, 4, 5]
ys = [0, 1, 4, 9, 16, 25]
plt.plot(xs, ys, marker="o")
plt.title("n squared")
plt.xlabel("n")
plt.ylabel("n²")
plt.show()

Bar

cities = ["Nairobi", "Mombasa", "Kisumu"]
units = [21, 15, 15]
plt.bar(cities, units)
plt.title("Units by city")
plt.ylabel("units")
plt.show()

NumPy helper

x = np.linspace(0, 2 * np.pi, 80)
plt.plot(x, np.sin(x), label="sin")
plt.plot(x, np.cos(x), label="cos")
plt.legend()
plt.title("sin and cos")
plt.show()

Each block is a complete script. The workbench clears the Plot panel on every Run.

Pitfall

Without plt.show(), the Plot panel stays empty. Do not paste Jupyter magics.