0

I have a list of Tuples like this :

list_months = [ ('A', 'January'),
                ('A', 'January'),
                ('A', 'January'),  # Total 10 instances of ('A', 'January')
                ('A', 'March'),
                ('A', 'March'),
                ('A', 'March'),
                ('A', 'February'),
                ('A', 'February'),
                .............
                ('B', 'January'),
                ('B', 'January'),  # Total 15 instances of ('B', 'January')
                ('B', 'July'),
                ('B', 'July'),
                ............. ]

This was obtained via a Pandas dataframe using :

for index, val in b['Incident Reported Date Time'].iteritems():
    list_months.append((index, str(val)))

I want to be able to generate a MatPlotLib graph such that :
X-Axis -> Months
Y-Axis -> Volume
Multiple Colored Lines -> Each representing 'A', 'B', 'C', etc.


enter image description here

For example in this picture the Red line would represent 'A' and the blue line 'B'.
Since there are 10 of ('A', 'January') it shows the red line on 10 at the Y-Axis for the month of January and there are 15 of ('B', January') so it shows the blue line at 15 at the Y-Axis for the month of January.

How can I generate this and the legend dynamically in matplotlib for Python3 ?
The graph should be line graphs like in the example image.

3
  • 1
    I suggest you look at pandas. If you store your data in a pandas DataFrame you will be able to use its grouping and plotting abilities to do this task. Commented Aug 2, 2015 at 21:59
  • I extracted the data from pandas into a list of tuples to make it easier to plot. The data is originally from 2 columns of a Pandas dataframe. Using the command for index, val in b['Incident Reported Date Time'].iteritems(): list_months.append((index, str(val))) Commented Aug 2, 2015 at 22:07
  • 1
    Hint: It's just matplotlib, not MatPlotLib. Check the website. Commented Aug 3, 2015 at 0:57

1 Answer 1

3

It will be easier to plot if you leave the data in a DataFrame. I made a sample DataFrame using the example data from your post:

list_months = [ ('A', 'January'),
                ('A', 'January'),
                ('A', 'January'),  # Total 10 instances of ('A', 'January')
                ('A', 'March'),
                ('A', 'March'),
                ('A', 'March'),
                ('A', 'February'),
                ('A', 'February'),
                ('B', 'January'),
                ('B', 'January'),  # Total 15 instances of ('B', 'January')
                ('B', 'July'),
                ('B', 'July')
            ]
d = pandas.DataFrame(list_months, columns=["Item", "Month"]).fillna(0)

>>> d
   Item     Month
0     A   January
1     A   January
2     A   January
3     A     March
4     A     March
5     A     March
6     A  February
7     A  February
8     B   January
9     B   January
10    B      July
11    B      July

Then you can get your plot easily:

>>> d.groupby(['Month', 'Item']).size().unstack().loc[['January', 'February', 'March', 'July']].fillna(0).plot(kind='line')

enter image description here

(The .loc['January', 'February', ...] bit is only necessarily to get the months in temporal order rather than alphabetical order. In the long run, you're probably better off storing this information via a datetime object or a number representing the month.)

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

3 Comments

How would the code be different if I had a column for date time objects and had to convert it to show monthwise in the graph ?
@MeghdeepRay: If you have a question about how to plot data with datetime objects, the best thing is to ask a separate question in which you provide sample data with datetime objects.
I put up a follow up question, would be grateful if you could help : stackoverflow.com/questions/31777482/…

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.