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}}
7
  • Most people will not be happy if you just ask for code without trying something first. Commented Feb 18, 2021 at 3:15
  • 1
    @StephanyBernazzani they're right, although they could've been less snarky. This website has a pretty strict etiquette. See the intro tour, the help center, and this post on how to ask a good question. Commented Feb 18, 2021 at 3:20
  • My comment is definitely not un-nice, but anyway you can start with stackoverflow.com/questions/3100985/… Commented Feb 18, 2021 at 3:20
  • And stackoverflow.com/questions/47449741/… Commented Feb 18, 2021 at 3:23
  • Just a heads-up: Length is misspelled. It's written as Lenght in your code. Commented Feb 18, 2021 at 3:24

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
@blorgon I agree. The proper visualization would be a multi-bar graph, but this is what OP asked for.
@blorgon I edited the answer to also include a bar graph.
So much better. Props on going the extra mile lol.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.