how do you make multiple subplots have the same y axis?

I want the data to be comparable

import matplotlib.pyplot as plt

dt = {
    '2018':[0.31,10.49,39.2,12.58,138.21,94.55,60.97,103.4,95.59,74.85,22.48,6.45],
    '2019':[21.6,8.2,5,16.6,177.7,174.1,338.6,216.7,353.3,546.9,138.9,33]
}

x = [i for i, d in enumerate(list(dt.values())[0])]

plt.plot(x, *list(dt.values()))
plt.show()

exp



Update

import matplotlib.pyplot as plt
import random
import math

dt = {
    '2001':[0.31,10.49,39.2,12.58,138.21,94.55,60.97,103.4,95.59,74.85,22.48,6.45],
    '2002':[21.6,8.2,5,16.6,177.7,174.1,338.6,216.7,353.3,546.9,138.9,33],
    '2003':[0.31,10.49,39.2,12.58,138.21,94.55,60.97,103.4,95.59,74.85,22.48,6.45],
    '2004':[21.6,8.2,5,16.6,177.7,174.1,338.6,216.7,353.3,546.9,138.9,33],
    '2005':[0.31,10.49,39.2,12.58,138.21,94.55,60.97,103.4,95.59,74.85,22.48,6.45],
    '2006':[21.6,8.2,5,16.6,177.7,174.1,338.6,216.7,353.3,546.9,138.9,33],
}

def random_color():
    return "#%06x" % random.randint(0, 0xFFFFFF)

x = [i for i, d in enumerate(list(dt.values())[0])]

dts = []
for i in list(dt.values()):
    dts += i

min_dt = min(dts)
max_dt = max(dts)
sqr = math.ceil(math.sqrt(len(dt)))

counter = 0

for k, v in dt.items():
    plt.subplot(sqr, sqr, counter+1)
    plt.title(k)
    plt.ylim(min_dt, max_dt)
    plt.plot(x, list(dt.values())[counter], color=random_color())
    counter += 1

plt.tight_layout()
plt.show()

exp2

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top