[Solved] Customize axes in Matplotlib


You can display subscripts by writing your column names using LaTex:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(
    {
        0: {
            "Method 1": 31.7,
            "Method 2": 44.2,
            "Method 3": 75.6,
            "Method 4": 87.5,
            "Method 5": 88.6,
            "Method 6": 100.0,
        },
        1: {
            "Method 1": 32.9,
            "Method 2": 45.4,
            "Method 3": 72.2,
            "Method 4": 83.2,
            "Method 5": 84.1,
            "Method 6": 100.0,
        },
        2: {
            "Method 1": 33.7,
            "Method 2": 46.9,
            "Method 3": 69.2,
            "Method 4": 79.5,
            "Method 5": 80.7,
            "Method 6": 100.0,
        },
        3: {
            "Method 1": 34.4,
            "Method 2": 48.9,
            "Method 3": 67.4,
            "Method 4": 77.8,
            "Method 5": 79.6,
            "Method 6": 100.0,
        },
        4: {
            "Method 1": 35.2,
            "Method 2": 45.5,
            "Method 3": 63.6,
            "Method 4": 72.2,
            "Method 5": 74.5,
            "Method 6": 100.0,
        },
    }
)
df.columns = [
    "$w_1=1.0$\n$w_2=0.0$",
    "$w_1=0.75$\n$w_2=0.25$",
    "$w_1=0.5$\n$w_2=0.5$",
    "$w_1=0.25$\n$w_2=0.75$",
    "$w_1=0.0$\n$w_2=1.0$",
]

COLOURS = ['blue', 'green', 'red', 'yellow', 'pink', 'black']

fig, ax = plt.subplots(figsize=(12, 8))
for n, (label, data) in enumerate(df.iterrows()):
    ax.plot(data, marker="o", linestyle="none", label=label, c=COLOURS[n])
ax.grid()
ax.legend(loc="best")

This’ll give you:

enter image description here

You can pass different colours by changing what’s in the COLOURS object.

16

solved Customize axes in Matplotlib