Which chart answers which question?

Compare stocks with a bar chart, relate risk to return with a scatter plot, and show a distribution of returns with a histogram in matplotlib.

After this lesson, you can pick a bar chart to compare stocks, a scatter plot to relate two numbers and a histogram to show how returns are spread.

How do I compare the stocks’ returns?

A bar chart draws one bar per stock with a length equal to its value, so ax.bar(returns.index, returns[2025]) compares the three stocks’ 2025 returns, CCC’s 10% against AAA’s 4% and BBB’s −2%. ax.bar_label writes each value at the end of its bar.

The made-up table holds the yearly returns in percent of AAA, BBB and CCC from 2022 to 2025, one row per stock, and their volatilities in 2025. The value axis must include zero so the lengths stay fair, and ax.axhline(0) draws it. ax.barh draws horizontal bars, which leave more room for long names.

The following box builds the data and draws the bars.

import matplotlib.pyplot as plt
import pandas as pd

returns = pd.DataFrame([[-8, 6, 12, 4], [3, -4, 5, -2], [1, 7, -3, 10]], index=["AAA", "BBB", "CCC"], columns=[2022, 2023, 2024, 2025])
volatility = pd.Series([10, 6, 18], index=["AAA", "BBB", "CCC"])
print(returns)
fig, ax = plt.subplots(figsize=(5, 3))
ax.bar_label(ax.bar(returns.index, returns[2025]), padding=2)
ax.axhline(0, color="black")
ax.set(ylabel="Return in 2025 (%)", ylim=(-5, 13))
plt.show()
     2022  2023  2024  2025
AAA    -8     6    12     4
BBB     3    -4     5    -2
CCC     1     7    -3    10

How do I show how risk relates to return?

A scatter plot draws one point per stock from two numbers, so ax.scatter(volatility, returns[2025]) puts each stock at its volatility across and its return up, AAA at (10, 4), BBB at (6, −2) and CCC at (18, 10). The two series must list the stocks in the same order.

ax.annotate writes each ticker next to its point, and xlim and ylim set the visible ranges, so a point outside them would be hidden. Three points show how the chart works, and a study of risk and return needs many more stocks.

The following box draws the points.

fig, ax = plt.subplots(figsize=(5, 3))
ax.scatter(volatility, returns[2025])
for ticker in returns.index:
    ax.annotate(ticker, (volatility[ticker], returns.loc[ticker, 2025]))
ax.axhline(0, color="black")
ax.set(xlabel="Volatility in 2025 (%)", ylabel="Return in 2025 (%)", xlim=(0, 20), ylim=(-5, 15))
plt.show()

How do I show how a stock’s returns are spread?

A histogram splits a range into intervals and draws a bar with the count of values in each, so ax.hist(returns.loc["AAA"], bins=[-10, 0, 10, 20]) counts AAA’s four yearly returns as 1 below zero, 2 from 0 to 10 and 1 from 10 to 20. Each interval includes its left edge, and the last one also its right edge.

ax.hist returns the counts, the edges and the bars. bins=3 would split the range of the data into three equal intervals instead, and values outside given edges are left out.

The following box draws the histogram and prints its edges and counts.

fig, ax = plt.subplots(figsize=(5, 3))
counts, edges, bars = ax.hist(returns.loc["AAA"], bins=[-10, 0, 10, 20], edgecolor="white")
ax.set(xlabel="AAA's yearly return (%)", ylabel="Number of years")
print(edges.tolist(), counts.tolist())
plt.show()
[-10.0, 0.0, 10.0, 20.0] [1.0, 2.0, 1.0]

Your turn

Draw AAA’s histogram with the edges −10, 5 and 15 and print the counts. You should see two years in each interval.

Only the edges change.

fig, ax = plt.subplots(figsize=(5, 3))
counts, edges, bars = ax.hist(returns.loc["AAA"], bins=[-10, 5, 15], edgecolor="white")
ax.set(xlabel="AAA's yearly return (%)", ylabel="Number of years")
print(counts.tolist())
plt.show()
[2.0, 2.0]