-2

I am trying to plot a bar graph where the x-axis represents the first element of each tuple and the y-axis is the second element of each tuple.

Something very similar to this post: Using Counter() in Python to build histogram?

arr = [(0, 152),
     (1, 106),
     (2, 71),
     (3, 89),
     (4, 69),
     (5, 83),
     (6, 139),
     (7, 141),
     (8, 164),
     (9, 75),
     (10, 98)]

How can I do this?

I have this so far:

Input:

plt.bar(counts.items())

Output:

TypeError: <lambda>() takes at least 2 arguments (1 given)

Thanks :)

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • start googling matplotlib, look for examples on bargraphs, adapt it to your data. fiddle around until it works. In short - code. Please go over [how to ask](https://stackoverflow.com/help/how-to-ask) and [on-topic](https://stackoverflow.com/help/on-topic) again and if you have questions provide your code as [mvce](https://stackoverflow.com/help/mcve). If you encounter errors, copy and paste the error message verbatim ( word for word) into your question. Avoid using screenshots unless you need to convey layout errors. We can NOT copy and paste your image into our IDEs to fix your code. – Patrick Artner Feb 19 '18 at 19:01
  • `bar` needs two arguments, x and y. So `x,y = zip(*arr); plt.bar(x,y)` gives you a bar plot as desired. – ImportanceOfBeingErnest Feb 19 '18 at 19:10

1 Answers1

1

One quick solution that seems to work well is to load the data into a Panda's DataFrame and use it's plot function:

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

x = [(0, 152),
     (1, 106),
     (2, 71),
     (3, 89),
     (4, 69),
     (5, 83),
     (6, 139),
     (7, 141),
     (8, 164),
     (9, 75),
     (10, 98)]

pd.DataFrame(x, columns=['lbl','val']).set_index('lbl').plot(kind='bar');

how_the_above_code_renders_the_chart

Jeremy
  • 86
  • 4