0

I have a dictionary with 17 keys, all with equal number of records. I want to make 17 subplots with each subplot showing each key's graph. When I use my code, I get all 17 lines(line chart) in all subplots.

My Code:

for j in range(1,18):
plt.subplot(4,5,j)    
for index, (key, value) in enumerate(degree_gender_ratios.items()):
        plt.plot(value)

enter image description here

Can someone help me with this?

1 Answers1

1

I assume that you know the number of entries in your dict, or an upper bound. Define your plot with at least as many subplots as needed (here 4x5 = 20):

from matplotlib import pyplot as plt
import numpy as np
f, ax = plt.subplots(4,5)

Now, just operate over each of your entries in the dict. No need for enumerations:

for a, (key, value) in zip(ax.flatten(), degree_gender_ratios.items()):
    a.plot(value)
planetmaker
  • 5,884
  • 3
  • 28
  • 37
  • It works thank you, but by using 4*5=20 subplots, it generates three empty subplots because I only have data enough for 17 such plots. Any way those three could be removed? – Prashanth Cp Nov 22 '18 at 14:27
  • 1
    That's a different question, answered at the end of a quick search: https://stackoverflow.com/questions/10035446/how-can-i-make-a-blank-subplot-in-matplotlib – planetmaker Nov 22 '18 at 14:29