1

How can I get the following nested dictionary into a line graph using matplotlib?

Ideally on the x axis I have each poet's name and 4 lines on the graph, each for every key (average sentence length, number of sentences, longest, shortest).

poemsD = {'Maya Angelou': {'Average Sentence Lenght': 16.68,
  'Number of sentences': 40,
  'Longest': 45,
  'Shortest': 2},
 'Amanda Gorman': {'Average Sentence Lenght': 14.49,
  'Number of sentences': 49,
  'Longest': 38,
  'Shortest': 3},
 'Elizabeth Alexander': {'Average Sentence Lenght': 14.57,
  'Number of sentences': 23,
  'Longest': 40,
  'Shortest': 1},
 'Miller Williams': {'Average Sentence Lenght': 12.83,
  'Number of sentences': 24,
  'Longest': 39,
  'Shortest': 2},
 'Richard Blanco': {'Average Sentence Lenght': 34.53,
  'Number of sentences': 17,
  'Longest': 63,
  'Shortest': 1},
 'Robert Frost': {'Average Sentence Lenght': 30.25,
  'Number of sentences': 4,
  'Longest': 37,
  'Shortest': 25}}
ddejohn
  • 8,775
  • 3
  • 17
  • 30
  • Most people will not be happy if you just ask for code without trying something first. – user202729 Feb 18 '21 at 03:15
  • 1
    @StephanyBernazzani they're right, although they could've been less snarky. This website has a pretty strict etiquette. See the [intro tour,](https://stackoverflow.com/tour) the [help center,](https://stackoverflow.com/help) and this post on [how to ask a good question.](https://stackoverflow.com/help/how-to-ask) – ddejohn Feb 18 '21 at 03:20
  • My comment is definitely not un-nice, but anyway you can start with https://stackoverflow.com/questions/3100985/plot-with-custom-text-for-x-axis-points – user202729 Feb 18 '21 at 03:20
  • And https://stackoverflow.com/questions/47449741/plotting-multiple-lines-in-python – user202729 Feb 18 '21 at 03:23
  • Just a heads-up: `Length` is misspelled. It's written as `Lenght` in your code. – ddejohn Feb 18 '21 at 03:24
  • It's consistently wrong so it only makes the output misspelled. – user202729 Feb 18 '21 at 03:24
  • ...hence why I'm pointing it out. – ddejohn Feb 18 '21 at 03:25

1 Answers1

2

You can to it by creating a Pandas dataframe and plotting it:

import pandas as pd
import matplotlib.pyplot as plt

poemsD = {'Maya Angelou': {'Average Sentence Lenght': 16.68, 'Number of sentences': 40, 'Longest': 45, 'Shortest': 2},
          'Amanda Gorman': {'Average Sentence Lenght': 14.49, 'Number of sentences': 49, 'Longest': 38, 'Shortest': 3},
          'Elizabeth Alexander': {'Average Sentence Lenght': 14.57, 'Number of sentences': 23, 'Longest': 40, 'Shortest': 1},
          'Miller Williams': {'Average Sentence Lenght': 12.83, 'Number of sentences': 24, 'Longest': 39, 'Shortest': 2},
          'Richard Blanco': {'Average Sentence Lenght': 34.53, 'Number of sentences': 17, 'Longest': 63, 'Shortest': 1},
          'Robert Frost': {'Average Sentence Lenght': 30.25, 'Number of sentences': 4, 'Longest': 37, 'Shortest': 25}}

df = pd.DataFrame(poemsD)
df2 = df.transpose()
df2.plot(figsize = (10,8))
plt.show()

enter image description here

A better visualization would be a bar graph. This is very easy to do by changing one line of the code:

df2.plot(figsize = (10,8), kind = 'bar')

enter image description here

pakpe
  • 5,391
  • 2
  • 8
  • 23
  • I know you're just providing OP with an answer, but *wow* that is an *awful* visualization for this data. OP, please consider an alternate visualization, as this graph should never actually see the light of day. – ddejohn Feb 18 '21 at 03:53
  • @blorgon I agree. The proper visualization would be a multi-bar graph, but this is what OP asked for. – pakpe Feb 18 '21 at 03:56
  • 1
    @blorgon I edited the answer to also include a bar graph. – pakpe Feb 18 '21 at 04:02
  • So much better. Props on going the extra mile lol. – ddejohn Feb 18 '21 at 04:03
  • I can now see why this is not a good visualization and I appreciate showing me the multi-bar graph. It def looks clearer and is a more efficient way of displaying the data! – Stephany Bernazzani Feb 18 '21 at 13:39